|
| 1 | +# Fluent Bit and Golang Plugins |
| 2 | + |
| 3 | +Fluent Bit currently supports integration of Golang plugins built as shared |
| 4 | +objects for output plugins only. The interface for the Golang plugins is |
| 5 | +currently under development but is functional. |
| 6 | + |
| 7 | +## Getting Started |
| 8 | + |
| 9 | +Compile Fluent Bit with Golang support, e.g: |
| 10 | + |
| 11 | +``` |
| 12 | +$ cd build/ |
| 13 | +$ cmake -DFLB_DEBUG=On -DFLB_PROXY_GO=On ../ |
| 14 | +$ make |
| 15 | +``` |
| 16 | + |
| 17 | +Once compiled, we can see a new option in the binary `-e` which stands for |
| 18 | +_external plugin_, e.g: |
| 19 | + |
| 20 | +``` |
| 21 | +$ bin/fluent-bit -h |
| 22 | +Usage: fluent-bit [OPTION] |
| 23 | +
|
| 24 | +Available Options |
| 25 | + -c --config=FILE specify an optional configuration file |
| 26 | + -d, --daemon run Fluent Bit in background mode |
| 27 | + -f, --flush=SECONDS flush timeout in seconds (default: 5) |
| 28 | + -i, --input=INPUT set an input |
| 29 | + -m, --match=MATCH set plugin match, same as '-p match=abc' |
| 30 | + -o, --output=OUTPUT set an output |
| 31 | + -p, --prop="A=B" set plugin configuration property |
| 32 | + -e, --plugin=FILE load an external plugin (shared lib) |
| 33 | + ... |
| 34 | +``` |
| 35 | + |
| 36 | +## Build a Go Plugin |
| 37 | + |
| 38 | +The _fluent-bit-go_ package is available to assist developers in creating Go |
| 39 | +plugins. |
| 40 | + |
| 41 | +https://github.com/fluent/fluent-bit-go |
| 42 | + |
| 43 | +At a minimum, a Go plugin looks like this: |
| 44 | + |
| 45 | +```go |
| 46 | +package main |
| 47 | + |
| 48 | +import "github.com/fluent/fluent-bit-go/output" |
| 49 | + |
| 50 | +//export FLBPluginRegister |
| 51 | +func FLBPluginRegister(def unsafe.Pointer) int { |
| 52 | + // Gets called only once when the plugin.so is loaded |
| 53 | + return output.FLBPluginRegister(ctx, "gstdout", "Stdout GO!") |
| 54 | +} |
| 55 | + |
| 56 | +//export FLBPluginInit |
| 57 | +func FLBPluginInit(plugin unsafe.Pointer) int { |
| 58 | + // Gets called only once for each instance you have configured. |
| 59 | + return output.FLB_OK |
| 60 | +} |
| 61 | + |
| 62 | +//export FLBPluginFlushCtx |
| 63 | +func FLBPluginFlushCtx(ctx, data unsafe.Pointer, length C.int, tag *C.char) int { |
| 64 | + // Gets called with a batch of records to be written to an instance. |
| 65 | + return output.FLB_OK |
| 66 | +} |
| 67 | + |
| 68 | +//export FLBPluginExit |
| 69 | +func FLBPluginExit() int { |
| 70 | + return output.FLB_OK |
| 71 | +} |
| 72 | + |
| 73 | +func main() { |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +the code above is a template to write an output plugin, it's really important |
| 78 | +to keep the package name as `main` and add an explicit `main()` function. |
| 79 | +This is a requirement as the code will be build as a shared library. |
| 80 | + |
| 81 | +To build the code above, use the following line: |
| 82 | + |
| 83 | +```bash |
| 84 | +$ go build -buildmode=c-shared -o out_gstdout.so out_gstdout.go |
| 85 | +``` |
| 86 | + |
| 87 | +Once built, a shared library called `out\_gstdout.so` will be available. It's |
| 88 | +really important to double check the final .so file is what we expect. Doing a |
| 89 | +`ldd` over the library we should see something similar to this: |
| 90 | + |
| 91 | +``` |
| 92 | +$ ldd out_gstdout.so |
| 93 | + linux-vdso.so.1 => (0x00007fff561dd000) |
| 94 | + libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc4aeef0000) |
| 95 | + libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc4aeb27000) |
| 96 | + /lib64/ld-linux-x86-64.so.2 (0x000055751a4fd000) |
| 97 | +``` |
| 98 | + |
| 99 | +## Run Fluent Bit with the new plugin |
| 100 | + |
| 101 | +```bash |
| 102 | +$ bin/fluent-bit -e /path/to/out_gstdout.so -i cpu -o gstdout |
| 103 | +``` |
0 commit comments