Skip to content

Commit a7bfaab

Browse files
Merge pull request #1661 from fluent/alexakreizinger/sc-123165/update-development-wasm-filter-plugins-doc
2 parents d26c1dc + 2faa6a0 commit a7bfaab

File tree

3 files changed

+51
-54
lines changed

3 files changed

+51
-54
lines changed

development/wasm-filter-plugins.md

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,48 @@
1-
# WASM Filter Plugins
1+
# Wasm filter plugins
22

3-
[WebAssembly](https://webassembly.org/) is binary instruction format for stack based virtual machine.
3+
[WebAssembly](https://webassembly.org/) (Wasm) is a binary instruction format for stack-based virtual machines.
44

5-
Fluent Bit currently supports integration of wasm plugins built as wasm/wasi objects for input and filter plugins only.
6-
The interface for the WASM filter plugins is currently under development but is functional.
5+
Fluent Bit supports integration of Wasm plugins built as Wasm/WASI objects for input and filter plugins only. The interface for Wasm filter plugins is currently under development but is functional.
76

87
## Prerequisites
98

10-
### Building Fluent Bit
9+
### Build Fluent Bit
1110

12-
There are no additional requirements to execute WASM plugins.
11+
There are no additional requirements to execute Wasm plugins.
1312

14-
#### Building flb-wamrc (optional)
13+
#### Build `flb-wamrc` (optional)
1514

16-
`flb-wamrc` is just `flb-` prefixed AOT (Ahead Of Time) compiler that is provided from [wasm-micro-runtime](https://github.com/bytecodealliance/wasm-micro-runtime).
15+
`flb-wamrc` is a `flb`-prefixed AOT (ahead of time) compiler that's provided from [`wasm-micro-runtime`](https://github.com/bytecodealliance/wasm-micro-runtime).
1716

18-
For `flb-wamrc` support, users have to install llvm infrastructure and some additional libraries (`libmlir`, `libPolly`, `libedit`, and `libpfm`), e.g:
17+
For `flb-wamrc` support, you must install the LLVM infrastructure and some additional libraries (`libmlir`, `libPolly`, `libedit`, and `libpfm`). For example:
1918

2019
```text
2120
# apt install -y llvm libmlir-14-dev libclang-common-14-dev libedit-dev libpfm4-dev
2221
```
2322

24-
### For Build WASM programs
23+
### For Wasm programs
2524

26-
Currently, Fluent Bit supports the following WASM toolchains:
25+
Currently, Fluent Bit supports the following Wasm tool chains:
2726

28-
* Rust on `wasm32-unknown-unknown`.
27+
* Rust on `wasm32-unknown-unknown`
2928
* rustc 1.62.1 (e092d0b6b 2022-07-16) or later
30-
* [TinyGo](https://github.com/tinygo-org/tinygo) on wasm32-wasi
29+
* [TinyGo](https://github.com/tinygo-org/tinygo) on `wasm32-wasi`
3130
* v0.24.0 or later
3231
* [WASI SDK](https://github.com/WebAssembly/wasi-sdk) 13 or later.
3332

34-
## Getting Started
33+
## Get started
3534

36-
As described in general options in [source installation](../installation/sources/build-and-install.md),
37-
WASM support is enabled by default.
38-
Compile Fluent Bit with WASM support, e.g:
35+
As described in the general options in the [source installation](../installation/sources/build-and-install.md) guide, Wasm support is enabled by default. Compile Fluent Bit with Wasm support, for example:
3936

4037
```text
4138
$ cd build/
4239
$ cmake .. [-DFLB_WAMRC=On]
4340
$ make
4441
```
4542

46-
To support AOT compiled WASM execution as filter plugins, users have to built Fluent Bit with `-DFLB_WAMRC=On`.
43+
To support AOT-compiled Wasm execution as filter plugins, build Fluent Bit with `-DFLB_WAMRC=On`.
4744

48-
Once compiled, we can see new plugins in which handles _wasm_, e.g:
45+
Once compiled, you can see new plugins that handle `wasm`, for example:
4946

5047
```text
5148
$ bin/fluent-bit -h
@@ -59,31 +56,30 @@ Filters
5956
wasm WASM program filter
6057
```
6158

62-
## Build a WASM Filter for Filter Plugin
59+
## Build a Wasm filter for filter plugin
6360

64-
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.
61+
The Fluent Bit Wasm filter assumes C ABI, also known as `wasm32-unknown-unknown` on Rust target and `wasm32-wasi` on TinyGo target.
6562

6663
### To Install Additional Components
6764

68-
TinyGo and WASI SDK support wasm target by default.
69-
When using Rust's `wasm32-unknown-unknown` target, users must install `wasm32-unknown-unknown` via [rustup](https://rustup.rs/). Then, installing that target components as:
65+
TinyGo and WASI SDK support Wasm target by default. When using Rust's `wasm32-unknown-unknown` target, you must install `wasm32-unknown-unknown` by using [rustup](https://rustup.rs/). Then, install the target components as follows:
7066

7167
```text
7268
$ rustup target add wasm32-unknown-unknown
7369
```
7470

75-
### Requirements of WASM programs
71+
### Requirements of Wasm programs
7672

77-
WASM filter plugins execute the function that has the following signagure.
73+
Wasm filter plugins execute the function that has the following signature.
7874

7975
For C:
8076

8177
```text
82-
// We can use an arbitrary function name for filter operations w/ WASM.
78+
// We can use an arbitrary function name for filter operations w/ Wasm.
8379
char* c_filter(char*, int, uint32_t, uint32_t, char*, int);
8480
```
8581

86-
For Go(TinyGo):
82+
For Go (TinyGo):
8783

8884
```text
8985
//export go_filter
@@ -95,14 +91,14 @@ For Rust:
9591

9692
```text
9793
// #[no_mangle] attribute is needed for preventing mangles and align C ABI.
98-
// Also we can use an arbitrary function name for filter operations w/ WASM.
94+
// Also we can use an arbitrary function name for filter operations w/ Wasm.
9995
#[no_mangle]
10096
pub extern "C" fn rust_filter(tag: *const c_char, tag_len: u32, time_sec: u32, time_nsec: u32, record: *const c_char, record_len: u32)
10197
```
10298

103-
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.
99+
The `//export XXX` attribute on TinyGo and `#[no_mangle]` attribute on Rust are required. This is because TinyGo and Rust will mangle their function names if they aren't specified.
104100

105-
Once built, a WASM program will be available. Then, that built program can be executed with the following Fluent Bit configuration:
101+
Once built, a Wasm program will be available. Then you can execute that built program with the following Fluent Bit configuration:
106102

107103
```text
108104
[INPUT]
@@ -121,14 +117,14 @@ Once built, a WASM program will be available. Then, that built program can be ex
121117
Match *
122118
```
123119

124-
For example, one of the examples of [Rust WASM filter](https://github.com/fluent/fluent-bit/tree/master/examples/filter_rust) should generate its filtered logs as follows:
120+
For example, one of the sample [Rust Wasm filters](https://github.com/fluent/fluent-bit/tree/master/examples/filter_rust) should generate its filtered logs as follows:
125121

126122
```text
127123
[0] dummy.local: [1666270588.271704000, {"lang"=>"Rust", "message"=>"dummy", "original"=>"{"message":"dummy"}", "tag"=>"dummy.local", "time"=>"2022-10-20T12:56:28.271704000 +0000"}]
128124
[0] dummy.local: [1666270589.270348000, {"lang"=>"Rust", "message"=>"dummy", "original"=>"{"message":"dummy"}", "tag"=>"dummy.local", "time"=>"2022-10-20T12:56:29.270348000 +0000"}]
129125
[0] dummy.local: [1666270590.271107000, {"lang"=>"Rust", "message"=>"dummy", "original"=>"{"message":"dummy"}", "tag"=>"dummy.local", "time"=>"2022-10-20T12:56:30.271107000 +0000"}]
130126
```
131-
Another example of a Rust WASM filter is the [flb_filter_iis](https://github.com/kenriortega/flb_filter_iis) filter.
127+
Another example of a Rust Wasm filter is the [flb_filter_iis](https://github.com/kenriortega/flb_filter_iis) filter.
132128
This filter takes the [Internet Information Services (IIS)](https://learn.microsoft.com/en-us/iis/manage/provisioning-and-managing-iis/configure-logging-in-iis) [w3c logs](https://learn.microsoft.com/en-us/iis/manage/provisioning-and-managing-iis/configure-logging-in-iis#select-w3c-fields-to-log) (with some custom modifications) and transforms the raw string into a standard Fluent Bit JSON structured record.
133129

134130
```text
@@ -158,32 +154,33 @@ The output after the filter logic will be:
158154
```text
159155
[0] iis.*: [[1692131925.559486675, {}], {"c_authorization_header"=>"-", "c_ip"=>"::1", "cs_bytes"=>756, "cs_cookie"=>"-", "cs_host"=>"localhost", "cs_method"=>"GET", "cs_referer"=>"-", "cs_uri_query"=>"-", "cs_uri_stem"=>"/", "cs_user_agent"=>"Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/115.0.0.0+Safari/537.36+Edg/115.0.1901.200", "date"=>"2023-08-11 19:56:44", "s_computername"=>"WIN-PC1", "s_ip"=>"::1", "s_port"=>"80", "s_sitename"=>"W3SVC1", "sc_bytes"=>142, "sc_status"=>"304", "source"=>"LogEntryIIS", "tag"=>"iis.*", "time"=>"2023-08-15T20:38:45.559486675 +0000", "time_taken"=>1078}]
160156
```
161-
This filter approach provides us with several powerful advantages inherent to programming languages.
162-
For instance, it:
163-
- 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.
164-
- 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".
165-
- Can be used to define a `allow/deny` list using a data structure array or a file to store predefined IP addresses.
166-
- Makes it possible to call an external resource such as an API or database to enhance our data.
167-
- Allows all methods to be thoroughly tested and shared as a binary bundle or library.
168-
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.
169157

170-
### Optimize execution of WASM programs
158+
This filter approach offers several advantages inherent to programming languages. For example:
159+
- It can be extended by adding type conversion to fields, such as `sc_bytes`, `cs_bytes`, and `time_taken`. You can use this to validate data results.
160+
- It allows for the use of conditions to apply more descriptive filters. For example, you can get only all logs that contain status codes higher than `4xx` or `5xx`.
161+
- It can be used to define allow lists and deny lists using a data structure array or a file to store predefined IP addresses.
162+
- It makes it possible to call an external resource such as an API or database to enhance your data.
163+
- It allows all methods to be thoroughly tested and shared as a binary bundle or library.
171164

172-
To optimize WASM program execution, there is the option of using `flb-wamrc`.
173-
`flb-wamrc` will reduce runtime footprint and to be best performance for filtering operations.
174-
This tool will be built when `-DFLB_WAMRC=On` cmake option is specified and llvm infrastructure is installed on the building box.
165+
These examples can be applied in a demo and can serve as an ideal starting point to create more complex logic, depending on your requirements.
166+
167+
### Optimize execution of Wasm programs
168+
169+
To optimize Wasm program execution, there is the option of using `flb-wamrc`. This option reduces your runtime footprint and to be best performance for filtering operations.
170+
171+
This tool will be built when the `-DFLB_WAMRC=On` CMake option is specified and LLVM infrastructure is installed on the building box.
175172

176173
```shell
177174
$ flb-wamrc -o /path/to/built_wasm.aot /path/to/built_wasm.wasm
178175
```
179176

180-
For further optimizations to the specific CPU such as Cortex-A57 series, e.g:
177+
For further optimizations to the specific CPU, such as Cortex-A57 series:
181178

182179
```text
183180
$ flb-wamrc --size-level=3 --target=aarch64v8 --cpu=cortex-a57 -o /path/to/built_wasm.aot /path/to/built_wasm.wasm
184181
```
185182

186-
Then, when AOT (Ahead Of Time) compiling is succeeded:
183+
Then, when AOT (Ahead Of Time) compiling has succeeded:
187184

188185
```text
189186
Create AoT compiler with:
@@ -196,13 +193,10 @@ Create AoT compiler with:
196193
Compile success, file /path/to/built_wasm.aot was generated.
197194
```
198195

199-
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.
196+
AOT compiling should generate CPU architecture-dependent objects. If you want to use an AOT compiled object on different architecture, it must align the `target` and `target cpu` for actual environments.
200197

201-
### Further Concrete Examples
198+
### Further examples
202199

203-
* C Filter
204-
* https://github.com/fluent/fluent-bit/tree/master/examples/filter_wasm_c
205-
* Rust Filter
206-
* https://github.com/fluent/fluent-bit/tree/master/examples/filter_rust
207-
* TinyGo Filter
208-
* https://github.com/fluent/fluent-bit/tree/master/examples/filter_wasm_go
200+
* [C filter](https://github.com/fluent/fluent-bit/tree/master/examples/filter_wasm_c)
201+
* [Rust Filter](https://github.com/fluent/fluent-bit/tree/master/examples/filter_rust)
202+
* [TinyGo filter](https://github.com/fluent/fluent-bit/tree/master/examples/filter_wasm_go)

vale-styles/FluentBit/Acronyms.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ exceptions:
4949
- JSX
5050
- LESS
5151
- LLDB
52+
- LLVM
5253
- LTS
5354
- NET
5455
- NOTE
@@ -89,6 +90,7 @@ exceptions:
8990
- UTC
9091
- UTF
9192
- UUID
93+
- WASI
9294
- XML
9395
- XSS
9496
- YAML

vale-styles/FluentBit/Spelling-exceptions.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ VictoriaMetrics
192192
VictorOps
193193
Vivo
194194
VMs
195+
Wasm
195196
Wavefront
196197
Worldmap
197198
Yocto

0 commit comments

Comments
 (0)