Skip to content

Commit 21b47f0

Browse files
pipeline: filters: lua: general cleanup
Signed-off-by: Alexa Kreizinger <[email protected]>
1 parent 9342572 commit 21b47f0

File tree

1 file changed

+61
-73
lines changed

1 file changed

+61
-73
lines changed

pipeline/filters/lua.md

Lines changed: 61 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
# Lua
22

3-
The **Lua** filter allows you to modify the incoming records (even split one record into multiple records) using custom [Lua](https://www.lua.org/) scripts.
3+
The _Lua_ filter lets you modify incoming records (or split one record into multiple records) using custom [Lua](https://www.lua.org/) scripts.
44

55
<img referrerpolicy="no-referrer-when-downgrade" src="https://static.scarf.sh/a.png?x-pxid=f519378e-536c-4b25-8949-ee6ed8d8d6c1" />
66

7-
Due to the necessity to have a flexible filtering mechanism, it is now possible to extend Fluent Bit capabilities by writing custom filters using Lua programming language. A Lua-based filter takes two steps:
7+
A Lua-based filter requires two steps:
88

9-
1. Configure the Filter in the main configuration
10-
2. Prepare a Lua script that will be used by the Filter
9+
1. Configure the filter in the main configuration
10+
2. Prepare a Lua script for the filter to use
1111

12-
## Configuration Parameters <a id="config"></a>
12+
## Configuration parameters
1313

1414
The plugin supports the following configuration parameters:
1515

1616
| Key | Description |
17-
| :--- | :--- |
18-
| script | Path to the Lua script that will be used. This can be a relative path against the main configuration file. |
19-
| call | Lua function name that will be triggered to do filtering. It's assumed that the function is declared inside the **script** parameter defined above. |
20-
| type\_int\_key | If these keys are matched, the fields are converted to integer. If more than one key, delimit by space. Note that starting from Fluent Bit v1.6 integer data types are preserved and not converted to double as in previous versions. |
21-
| type\_array\_key| If these keys are matched, the fields are handled as array. If more than one key, delimit by space. It is useful the array can be empty. |
22-
| protected\_mode | If enabled, Lua script will be executed in protected mode. It prevents Fluent Bit from crashing when invalid Lua script is executed or the triggered Lua function throws exceptions. Default is true. |
23-
| time\_as\_table | By default when the Lua script is invoked, the record timestamp is passed as a *floating number* which might lead to precision loss when it is converted back. If you desire timestamp precision, enabling this option will pass the timestamp as a Lua table with keys `sec` for seconds since epoch and `nsec` for nanoseconds. |
24-
| code | Inline LUA code instead of loading from a path via `script`. |
25-
| enable_flb_null| If enabled, null will be converted to flb_null in Lua. It is useful to prevent removing key/value since nil is a special value to remove key value from map in Lua. Default is false. |
17+
| --- | ----------- |
18+
| `script` | Path to the Lua script that will be used. This can be a relative path against the main configuration file. |
19+
| `call` | The Lua function name that will be triggered to do filtering. It's assumed that the function is declared inside the `script` parameter. |
20+
| `type_int_key` | If these keys are matched, the fields are converted to integers. If more than one key, delimit by space. |
21+
| `type_array_key` | If these keys are matched, the fields are handled as array. If more than one key, delimit by space. The array can be empty. |
22+
| `protected_mode` | If enabled, the Lua script will be executed in protected mode. It prevents Fluent Bit from crashing when an invalid Lua script is executed or the triggered Lua function throws exceptions. Default value: `true`. |
23+
| `time_as_table` | By default, when the Lua script is invoked, the record timestamp is passed as a floating number, which might lead to precision loss when it is converted back. If you need timestamp precision, enabling this option will pass the timestamp as a Lua table with keys `sec` for seconds since epoch and `nsec` for nanoseconds. |
24+
| `code` | Inline Lua code instead of loading from a path defined in `script`. |
25+
| `enable_flb_null` | If enabled, `null` will be converted to `flb_null` in Lua. This helps prevent removing key/value since `nil` is a special value to remove key/value from map in Lua. Default value: `false`. |
2626

27-
## Getting Started <a id="getting_started"></a>
27+
## Get started
2828

29-
In order to test the filter, you can run the plugin from the command line or through the configuration file. The following examples use the [dummy](../inputs/dummy.md) input plugin for data ingestion, invoke Lua filter using the [test.lua](https://github.com/fluent/fluent-bit/blob/master/scripts/test.lua) script and call the [cb\_print\(\)](https://github.com/fluent/fluent-bit/blob/master/scripts/test.lua#L29) function which only prints the same information to the standard output:
29+
To test the Lua filter, you can run the plugin from the command line or through the configuration file. The following examples use the [dummy](../inputs/dummy.md) input plugin for data ingestion, invoke Lua filter using the [test.lua](https://github.com/fluent/fluent-bit/blob/master/scripts/test.lua) script, and call the [`cb_print()`](https://github.com/fluent/fluent-bit/blob/master/scripts/test.lua#L29) function, which only prints the same information to the standard output.
3030

31-
### Command Line
31+
### Command line
3232

3333
From the command line you can use the following options:
3434

3535
```bash
3636
$ fluent-bit -i dummy -F lua -p script=test.lua -p call=cb_print -m '*' -o null
3737
```
3838

39-
### Configuration File
39+
### Configuration file
4040

41-
In your main configuration file append the following _Input_, _Filter_ & _Output_ sections:
41+
In your main configuration file, append the following `Input`, `Filter` and `Output` sections:
4242

4343
{% tabs %}
4444
{% tab title="fluent-bit.conf" %}
@@ -76,16 +76,16 @@ pipeline:
7676
{% endtabs %}
7777
7878
79-
## Lua Script Filter API <a id="lua_script"></a>
79+
## Lua script filter API
8080
81-
The life cycle of a filter have the following steps:
81+
The life cycle of a filter has the following steps:
8282
83-
1. Upon Tag matching by this filter, it may process or bypass the record.
84-
2. If tag matched, it will accept the record and invoke the function defined in the `call` property which basically is the name of a function defined in the Lua `script`.
85-
3. Invoke Lua function and pass each record in JSON format.
86-
4. Upon return, validate return value and continue the pipeline.
83+
1. Upon tag matching by this filter, it might process or bypass the record.
84+
2. If the tag matched, it will accept the record and invoke the function defined in the `call` property, which is the name of a function defined in the Lua `script`.
85+
3. It invokes the Lua function and passes each record in JSON format.
86+
4. Upon return, it validates the return value and continues the pipeline.
8787

88-
## Callback Prototype
88+
## Callback prototype
8989

9090
The Lua script can have one or multiple callbacks that can be used by this filter. The function prototype is as follows:
9191

@@ -96,23 +96,23 @@ function cb_print(tag, timestamp, record)
9696
end
9797
```
9898

99-
#### Function Arguments
99+
#### Function arguments
100100

101-
| name | description |
102-
| :--- | :--- |
103-
| tag | Name of the tag associated with the incoming record. |
104-
| timestamp | Unix timestamp with nanoseconds associated with the incoming record. The original format is a double (seconds.nanoseconds) |
105-
| record | Lua table with the record content |
101+
| Name | Description |
102+
| ---- | ----------- |
103+
| `tag` | Name of the tag associated with the incoming record. |
104+
| `timestamp` | Unix timestamp with nanoseconds associated with the incoming record. The original format is a double (`seconds.nanoseconds`). |
105+
| `record` | Lua table with the record content. |
106106

107-
#### Return Values
107+
#### Return values
108108

109-
Each callback **must** return three values:
109+
Each callback must return three values:
110110

111-
| name | data type | description |
112-
| :--- | :--- | :--- |
113-
| code | integer | The code return value represents the result and further action that may follows. If _code_ equals -1, means that the record will be dropped. If _code_ equals 0, the record will not be modified, otherwise if _code_ equals 1, means the original timestamp and record have been modified so it must be replaced by the returned values from _timestamp_ (second return value) and _record_ (third return value). If _code_ equals 2, means the original timestamp is not modified and the record has been modified so it must be replaced by the returned values from _record_ (third return value). The _code_ 2 is supported from v1.4.3. |
114-
| timestamp | double | If code equals 1, the original record timestamp will be replaced with this new value. |
115-
| 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) |
111+
| Name | Data type | Description |
112+
| ---- | --------- | ----------- |
113+
| `code` | integer | The code return value represents the result and further actions that might follow. If `code` equals `-1`, this means that the record will be dropped. If `code` equals `0`, the record won't be modified. Otherwise, if `code` equals `1`, this means the original timestamp and record have been modified, so it must be replaced by the returned values from `timestamp` (second return value) and `record` (third return value). If `code` equals `2`, this means the original timestamp won't be modified and the record has been modified, so it must be replaced by the returned values from `record` (third return value). |
114+
| `timestamp` | double | If `code` equals `1`, the original record timestamp will be replaced with this new value. |
115+
| `record` | table | If `code` equals `1`, the original record information will be replaced with this new value. The `record` value must be a valid Lua table. This value can be an array of tables (for example, an array of objects in JSON format), and in that case the input record is effectively split into multiple records. |
116116

117117
## Features
118118

@@ -176,36 +176,29 @@ pipeline:
176176
{% endtab %}
177177
{% endtabs %}
178178

179-
### Number Type
179+
### Number type
180180

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.
181+
Lua treats numbers as a `double` type, which means an `integer` type containing data like user IDs and log levels will be converted to a `double`. To avoid type conversion, use the `type_int_key` property.
184182

185-
### Protected Mode
183+
### Protected mode
186184

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.
185+
Fluent Bit supports protected mode to prevent crashes if it executes an invalid Lua script. See [Error Handling in Application Code](https://www.lua.org/pil/24.3.1.html) in the Lua documentation for more information.
190186

187+
## Code examples
191188

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-
189+
For functional examples of this interface, refer to the code samples provided in the source code of the project located here:
196190

197191
### Processing environment variables
198192

199-
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.
193+
As an example that combines Lua processing with the [Kubernetes filter](./kubernetes.md) that demonstrates using environment variables with Lua regular expressions and substitutions.
200194

201-
Kubernetes pods generally have various environment variables set by the infrastructure automatically which may contain useful information.
195+
Kubernetes pods generally have various environment variables set by the infrastructure automatically, which can contain valuable information.
202196

203-
In this example, we want to extract part of the Kubernetes cluster API name.
197+
This example extracts part of the Kubernetes cluster API name.
204198

205-
The environment variable is set like so:
206-
`KUBERNETES_SERVICE_HOST: api.sandboxbsh-a.project.domain.com`
199+
The environment variable is set as `KUBERNETES_SERVICE_HOST: api.sandboxbsh-a.project.domain.com`.
207200

208-
We want to extract the `sandboxbsh` name and add it to our record as a special key.
201+
The goal of this example is to extract the `sandboxbsh` name and add it to the record as a special key.
209202

210203
{% tabs %}
211204
{% tab title="fluent-bit.conf" %}
@@ -258,9 +251,9 @@ filters.lua:
258251
end
259252
```
260253

261-
### Record Split
254+
### Record split
262255

263-
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.
256+
The Lua callback function can return an array of tables (for example, an 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.
264257

265258
For example:
266259

@@ -337,8 +330,7 @@ See also [Fluent Bit: PR 811](https://github.com/fluent/fluent-bit/pull/811).
337330

338331
### Response code filtering
339332

340-
In this example, we want to filter Istio logs to exclude lines with response codes between 1 and 399.
341-
Istio is configured to write the logs in json format.
333+
This example filters Istio logs to exclude lines with a response code between `1` and `399`. Istio is confiured to write logs in JSON format.
342334

343335
#### Lua script
344336

@@ -443,16 +435,15 @@ pipeline:
443435

444436
#### Output
445437

446-
In the output only the messages with response code 0 or greater than 399 are shown.
438+
In the output, only the messages with response code `0` or greater than `399` are shown.
447439

448-
### Time format Conversion
440+
### Time format conversion
449441

450-
The following example converts a field's specific type of `datetime` format to
451-
`utc ISO 8601` format.
442+
The following example converts a field's specific type of `datetime` format to the UTC ISO 8601 format.
452443

453444
#### Lua script
454445

455-
Script `custom_datetime_format.lua`
446+
Script `custom_datetime_format.lua`:
456447

457448
```lua
458449
function convert_to_utc(tag, timestamp, record)
@@ -478,8 +469,7 @@ end
478469

479470
#### Configuration
480471

481-
Use this configuration to obtain a JSON key with `datetime`, and then convert it to
482-
another format.
472+
Use this configuration to obtain a JSON key with `datetime`, and then convert it to another format.
483473

484474
{% tabs %}
485475
{% tab title="fluent-bit.conf" %}
@@ -564,8 +554,7 @@ Which are handled by dummy in this example.
564554

565555
#### Output
566556

567-
The output of this process shows the conversion of the `datetime` of two timezones to
568-
`ISO 8601` format in `UTC`.
557+
The output of this process shows the conversion of the `datetime` of two timezones to ISO 8601 format in UTC.
569558

570559
```ini
571560
...
@@ -584,8 +573,7 @@ env:
584573
myvar1: myvalue1
585574
```
586575
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']`.
576+
These variables can be accessed from the Lua code by referring to the `FLB_ENV` Lua table. Since this is a Lua table, you can access its subrecords through the same syntax (for example, `FLB_ENV['A']`).
589577

590578
#### Configuration
591579

@@ -623,6 +611,6 @@ pipeline:
623611

624612
#### Output
625613

626-
```
614+
```shell
627615
test: [[1731990257.781970977, {}], {"my_env"=>{"A"=>"aaa", "C"=>"ccc", "HOSTNAME"=>"monox-2.lan", "B"=>"bbb"}, "rand_value"=>4805047635809401856}]
628616
```

0 commit comments

Comments
 (0)