Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter_lua: expose env variables in FLB_ENV Lua table #9617

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions plugins/filter_lua/lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_env.h>
#include <fluent-bit/flb_log_event_decoder.h>
#include <fluent-bit/flb_log_event_encoder.h>

#include <msgpack.h>

#include "fluent-bit/flb_mem.h"
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down
Loading