From 88ed9254833491a9a0df82ca244257f3ec4dcd09 Mon Sep 17 00:00:00 2001 From: Takahiro Yamashita Date: Sun, 6 Aug 2023 13:19:36 +0900 Subject: [PATCH] filter_lua: support nil_str to replace null value Signed-off-by: Takahiro Yamashita --- plugins/filter_lua/lua.c | 9 ++++++++- plugins/filter_lua/lua_config.c | 9 +++++++++ plugins/filter_lua/lua_config.h | 1 + 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/plugins/filter_lua/lua.c b/plugins/filter_lua/lua.c index a95c87da1fe..6d9a366a15c 100644 --- a/plugins/filter_lua/lua.c +++ b/plugins/filter_lua/lua.c @@ -503,7 +503,7 @@ static int cb_lua_filter(const void *data, size_t bytes, lua_pushnumber(ctx->lua->state, ts); } - flb_lua_pushmsgpack(ctx->lua->state, log_event.body); + flb_lua_pushmsgpack(ctx->lua->state, log_event.body, ctx->l2cc.l2c_nil_str); if (ctx->protected_mode) { ret = lua_pcall(ctx->lua->state, 3, 3, 0); if (ret != 0) { @@ -671,6 +671,13 @@ static struct flb_config_map config_map[] = { "If these keys are matched, the fields are converted to array. " "If more than one key, delimit by space." }, + { + FLB_CONFIG_MAP_STR, "nil_str", NULL, + 0, FLB_TRUE, offsetof(struct lua_filter, nil_str), + "If set, nil value will be replaced by this string in Lua. " + "It is to prevent remove nil value from record " + "since nil value is to delete key/value from associative array in Lua." + }, { FLB_CONFIG_MAP_BOOL, "protected_mode", "true", 0, FLB_TRUE, offsetof(struct lua_filter, protected_mode), diff --git a/plugins/filter_lua/lua_config.c b/plugins/filter_lua/lua_config.c index f0c15419610..74467a61962 100644 --- a/plugins/filter_lua/lua_config.c +++ b/plugins/filter_lua/lua_config.c @@ -127,6 +127,12 @@ struct lua_filter *lua_config_create(struct flb_filter_instance *ins, } lf->l2cc.l2c_types_num = 0; + if (lf->nil_str) { + lf->l2cc.l2c_nil_str = flb_sds_create(lf->nil_str); + } + else { + lf->l2cc.l2c_nil_str = NULL; + } tmp = flb_filter_get_property("type_int_key", ins); if (tmp) { split = flb_utils_split(tmp, ' ', FLB_LUA_L2C_TYPES_NUM_MAX); @@ -189,6 +195,9 @@ void lua_config_destroy(struct lua_filter *lf) if (lf->buffer) { flb_sds_destroy(lf->buffer); } + if (lf->l2cc.l2c_nil_str) { + flb_sds_destroy(lf->l2cc.l2c_nil_str); + } mk_list_foreach_safe(head, tmp_list, &lf->l2cc.l2c_types) { l2c = mk_list_entry(head, struct flb_lua_l2c_type, _head); diff --git a/plugins/filter_lua/lua_config.h b/plugins/filter_lua/lua_config.h index e5cc1a9b4e5..1fb83fde63a 100644 --- a/plugins/filter_lua/lua_config.h +++ b/plugins/filter_lua/lua_config.h @@ -33,6 +33,7 @@ struct lua_filter { flb_sds_t script; /* lua script path */ flb_sds_t call; /* function name */ flb_sds_t buffer; /* json dec buffer */ + flb_sds_t nil_str; /* The string to represent nil value in Lua */ int protected_mode; /* exec lua function in protected mode */ int time_as_table; /* timestamp as a Lua table */ struct flb_lua_l2c_config l2cc; /* lua -> C config */