Skip to content

Commit

Permalink
filter_lua: support nil_str to replace null value
Browse files Browse the repository at this point in the history
Signed-off-by: Takahiro Yamashita <[email protected]>
  • Loading branch information
nokute78 committed Aug 6, 2023
1 parent 410fbac commit 88ed925
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion plugins/filter_lua/lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
Expand Down
9 changes: 9 additions & 0 deletions plugins/filter_lua/lua_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions plugins/filter_lua/lua_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 88ed925

Please sign in to comment.