Skip to content

Commit 7342199

Browse files
committed
Added Timeformat conversion using lua script through filter
Signed-off-by: fahadahammed <[email protected]>
1 parent b2051b8 commit 7342199

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

pipeline/filters/lua.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,113 @@ pipeline:
436436
#### Output
437437

438438
In the output only the messages with response code 0 or greater than 399 are shown.
439+
440+
441+
### Timeformat Conversion
442+
443+
In this example, we want to convert specific type of datetime format of a field to `utc ISO 8601` format.
444+
445+
#### Lua script
446+
447+
Script `custom_datetime_format.lua`
448+
449+
```lua
450+
function convert_to_utc(tag, timestamp, record)
451+
local date_time = record["pub_date"]
452+
local new_record = record
453+
if date_time then
454+
if string.find(date_time, ",") then
455+
local pattern = "(%a+, %d+ %a+ %d+ %d+:%d+:%d+) ([+-]%d%d%d%d)"
456+
local date_part, zone_part = date_time:match(pattern)
457+
458+
if date_part and zone_part then
459+
local command = string.format("date -u -d '%s %s' +%%Y-%%m-%%dT%%H:%%M:%%SZ", date_part, zone_part)
460+
local handle = io.popen(command)
461+
local result = handle:read("*a")
462+
handle:close()
463+
new_record["pub_date"] = result:match("%S+")
464+
end
465+
end
466+
end
467+
return 1, timestamp, new_record
468+
end
469+
```
470+
471+
#### Configuration
472+
473+
Configuration to get a json key with datetime and convert it to another format.
474+
475+
{% tabs %}
476+
{% tab title="fluent-bit.conf" %}
477+
```ini
478+
[INPUT]
479+
Name dummy
480+
Dummy {"event": "Restock", "pub_date": "Tue, 30 Jul 2024 18:01:06 +0000"}
481+
Tag event_category_a
482+
483+
[INPUT]
484+
Name dummy
485+
Dummy {"event": "Soldout", "pub_date": "Mon, 29 Jul 2024 10:15:00 +0600"}
486+
Tag event_category_b
487+
488+
489+
[FILTER]
490+
Name lua
491+
Match *
492+
Script custom_datetime_format.lua
493+
call convert_to_utc
494+
495+
[Output]
496+
Name stdout
497+
Match *
498+
```
499+
{% endtab %}
500+
501+
{% tab title="fluent-bit.yaml" %}
502+
```yaml
503+
pipeline:
504+
inputs:
505+
- name: dummy
506+
dummy: '{"event": "Restock", "pub_date": "Tue, 30 Jul 2024 18:01:06 +0000"}'
507+
tag: event_category_a
508+
509+
- name: dummy
510+
dummy: '{"event": "Soldout", "pub_date": "Mon, 29 Jul 2024 10:15:00 +0600"}'
511+
tag: event_category_b
512+
513+
filters:
514+
- name: lua
515+
match: '*'
516+
script: custom_datetime_format.lua
517+
call: convert_to_utc
518+
519+
outputs:
520+
- name: stdout
521+
match: '*'
522+
```
523+
{% endtab %}
524+
{% endtabs %}
525+
526+
#### Input
527+
528+
```json
529+
{"event": "Restock", "pub_date": "Tue, 30 Jul 2024 18:01:06 +0000"}
530+
```
531+
and
532+
533+
```json
534+
{"event": "Soldout", "pub_date": "Mon, 29 Jul 2024 10:15:00 +0600"}
535+
```
536+
Which are handled by dummy in this example.
537+
538+
#### Output
539+
540+
In the output It will convert the date time of two timezone to `ISO 8601` format in `UTC`.
541+
542+
```ini
543+
...
544+
[2024/08/01 00:56:25] [ info] [output:stdout:stdout.0] worker #0 started
545+
[0] event_category_a: [[1722452186.727104902, {}], {"event"=>"Restock", "pub_date"=>"2024-07-30T18:01:06Z"}]
546+
[0] event_category_b: [[1722452186.730255842, {}], {"event"=>"Soldout", "pub_date"=>"2024-07-29T04:15:00Z"}]
547+
...
548+
```

0 commit comments

Comments
 (0)