summaryrefslogtreecommitdiffstats
path: root/fluent-bit/examples/filter_wasm_go
diff options
context:
space:
mode:
Diffstat (limited to 'fluent-bit/examples/filter_wasm_go')
-rw-r--r--fluent-bit/examples/filter_wasm_go/README.md63
-rw-r--r--fluent-bit/examples/filter_wasm_go/filter.go41
-rw-r--r--fluent-bit/examples/filter_wasm_go/go.mod9
-rw-r--r--fluent-bit/examples/filter_wasm_go/go.sum18
4 files changed, 131 insertions, 0 deletions
diff --git a/fluent-bit/examples/filter_wasm_go/README.md b/fluent-bit/examples/filter_wasm_go/README.md
new file mode 100644
index 000000000..863cf8360
--- /dev/null
+++ b/fluent-bit/examples/filter_wasm_go/README.md
@@ -0,0 +1,63 @@
+# Fluent Bit / filter_wasm_go
+
+This source source tree provides an example of WASM filter program with WASI mode.
+
+## Prerequisites
+
+Tested on
+
+* TinyGo
+ * [tinygo](https://tinygo.org/) tinygo version 0.23.0 linux/amd64 (using go version go1.18.2 and LLVM version 14.0.0)
+ * [tinygo](https://tinygo.org/) tinygo version 0.24.0 linux/amd64 (using go version go1.18.2 and LLVM version 14.0.0)
+
+For Ubuntu, it's easy to install with:
+
+```console
+$ wget https://github.com/tinygo-org/tinygo/releases/download/v0.24.0/tinygo_0.24.0_amd64.deb
+$ sudo dpkg -i tinygo_0.24.0_amd64.deb
+```
+
+## How to build
+
+Execute _tinygo build_ as follows:
+
+```console
+$ tinygo build -wasm-abi=generic -target=wasi -o filter.wasm filter.go
+```
+
+Finally, under the same directory, `*.wasm` file will be created:
+
+```console
+$ ls *.wasm
+filter.wasm
+```
+
+## How to confirm WASM filter integration
+
+Create fluent-bit configuration file as follows:
+
+```ini
+[SERVICE]
+ Flush 1
+ Daemon Off
+ Log_Level info
+ HTTP_Server Off
+ HTTP_Listen 0.0.0.0
+ HTTP_Port 2020
+
+[INPUT]
+ Name dummy
+ Tag dummy.local
+
+[FILTER]
+ Name wasm
+ match dummy.*
+ WASM_Path /path/to/filter.wasm
+ Function_Name go_filter
+ accessible_paths .,/path/to/fluent-bit
+
+[OUTPUT]
+ Name stdout
+ Match *
+
+```
diff --git a/fluent-bit/examples/filter_wasm_go/filter.go b/fluent-bit/examples/filter_wasm_go/filter.go
new file mode 100644
index 000000000..fe7303c68
--- /dev/null
+++ b/fluent-bit/examples/filter_wasm_go/filter.go
@@ -0,0 +1,41 @@
+package main
+
+import (
+ "fmt"
+ "time"
+ "unsafe"
+
+ "github.com/valyala/fastjson"
+)
+
+//export go_filter
+func go_filter(tag *uint8, tag_len uint, time_sec uint, time_nsec uint, record *uint8, record_len uint) *uint8 {
+ btag := unsafe.Slice(tag, tag_len) // Note, requires Go 1.17 (tinygo 0.20)
+ brecord := unsafe.Slice(record, record_len)
+ now := time.Unix(int64(time_sec), int64(time_nsec))
+
+ br := string(brecord)
+ var p fastjson.Parser
+ value, err := p.Parse(br)
+ if err != nil {
+ fmt.Println(err)
+ return nil
+ }
+ obj, err := value.Object()
+ if err != nil {
+ fmt.Println(err)
+ return nil
+ }
+
+ var arena fastjson.Arena
+ obj.Set("time", arena.NewString(now.String()))
+ obj.Set("tag", arena.NewString(string(btag)))
+ obj.Set("original", arena.NewString(br))
+ s := obj.String()
+ s += string(rune(0)) // Note: explicit null terminator.
+ rv := []byte(s)
+
+ return &rv[0]
+}
+
+func main() {}
diff --git a/fluent-bit/examples/filter_wasm_go/go.mod b/fluent-bit/examples/filter_wasm_go/go.mod
new file mode 100644
index 000000000..8250a72d2
--- /dev/null
+++ b/fluent-bit/examples/filter_wasm_go/go.mod
@@ -0,0 +1,9 @@
+module filter_wasm_go
+
+go 1.18
+
+require (
+ github.com/valyala/fastjson v1.6.3 // indirect
+ github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
+ github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
+)
diff --git a/fluent-bit/examples/filter_wasm_go/go.sum b/fluent-bit/examples/filter_wasm_go/go.sum
new file mode 100644
index 000000000..2489b8d54
--- /dev/null
+++ b/fluent-bit/examples/filter_wasm_go/go.sum
@@ -0,0 +1,18 @@
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/goccy/go-json v0.9.10 h1:hCeNmprSNLB8B8vQKWl6DpuH0t60oEs+TAk9a7CScKc=
+github.com/goccy/go-json v0.9.10/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
+github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
+github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
+github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
+github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/valyala/fastjson v1.6.3 h1:tAKFnnwmeMGPbwJ7IwxcTPCNr3uIzoIj3/Fh90ra4xc=
+github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
+github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
+github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
+github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
+github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=