You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pipeline/filters/lua.md
+61-73Lines changed: 61 additions & 73 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,44 +1,44 @@
1
1
# Lua
2
2
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.
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:
8
8
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
11
11
12
-
## Configuration Parameters <aid="config"></a>
12
+
## Configuration parameters
13
13
14
14
The plugin supports the following configuration parameters:
15
15
16
16
| 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 keyvalue 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`. |
26
26
27
-
## Getting Started <aid="getting_started"></a>
27
+
## Get started
28
28
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.
30
30
31
-
### Command Line
31
+
### Command line
32
32
33
33
From the command line you can use the following options:
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:
42
42
43
43
{% tabs %}
44
44
{% tab title="fluent-bit.conf" %}
@@ -76,16 +76,16 @@ pipeline:
76
76
{% endtabs %}
77
77
78
78
79
-
## Lua Script Filter API <a id="lua_script"></a>
79
+
## Lua script filter API
80
80
81
-
The life cycle of a filter have the following steps:
81
+
The life cycle of a filter has the following steps:
82
82
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.
87
87
88
-
## Callback Prototype
88
+
## Callback prototype
89
89
90
90
The Lua script can have one or multiple callbacks that can be used by this filter. The function prototype is as follows:
91
91
@@ -96,23 +96,23 @@ function cb_print(tag, timestamp, record)
96
96
end
97
97
```
98
98
99
-
#### Function Arguments
99
+
#### Function arguments
100
100
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. |
106
106
107
-
#### Return Values
107
+
#### Return values
108
108
109
-
Each callback **must** return three values:
109
+
Each callback must return three values:
110
110
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. |
116
116
117
117
## Features
118
118
@@ -176,36 +176,29 @@ pipeline:
176
176
{% endtab %}
177
177
{% endtabs %}
178
178
179
-
### Number Type
179
+
### Number type
180
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.
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.
184
182
185
-
### Protected Mode
183
+
### Protected mode
186
184
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.
190
186
187
+
## Code examples
191
188
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:
196
190
197
191
### Processing environment variables
198
192
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.
200
194
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.
202
196
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.
The environment variable is set as `KUBERNETES_SERVICE_HOST: api.sandboxbsh-a.project.domain.com`.
207
200
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.
209
202
210
203
{% tabs %}
211
204
{% tab title="fluent-bit.conf" %}
@@ -258,9 +251,9 @@ filters.lua:
258
251
end
259
252
```
260
253
261
-
### Record Split
254
+
### Record split
262
255
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.
264
257
265
258
For example:
266
259
@@ -337,8 +330,7 @@ See also [Fluent Bit: PR 811](https://github.com/fluent/fluent-bit/pull/811).
337
330
338
331
### Response code filtering
339
332
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.
342
334
343
335
#### Lua script
344
336
@@ -443,16 +435,15 @@ pipeline:
443
435
444
436
#### Output
445
437
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.
447
439
448
-
### Time format Conversion
440
+
### Time format conversion
449
441
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.
452
443
453
444
#### Lua script
454
445
455
-
Script `custom_datetime_format.lua`
446
+
Script `custom_datetime_format.lua`:
456
447
457
448
```lua
458
449
functionconvert_to_utc(tag, timestamp, record)
@@ -478,8 +469,7 @@ end
478
469
479
470
#### Configuration
480
471
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.
483
473
484
474
{% tabs %}
485
475
{% tab title="fluent-bit.conf" %}
@@ -564,8 +554,7 @@ Which are handled by dummy in this example.
564
554
565
555
#### Output
566
556
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.
569
558
570
559
```ini
571
560
...
@@ -584,8 +573,7 @@ env:
584
573
myvar1: myvalue1
585
574
```
586
575
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']`).
0 commit comments