Skip to content

Commit e6c946e

Browse files
authored
filter_lua: support new return value to keep timestamp (#2100)
* filter_lua: support return value 2 to keep timestamp.(#2015) Signed-off-by: Takahiro YAMASHITA <nokute78@gmail.com> * scripts: add comment about keeping timestamp Signed-off-by: Takahiro YAMASHITA <nokute78@gmail.com>
1 parent 5c38600 commit e6c946e

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

plugins/filter_lua/lua.c

+14-9
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ static int cb_lua_init(struct flb_filter_instance *f_ins,
297297
return 0;
298298
}
299299

300-
static int pack_result (double ts, msgpack_packer *pck, msgpack_sbuffer *sbuf,
300+
static int pack_result (struct flb_time *ts, msgpack_packer *pck, msgpack_sbuffer *sbuf,
301301
char *data, size_t bytes)
302302
{
303303
int ret;
@@ -306,7 +306,6 @@ static int pack_result (double ts, msgpack_packer *pck, msgpack_sbuffer *sbuf,
306306
size_t off = 0;
307307
msgpack_object root;
308308
msgpack_unpacked result;
309-
struct flb_time t;
310309

311310
msgpack_unpacked_init(&result);
312311
ret = msgpack_unpack_next(&result, data, bytes, &off);
@@ -334,8 +333,7 @@ static int pack_result (double ts, msgpack_packer *pck, msgpack_sbuffer *sbuf,
334333
msgpack_pack_array(pck, 2);
335334

336335
/* timestamp: convert from double to Fluent Bit format */
337-
flb_time_from_double(&t, ts);
338-
flb_time_append_to_msgpack(&t, pck, 0);
336+
flb_time_append_to_msgpack(ts, pck, 0);
339337

340338
/* Pack lua table */
341339
msgpack_pack_object(pck, *(map+i));
@@ -363,9 +361,7 @@ static int pack_result (double ts, msgpack_packer *pck, msgpack_sbuffer *sbuf,
363361
/* main array */
364362
msgpack_pack_array(pck, 2);
365363

366-
/* timestamp: convert from double to Fluent Bit format */
367-
flb_time_from_double(&t, ts);
368-
flb_time_append_to_msgpack(&t, pck, 0);
364+
flb_time_append_to_msgpack(ts, pck, 0);
369365

370366
/* Pack lua table */
371367
msgpack_sbuffer_write(sbuf, data, bytes);
@@ -391,6 +387,7 @@ static int cb_lua_filter(const void *data, size_t bytes,
391387
msgpack_unpacked result;
392388
msgpack_sbuffer tmp_sbuf;
393389
msgpack_packer tmp_pck;
390+
struct flb_time t_orig;
394391
struct flb_time t;
395392
struct lua_filter *ctx = filter_context;
396393
/* Lua return values */
@@ -413,6 +410,7 @@ static int cb_lua_filter(const void *data, size_t bytes,
413410

414411
/* Get timestamp */
415412
flb_time_pop_from_msgpack(&t, &result, &p);
413+
t_orig = t;
416414
ts = flb_time_to_double(&t);
417415

418416
/* Prepare function call, pass 3 arguments, expect 3 return values */
@@ -442,8 +440,15 @@ static int cb_lua_filter(const void *data, size_t bytes,
442440
else if (l_code == 0) { /* Keep record, repack */
443441
msgpack_pack_object(&tmp_pck, root);
444442
}
445-
else if (l_code == 1) { /* Modified, pack new data */
446-
ret = pack_result(l_timestamp, &tmp_pck, &tmp_sbuf,
443+
else if (l_code == 1 || l_code == 2) { /* Modified, pack new data */
444+
if (l_code == 1) {
445+
flb_time_from_double(&t, l_timestamp);
446+
}
447+
else if(l_code == 2) {
448+
/* Keep the timestamp */
449+
t = t_orig;
450+
}
451+
ret = pack_result(&t, &tmp_pck, &tmp_sbuf,
447452
data_sbuf.data, data_sbuf.size);
448453
if (ret == FLB_FALSE) {
449454
flb_plg_error(ctx->ins, "invalid table returned at %s(), %s",

scripts/test.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
- cb_replace => Replace record content with a new table
88
99
The key inside each function is to do a proper handling of the
10-
return values. Each function must return 3 values:
10+
return values. Each function must return 4 values:
1111
1212
return code, timestamp, record
1313
1414
where:
1515
1616
- code : -1 record must be deleted
1717
0 record not modified, keep the original
18-
1 record was modified, replace content
18+
1 record was modified, replace timestamp and record.
19+
2 record was modified, replace record and keep timestamp.
1920
- timestamp: Unix timestamp with precision (double)
2021
- record : Table with multiple key/val
2122

0 commit comments

Comments
 (0)