WebAssembly is binary instruction format for stack based virtual machine.
Fluent Bit currently supports integration of wasm plugins built as wasm/wasi objects for input and filter plugins only. The interface for the WASM filter plugins is currently under development but is functional.
There are no additional requirements to execute WASM plugins.
flb-wamrc
is just flb-
prefixed AOT (Ahead Of Time) compiler that is provided from wasm-micro-runtime.
For flb-wamrc
support, users have to install llvm infrastructure and some additional libraries (libmlir
, libPolly
, libedit
, and libpfm
), e.g:
Currently, Fluent Bit supports the following WASM toolchains:
Rust on wasm32-unknown-unknown
.
rustc 1.62.1 (e092d0b6b 2022-07-16) or later
TinyGo on wasm32-wasi
v0.24.0 or later
WASI SDK 13 or later.
As described in general options in source installation, WASM support is enabled by default. Compile Fluent Bit with WASM support, e.g:
To support AOT compiled WASM execution as filter plugins, users have to built Fluent Bit with -DFLB_WAMRC=On
.
Once compiled, we can see new plugins in which handles wasm, e.g:
Currently, Fluent Bit's WASM filter assumes C ABI that is also known as wasm32-unknown-unknown
on Rust target and wasm32-wasi
on TinyGo target.
TinyGo and WASI SDK support wasm target by default. When using Rust's wasm32-unknown-unknown
target, users must install wasm32-unknown-unknown
via rustup. Then, installing that target components as:
WASM filter plugins execute the function that has the following signagure.
For C:
For Go(TinyGo):
For Rust:
Note that //export XXX
on TinyGo and #[no_mangle]
attributes on Rust are required. This is because TinyGo and Rust will mangle their function names if they are not specified.
Once built, a WASM program will be available. Then, that built program can be executed with the following Fluent Bit configuration:
For example, one of the examples of Rust WASM filter should generate its filtered logs as follows:
Another example of a Rust WASM filter is the flb_filter_iis filter. This filter takes the Internet Information Services (IIS) w3c logs (with some custom modifications) and transforms the raw string into a standard Fluent Bit JSON structured record.
The incoming raw strings from an IIS log are composed of the following fields:
date time s-sitename s-computername s-ip cs-method cs-uri-stem cs-uri-query s-port c-ip cs(User-Agent) cs(Cookie) cs(Referer) cs-host sc-status sc-bytes cs-bytes time-taken c-authorization-header
The output after the filter logic will be:
This filter approach provides us with several powerful advantages inherent to programming languages. For instance, it:
Can be extended by adding type conversion to fields such as sc_bytes, cs_bytes, time_taken
. This is particularly useful when we need to validate our data results.
Allows for the use of conditions to apply more descriptive filters, for example, "get only all logs that contain status codes above 4xx or 5xx".
Can be used to define a allow/deny
list using a data structure array or a file to store predefined IP addresses.
Makes it possible to call an external resource such as an API or database to enhance our data.
Allows all methods to be thoroughly tested and shared as a binary bundle or library. These examples can be applied in our demo and can serve as an ideal starting point to create more complex logic, depending on our requirements.
To optimize WASM program execution, there is the option of using flb-wamrc
. flb-wamrc
will reduce runtime footprint and to be best performance for filtering operations. This tool will be built when -DFLB_WAMRC=On
cmake option is specified and llvm infrastructure is installed on the building box.
For further optimizations to the specific CPU such as Cortex-A57 series, e.g:
Then, when AOT (Ahead Of Time) compiling is succeeded:
Note that AOT compiling should generate CPU architecture-dependent objects. If users want to use AOT compiled object on the different archtecture, it must align the target and target cpu for actual environments.
C Filter
https://github.com/fluent/fluent-bit/tree/master/examples/filter_wasm_c
Rust Filter
https://github.com/fluent/fluent-bit/tree/master/examples/filter_rust
TinyGo Filter
https://github.com/fluent/fluent-bit/tree/master/examples/filter_wasm_go