Fluent Bit and Golang Plugins

Fluent Bit currently supports integration of Golang plugins built as shared objects for output plugins only. The interface for the Golang plugins is currently under development but is functional.

Getting Started

Compile Fluent Bit with Golang support, e.g:

  1. $ cd build/
  2. $ cmake -DFLB_DEBUG=On -DFLB_PROXY_GO=On ../
  3. $ make

Once compiled, we can see a new option in the binary -e which stands for external plugin, e.g:

  1. $ bin/fluent-bit -h
  2. Usage: fluent-bit [OPTION]
  3. Available Options
  4. -c --config=FILE specify an optional configuration file
  5. -d, --daemon run Fluent Bit in background mode
  6. -f, --flush=SECONDS flush timeout in seconds (default: 5)
  7. -i, --input=INPUT set an input
  8. -m, --match=MATCH set plugin match, same as '-p match=abc'
  9. -o, --output=OUTPUT set an output
  10. -p, --prop="A=B" set plugin configuration property
  11. -e, --plugin=FILE load an external plugin (shared lib)
  12. ...

Build a Go Plugin

The fluent-bit-go package is available to assist developers in creating Go plugins.

https://github.com/fluent/fluent-bit-go

At a minimum, a Go plugin looks like this:

  1. package main
  2. import "github.com/fluent/fluent-bit-go/output"
  3. //export FLBPluginRegister
  4. func FLBPluginRegister(ctx unsafe.Pointer) int {
  5. // Gets called only once when the plugin.so is loaded
  6. return output.FLBPluginRegister(ctx, "gstdout", "Stdout GO!")
  7. }
  8. //export FLBPluginInit
  9. func FLBPluginInit(plugin unsafe.Pointer) int {
  10. // Gets called only once for each instance you have configured.
  11. return output.FLB_OK
  12. }
  13. //export FLBPluginFlushCtx
  14. func FLBPluginFlushCtx(ctx, data unsafe.Pointer, length C.int, tag *C.char) int {
  15. // Gets called with a batch of records to be written to an instance.
  16. return output.FLB_OK
  17. }
  18. //export FLBPluginExit
  19. func FLBPluginExit() int {
  20. return output.FLB_OK
  21. }
  22. func main() {
  23. }

the code above is a template to write an output plugin, it’s really important to keep the package name as main and add an explicit main() function. This is a requirement as the code will be build as a shared library.

To build the code above, use the following line:

  1. $ go build -buildmode=c-shared -o out_gstdout.so out_gstdout.go

Once built, a shared library called out\_gstdout.so will be available. It’s really important to double check the final .so file is what we expect. Doing a ldd over the library we should see something similar to this:

  1. $ ldd out_gstdout.so
  2. linux-vdso.so.1 => (0x00007fff561dd000)
  3. libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc4aeef0000)
  4. libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc4aeb27000)
  5. /lib64/ld-linux-x86-64.so.2 (0x000055751a4fd000)

Run Fluent Bit with the new plugin

  1. $ bin/fluent-bit -e /path/to/out_gstdout.so -i cpu -o gstdout