Skip to content

Commit 20bf74f

Browse files
lecarospatrick-stephenslockewritesdocs
authored
filters: lua: add docs for Fluent Bit env vars (PR #9617) (#1523)
* filters: lua: reorganize docs Signed-off-by: lecaros <[email protected]> * filters: lua: address typos Signed-off-by: lecaros <[email protected]> * filters: lua: add example to access configuration variables. Signed-off-by: lecaros <[email protected]> * filters: lua: add format to output sample Signed-off-by: lecaros <[email protected]> * filters: lua: add format to output sample Signed-off-by: lecaros <[email protected]> * filters: lua: add suggestion Co-authored-by: Pat <[email protected]> Signed-off-by: José Lecaros <[email protected]> * filters: lua: add suggestion Co-authored-by: Pat <[email protected]> Signed-off-by: José Lecaros <[email protected]> * filters: lua: commit suggestion Co-authored-by: Adam Locke <[email protected]> Signed-off-by: José Lecaros <[email protected]> * filters: lua: commit suggestion Co-authored-by: Adam Locke <[email protected]> Signed-off-by: José Lecaros <[email protected]> * filters: lua: commit suggestion Co-authored-by: Adam Locke <[email protected]> Signed-off-by: José Lecaros <[email protected]> --------- Signed-off-by: lecaros <[email protected]> Signed-off-by: José Lecaros <[email protected]> Co-authored-by: Pat <[email protected]> Co-authored-by: Adam Locke <[email protected]>
1 parent d812346 commit 20bf74f

File tree

1 file changed

+82
-25
lines changed

1 file changed

+82
-25
lines changed

pipeline/filters/lua.md

+82-25
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,9 @@ Each callback **must** return three values:
114114
| timestamp | double | If code equals 1, the original record timestamp will be replaced with this new value. |
115115
| record | table | If code equals 1, the original record information will be replaced with this new value. Note that the _record_ value **must** be a valid Lua table. This value can be an array of tables (i.e., array of objects in JSON format), and in that case the input record is effectively split into multiple records. (see below for more details) |
116116

117-
### Code Examples
117+
## Features
118118

119-
For functional examples of this interface, please refer to the code samples provided in the source code of the project located here:
120-
121-
[https://github.com/fluent/fluent-bit/tree/master/scripts](https://github.com/fluent/fluent-bit/tree/master/scripts)
122-
123-
#### Inline configuration
119+
### Inline configuration
124120

125121
The [Fluent Bit smoke tests](https://github.com/fluent/fluent-bit/tree/master/packaging/testing/smoke/container) include examples to verify during CI.
126122

@@ -180,7 +176,25 @@ pipeline:
180176
{% endtab %}
181177
{% endtabs %}
182178

183-
#### Environment variable processing
179+
### Number Type
180+
181+
Lua treats numbers as a `double` type, which means an `integer` type
182+
containing data like user IDs and log levels will be converted to a `double`.
183+
To avoid type conversion, use the `type_int_key` property.
184+
185+
### Protected Mode
186+
187+
Fluent Bit supports protected mode to prevent crashes if it executes an invalid Lua script.
188+
See [Error Handling in Application Code](https://www.lua.org/pil/24.3.1.html) in
189+
the Lua documentation for more information.
190+
191+
192+
## Code Examples
193+
194+
For functional examples of this interface, please refer to the code samples provided in the source code of the project located here:
195+
196+
197+
### Processing environment variables
184198

185199
As an example that combines a bit of LUA processing with the [Kubernetes filter](./kubernetes.md) that demonstrates using environment variables with LUA regex and substitutions.
186200

@@ -197,11 +211,11 @@ We want to extract the `sandboxbsh` name and add it to our record as a special k
197211
{% tab title="fluent-bit.conf" %}
198212
```
199213
[FILTER]
200-
Name lua
201-
Alias filter-iots-lua
202-
Match iots_thread.*
203-
Script filters.lua
204-
Call set_landscape_deployment
214+
Name lua
215+
Alias filter-iots-lua
216+
Match iots_thread.*
217+
Script filters.lua
218+
Call set_landscape_deployment
205219
```
206220
{% endtab %}
207221

@@ -244,14 +258,6 @@ filters.lua:
244258
end
245259
```
246260

247-
### Number Type
248-
249-
+Lua treats number as double. It means an integer field (e.g. IDs, log levels) will be converted double. To avoid type conversion, The `type_int_key` property is available.
250-
251-
### Protected Mode
252-
253-
Fluent Bit supports protected mode to prevent crash when executes invalid Lua script. See also [Error Handling in Application Code](https://www.lua.org/pil/24.3.1.html).
254-
255261
### Record Split
256262

257263
The Lua callback function can return an array of tables (i.e., array of records) in its third _record_ return value. With this feature, the Lua filter can split one input record into multiple records according to custom logic.
@@ -331,7 +337,7 @@ See also [Fluent Bit: PR 811](https://github.com/fluent/fluent-bit/pull/811).
331337

332338
### Response code filtering
333339

334-
In this example, we want to filter istio logs to exclude lines with response codes between 1 and 399.
340+
In this example, we want to filter Istio logs to exclude lines with response codes between 1 and 399.
335341
Istio is configured to write the logs in json format.
336342

337343
#### Lua script
@@ -353,7 +359,7 @@ end
353359

354360
#### Configuration
355361

356-
Configuration to get istio logs and apply response code filter to them.
362+
Configuration to get Istio logs and apply response code filter to them.
357363

358364
{% tabs %}
359365
{% tab title="fluent-bit.conf" %}
@@ -439,8 +445,7 @@ pipeline:
439445

440446
In the output only the messages with response code 0 or greater than 399 are shown.
441447

442-
443-
### Timeformat Conversion
448+
### Time format Conversion
444449

445450
The following example converts a field's specific type of `datetime` format to
446451
`utc ISO 8601` format.
@@ -568,4 +573,56 @@ The output of this process shows the conversion of the `datetime` of two timezon
568573
[0] event_category_a: [[1722452186.727104902, {}], {"event"=>"Restock", "pub_date"=>"2024-07-30T18:01:06Z"}]
569574
[0] event_category_b: [[1722452186.730255842, {}], {"event"=>"Soldout", "pub_date"=>"2024-07-29T04:15:00Z"}]
570575
...
571-
```
576+
```
577+
578+
### Using configuration variables
579+
580+
Fluent Bit supports definition of configuration variables, which can be done in the following way:
581+
582+
```yaml
583+
env:
584+
myvar1: myvalue1
585+
```
586+
587+
These variables can be accessed from the Lua code by referring to the FLB_ENV Lua table.
588+
Being this a Lua table, the subrecords can be accessed following the same syntax, i.e. `FLB_ENV['A']`.
589+
590+
#### Configuration
591+
592+
```yaml
593+
env:
594+
A: aaa
595+
B: bbb
596+
C: ccc
597+
598+
service:
599+
flush: 1
600+
log_level: info
601+
602+
pipeline:
603+
inputs:
604+
- name: random
605+
tag: test
606+
samples: 10
607+
608+
filters:
609+
- name: lua
610+
match: "*"
611+
call: append_tag
612+
code: |
613+
function append_tag(tag, timestamp, record)
614+
new_record = record
615+
new_record["my_env"] = FLB_ENV
616+
return 1, timestamp, new_record
617+
end
618+
619+
outputs:
620+
- name: stdout
621+
match: "*"
622+
```
623+
624+
#### Output
625+
626+
```
627+
test: [[1731990257.781970977, {}], {"my_env"=>{"A"=>"aaa", "C"=>"ccc", "HOSTNAME"=>"monox-2.lan", "B"=>"bbb"}, "rand_value"=>4805047635809401856}]
628+
```

0 commit comments

Comments
 (0)