Lua

The Lua filter lets you modify incoming records (or split one record into multiple records) using custom Lua scripts.

A Lua-based filter requires two steps:

  1. Configure the filter in the main configuration.

  2. Prepare a Lua script for the filter to use.

Configuration parameters

The plugin supports the following configuration parameters:

Key
Description

script

Path to the Lua script that will be used. This can be a relative path against the main configuration file.

call

The Lua function name that will be triggered to do filtering. It's assumed that the function is declared inside the script parameter.

type_int_key

If these keys are matched, the fields are converted to integers. If more than one key, delimit by space.

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.

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.

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's 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.

code

Inline Lua code instead of loading from a path defined in script.

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.

Get started

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 input plugin for data ingestion, invoke Lua filter using the test.lua script, and call the cb_print() function, which only prints the same information to the standard output.

Command line

From the command line you can use the following options:

Configuration file

In your main configuration file, append the following Input, Filter, and Output sections:

Lua script filter API

The life cycle of a filter has the following steps:

  1. Upon tag matching by this filter, it might process or bypass the record.

  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.

  3. It invokes the Lua function and passes each record in JSON format.

  4. Upon return, it validates the return value and continues the pipeline.

Callback prototype

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

Function arguments

Name
Description

tag

Name of the tag associated with the incoming record.

timestamp

Unix timestamp with nanoseconds associated with the incoming record. The original format is a double (seconds.nanoseconds).

record

Lua table with the record content.

Return values

Each callback must return three values:

Name
Data type
Description

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).

timestamp

double

If code equals 1, the original record timestamp will be replaced with this new value.

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.

Lua extended callback with groups and metadata support

This feature requires Fluent Bit version 4.0.4 or later.

For more advanced use cases, especially when working with structured formats like OpenTelemetry logs, Fluent Bit supports an extended callback prototype that provides access to group metadata and record metadata.

Extended function signature

Extended function arguments

Name
Description

tag

Name of the tag associated with the incoming record.

timestamp

Unix timestamp with nanoseconds associated with the incoming record.

group

A read-only table containing group-level metadata (for example, OpenTelemetry resource or scope info). This will be an empty table if the log isn't part of a group.

metadata

A table representing the record-specific metadata. You can modify this if needed.

record

Lua table with the record content.

Extended return values

Each extended callback must return four values:

Name
Data type
Description

code

integer

The code return value: -1 (drop record), 0 (no modification), or 1 (record was modified).

timestamp

double

The updated timestamp.

metadata

table

A new or modified metadata table.

record

table

A new or modified log record. This can be an array of tables for splitting records.

Function signature detection

At load time, the Lua filter automatically detects which callback prototype to use based on the number of parameters:

  • Three arguments: Uses the classic mode (tag, timestamp, record)

  • Five arguments: Uses the metadata-aware mode (tag, timestamp, group, metadata, record)

This ensures backward compatibility with existing Lua scripts.

Multiple records with metadata

When using the extended prototype, you can return multiple records with their respective metadata:

The metadata and record arrays must have the same length.

OpenTelemetry example

This example demonstrates processing OpenTelemetry logs with group metadata access:

Configuration [#configuration-otel]

Input JSON

Group metadata is read-only and should not be modified. If you don't need group or metadata support, you can continue using the three-argument prototype.

Features

Inline configuration

The Fluent Bit smoke tests include examples to verify during CI.

Number type

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.

Protected mode

Fluent Bit supports protected mode to prevent crashes if it executes an invalid Lua script. See Error Handling in Application Code in the Lua documentation for more information.

Code examples

For functional examples of this interface, refer to the code samples provided in the source code of the project.

Processing environment variables

As an example that combines Lua processing with the Kubernetes filter that demonstrates using environment variables with Lua regular expressions and substitutions.

Kubernetes pods generally have various environment variables set by the infrastructure automatically, which can contain valuable information.

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.

The goal of this example is to extract the sandboxbsh name and add it to the record as a special key.

filters.lua:

Record split

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.

For example:

Lua script [#lua-record-split]

Configuration [#configuration-record-split]

Input [#input-record-split]

Output [#output-record-split]

See also Fluent Bit: PR 811.

Response code filtering

This example filters Istio logs to exclude lines with a response code between 1 and 399. Istio is configured to write logs in JSON format.

Lua script [#lua-response-code]

Script response_code_filter.lua

Configuration [#configuration-response-code]

Configuration to get Istio logs and apply response code filter to them.

Input [#input-response-code]

Output [#output-response-code]

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

Time format conversion

The following example converts a field's specific type of datetime format to the UTC ISO 8601 format.

Lua script [#lua-time-format]

Script custom_datetime_format.lua:

Configuration [#configuration-time-format]

Use this configuration to obtain a JSON key with datetime, and then convert it to another format.

Input [#input-time-format]

and

Which are handled by dummy in this example.

Output [#output-time-format]

The output of this process shows the conversion of the datetime of two timezones to ISO 8601 format in UTC.

Using configuration variables

Fluent Bit supports definition of configuration variables, which can be done in the following way:

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 sub-records through the same syntax (for example, FLB_ENV['A']).

Configuration [#configuration-env-var]

Output

Last updated

Was this helpful?