From 5faefe0c6c2e3ba98492f1908c85140350a28586 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 29 Jan 2024 10:31:57 +0000 Subject: [PATCH] [Feature] Allow to add templates to redis history prefix Issue: #4793 Closes: #4793 --- conf/modules.d/history_redis.conf | 2 +- src/plugins/lua/history_redis.lua | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/conf/modules.d/history_redis.conf b/conf/modules.d/history_redis.conf index 0d1c7f66f..b55d2fbba 100644 --- a/conf/modules.d/history_redis.conf +++ b/conf/modules.d/history_redis.conf @@ -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 diff --git a/src/plugins/lua/history_redis.lua b/src/plugins/lua/history_redis.lua index a5f4ec854..01be2dacd 100644 --- a/src/plugins/lua/history_redis.lua +++ b/src/plugins/lua/history_redis.lua @@ -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', }) -- 2.39.5