]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Feature] Allow to add templates to redis history prefix 4801/head
authorVsevolod Stakhov <vsevolod@rspamd.com>
Mon, 29 Jan 2024 10:31:57 +0000 (10:31 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Mon, 29 Jan 2024 10:31:57 +0000 (10:31 +0000)
Issue: #4793
Closes: #4793
conf/modules.d/history_redis.conf
src/plugins/lua/history_redis.lua

index 0d1c7f66f014acb8e42e3a909dd0da6bd63efe5b..b55d2fbbafc97d8929ee2e588199b8b4fae1dde0 100644 (file)
@@ -14,7 +14,7 @@
 
 history_redis {
   #servers = 127.0.0.1:6379; # Redis server to store history
-  key_prefix = "rs_history"; # Default key name
+  key_prefix = "rs_history{{HOSTNAME}}{{COMPRESS}}"; # Default key name template
   nrows = 200; # Default rows limit
   compress = true; # Use zstd compression when storing data in redis
   subject_privacy = false; # subject privacy is off
index a5f4ec854d7a73be4a1d5a28a2a2b371b54ba1fc..01be2dacd715a84433cb2e64833b3ad50a526491 100644 (file)
@@ -49,12 +49,15 @@ local ucl = require "ucl"
 local ts = (require "tableshape").types
 local E = {}
 local N = "history_redis"
-local hostname = rspamd_util.get_hostname()
+
+local template_env = {
+  HOSTNAME = rspamd_util.get_hostname(),
+}
 
 local redis_params
 
 local settings = {
-  key_prefix = 'rs_history', -- default key name
+  key_prefix = 'rs_history{{HOSTNAME}}{{COMPRESS}}', -- default key name template
   expire = nil, -- default no expire
   nrows = 200, -- default rows limit
   compress = true, -- use zstd compression when storing data in redis
@@ -146,7 +149,7 @@ local function history_save(task)
   end
 
   local data = task:get_protocol_reply { 'metrics', 'basic' }
-  local prefix = settings.key_prefix .. hostname
+  local prefix = lua_util.jinja_template(settings.key_prefix, template_env)
 
   if data then
     normalise_results(data, task)
@@ -159,8 +162,6 @@ local function history_save(task)
 
   if settings.compress then
     json = rspamd_util.zstd_compress(json)
-    -- Distinguish between compressed and non-compressed options
-    prefix = prefix .. '_zst'
   end
 
   local ret, conn, _ = lua_redis.rspamd_redis_make_request(task,
@@ -182,11 +183,7 @@ local function history_save(task)
 end
 
 local function handle_history_request(task, conn, from, to, reset)
-  local prefix = settings.key_prefix .. hostname
-  if settings.compress then
-    -- Distinguish between compressed and non-compressed options
-    prefix = prefix .. '_zst'
-  end
+  local prefix = lua_util.jinja_template(settings.key_prefix, template_env)
 
   if reset then
     local function redis_ltrim_cb(err, _)
@@ -290,6 +287,12 @@ if opts then
   end
   settings = res
 
+  if settings.compress then
+    template_env.COMPRESS = '_zst'
+  else
+    template_env.COMPRESS = ''
+  end
+
   redis_params = lua_redis.parse_redis_server('history_redis')
   if not redis_params then
     rspamd_logger.infox(rspamd_config, 'no servers are specified, disabling module')
@@ -302,7 +305,7 @@ if opts then
       flags = 'empty,explicit_disable,ignore_passthrough',
       augmentations = { string.format("timeout=%f", redis_params.timeout or 0.0) }
     })
-    lua_redis.register_prefix(settings.key_prefix .. hostname, N,
+    lua_redis.register_prefix(lua_util.jinja_template(settings.key_prefix, template_env), N,
         "Redis history", {
           type = 'list',
         })