From 6c3df1b8721778d70121bf64d0f73f56cd5b7492 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Mon, 18 Nov 2024 22:22:20 -0600 Subject: [PATCH] filter_lua: expose env variables in FLB_ENV Lua table Signed-off-by: Eduardo Silva --- plugins/filter_lua/lua.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/plugins/filter_lua/lua.c b/plugins/filter_lua/lua.c index 5186955b8ae..de7e6983188 100644 --- a/plugins/filter_lua/lua.c +++ b/plugins/filter_lua/lua.c @@ -27,8 +27,10 @@ #include #include #include +#include #include #include + #include #include "fluent-bit/flb_mem.h" @@ -76,6 +78,31 @@ static int cb_lua_pre_run(struct flb_filter_instance *f_ins, return ret; } +static int env_variables(struct flb_config *config, struct flb_luajit *lj) +{ + struct mk_list *list; + struct mk_list *head; + struct flb_env *env; + struct flb_hash_table_entry *entry; + + lua_newtable(lj->state); + + env = (struct flb_env *) config->env; + list = (struct mk_list *) &env->ht->entries; + mk_list_foreach(head, list) { + entry = mk_list_entry(head, struct flb_hash_table_entry, _head_parent); + if (entry->val_size <= 0) { + continue; + } + lua_pushlstring(lj->state, entry->key, entry->key_len); + lua_pushlstring(lj->state, entry->val, entry->val_size); + lua_settable(lj->state, -3); + } + + lua_setglobal(lj->state, "FLB_ENV"); + return 0; +} + static int cb_lua_init(struct flb_filter_instance *f_ins, struct flb_config *config, void *data) @@ -101,6 +128,9 @@ static int cb_lua_init(struct flb_filter_instance *f_ins, } ctx->lua = lj; + /* register environment variables */ + env_variables(config, lj); + if (ctx->enable_flb_null) { flb_lua_enable_flb_null(lj->state); }