Skip to content

Commit 3f17bde

Browse files
Added Time Format conversion using lua script through filter in "Lua filter" (fluent#1417)
* Added Timeformat conversion using lua script through filter Signed-off-by: fahadahammed <[email protected]> * Added inline lua script for yaml for better readability suggestion from pat. Signed-off-by: fahadahammed <[email protected]> * Update pipeline/filters/lua.md Co-authored-by: Craig Norris <[email protected]> Signed-off-by: Fahad Ahammed <[email protected]> * Update pipeline/filters/lua.md Co-authored-by: Craig Norris <[email protected]> Signed-off-by: Fahad Ahammed <[email protected]> * Update pipeline/filters/lua.md Co-authored-by: Craig Norris <[email protected]> Signed-off-by: Fahad Ahammed <[email protected]> --------- Signed-off-by: fahadahammed <[email protected]> Signed-off-by: Fahad Ahammed <[email protected]> Co-authored-by: Craig Norris <[email protected]>
1 parent b2051b8 commit 3f17bde

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

pipeline/filters/lua.md

+131
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,134 @@ 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+
The following example converts a field's specific type of `datetime` format to
444+
`utc ISO 8601` format.
445+
446+
#### Lua script
447+
448+
Script `custom_datetime_format.lua`
449+
450+
```lua
451+
function convert_to_utc(tag, timestamp, record)
452+
local date_time = record["pub_date"]
453+
local new_record = record
454+
if date_time then
455+
if string.find(date_time, ",") then
456+
local pattern = "(%a+, %d+ %a+ %d+ %d+:%d+:%d+) ([+-]%d%d%d%d)"
457+
local date_part, zone_part = date_time:match(pattern)
458+
459+
if date_part and zone_part then
460+
local command = string.format("date -u -d '%s %s' +%%Y-%%m-%%dT%%H:%%M:%%SZ", date_part, zone_part)
461+
local handle = io.popen(command)
462+
local result = handle:read("*a")
463+
handle:close()
464+
new_record["pub_date"] = result:match("%S+")
465+
end
466+
end
467+
end
468+
return 1, timestamp, new_record
469+
end
470+
```
471+
472+
#### Configuration
473+
474+
Use this configuration to obtain a JSON key with `datetime`, and then convert it to
475+
another format.
476+
477+
{% tabs %}
478+
{% tab title="fluent-bit.conf" %}
479+
```ini
480+
[INPUT]
481+
Name dummy
482+
Dummy {"event": "Restock", "pub_date": "Tue, 30 Jul 2024 18:01:06 +0000"}
483+
Tag event_category_a
484+
485+
[INPUT]
486+
Name dummy
487+
Dummy {"event": "Soldout", "pub_date": "Mon, 29 Jul 2024 10:15:00 +0600"}
488+
Tag event_category_b
489+
490+
491+
[FILTER]
492+
Name lua
493+
Match *
494+
Script custom_datetime_format.lua
495+
call convert_to_utc
496+
497+
[Output]
498+
Name stdout
499+
Match *
500+
```
501+
{% endtab %}
502+
503+
{% tab title="fluent-bit.yaml" %}
504+
```yaml
505+
pipeline:
506+
inputs:
507+
- name: dummy
508+
dummy: '{"event": "Restock", "pub_date": "Tue, 30 Jul 2024 18:01:06 +0000"}'
509+
tag: event_category_a
510+
511+
- name: dummy
512+
dummy: '{"event": "Soldout", "pub_date": "Mon, 29 Jul 2024 10:15:00 +0600"}'
513+
tag: event_category_b
514+
515+
filters:
516+
- name: lua
517+
match: '*'
518+
code: |
519+
function convert_to_utc(tag, timestamp, record)
520+
local date_time = record["pub_date"]
521+
local new_record = record
522+
if date_time then
523+
if string.find(date_time, ",") then
524+
local pattern = "(%a+, %d+ %a+ %d+ %d+:%d+:%d+) ([+-]%d%d%d%d)"
525+
local date_part, zone_part = date_time:match(pattern)
526+
if date_part and zone_part then
527+
local command = string.format("date -u -d '%s %s' +%%Y-%%m-%%dT%%H:%%M:%%SZ", date_part, zone_part)
528+
local handle = io.popen(command)
529+
local result = handle:read("*a")
530+
handle:close()
531+
new_record["pub_date"] = result:match("%S+")
532+
end
533+
end
534+
end
535+
return 1, timestamp, new_record
536+
end
537+
call: convert_to_utc
538+
539+
outputs:
540+
- name: stdout
541+
match: '*'
542+
```
543+
{% endtab %}
544+
{% endtabs %}
545+
546+
#### Input
547+
548+
```json
549+
{"event": "Restock", "pub_date": "Tue, 30 Jul 2024 18:01:06 +0000"}
550+
```
551+
and
552+
553+
```json
554+
{"event": "Soldout", "pub_date": "Mon, 29 Jul 2024 10:15:00 +0600"}
555+
```
556+
Which are handled by dummy in this example.
557+
558+
#### Output
559+
560+
The output of this process shows the conversion of the `datetime` of two timezones to
561+
`ISO 8601` format in `UTC`.
562+
563+
```ini
564+
...
565+
[2024/08/01 00:56:25] [ info] [output:stdout:stdout.0] worker #0 started
566+
[0] event_category_a: [[1722452186.727104902, {}], {"event"=>"Restock", "pub_date"=>"2024-07-30T18:01:06Z"}]
567+
[0] event_category_b: [[1722452186.730255842, {}], {"event"=>"Soldout", "pub_date"=>"2024-07-29T04:15:00Z"}]
568+
...
569+
```

0 commit comments

Comments
 (0)