|
1 | | -This example demonstrates using a plugin where there are multiple instances |
2 | | -and `FLBPluginFlushCtx` is used to disambiguate multiple instances of the |
3 | | -output plugin. |
| 1 | +# Example: out_multiinstance |
| 2 | + |
| 3 | +The following example code implements an output plugin that works with |
| 4 | +multiple configured instances. It describes how to share context from the |
| 5 | +specified instance configuration to the flush callback. |
| 6 | + |
| 7 | +Every output plugin go through four callbacks associated to different phases: |
| 8 | + |
| 9 | +| Plugin Phase | Callback | |
| 10 | +|---------------------|----------------------------| |
| 11 | +| Registration | FLBPluginRegister() | |
| 12 | +| Initialization | FLBPluginInit() | |
| 13 | +| Runtime Flush | FLBPluginFlushCtx() | |
| 14 | +| Exit | FLBPluginExit() | |
| 15 | + |
| 16 | +## Plugin Registration |
| 17 | + |
| 18 | +When Fluent Bit loads a Golang plugin, it looks up and loads the registration |
| 19 | +callback that aims to populate the internal structure with plugin name and |
| 20 | +description: |
| 21 | + |
| 22 | +```go |
| 23 | +//export FLBPluginRegister |
| 24 | +func FLBPluginRegister(def unsafe.Pointer) int { |
| 25 | + return output.FLBPluginRegister(ctx, "multiinstance", "Testing multiple instances") |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +This function is invoked at start time _before_ any configuration is done |
| 30 | +inside the engine. |
| 31 | + |
| 32 | +## Plugin Initialization |
| 33 | + |
| 34 | +Before the engine starts, it initializes all plugins that were configured. |
| 35 | +As part of the initialization, the plugin can obtain configuration parameters |
| 36 | +and do any other internal checks. It can also set the context for this |
| 37 | +instance in case params need to be retrieved during flush. |
| 38 | +E.g: |
| 39 | + |
| 40 | +```go |
| 41 | +//export FLBPluginInit |
| 42 | +func FLBPluginInit(ctx unsafe.Pointer) int { |
| 43 | + id := output.FLBPluginConfigKey(plugin, "id") |
| 44 | + log.Printf("[multiinstance] id = %q", id) |
| 45 | + // Set the context to point to any Go variable |
| 46 | + output.FLBPluginSetContext(plugin, unsafe.Pointer(&id)) |
| 47 | + return output.FLB_OK |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +The function must return FLB\_OK when it initialized properly or FLB\_ERROR if |
| 52 | +something went wrong. If the plugin reports an error, the engine will _not_ |
| 53 | +load the instance. |
| 54 | + |
| 55 | +## Runtime Flush with Context |
| 56 | + |
| 57 | +Upon flush time, when Fluent Bit wants to flush it's buffers, the runtime flush |
| 58 | +callback will be triggered. |
| 59 | + |
| 60 | +The callback will receive the configuration context, a raw buffer of msgpack |
| 61 | +data, the proper bytes length and the associated tag. |
| 62 | + |
| 63 | +```go |
| 64 | +//export FLBPluginFlushCtx |
| 65 | +func FLBPluginFlush(ctx, data unsafe.Pointer, length C.int, tag *C.char) int { |
| 66 | + |
| 67 | + id := *(*string)(ctx) |
| 68 | + log.Printf("[multiinstance] Flush called for id: %s", *id) |
| 69 | + return output.FLB_OK |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +When done, there are three returning values available: |
| 74 | + |
| 75 | +| Return value | Description | |
| 76 | +|---------------|------------------------------------------------| |
| 77 | +| FLB\_OK | The data have been processed normally. | |
| 78 | +| FLB\_ERROR | An internal error have ocurred, the plugin will not handle the set of records/data again. | |
| 79 | +| FLB\_RETRY | A recoverable error have ocurred, the engine can try to flush the records/data later.| |
| 80 | + |
| 81 | +## Plugin Exit |
| 82 | + |
| 83 | +When Fluent Bit will stop using the instance of the plugin, it will trigger the exit callback. e.g: |
| 84 | + |
| 85 | +```go |
| 86 | +//export FLBPluginExit |
| 87 | +func FLBPluginExit() int { |
| 88 | + return output.FLB_OK |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## Playground |
| 93 | + |
| 94 | +Build the docker image locally to see how it works. |
| 95 | + |
| 96 | +```bash |
| 97 | +$ cd $GOPATH/src/github.com/fluent/fluent-bit-go/examples/out_multiinstance |
| 98 | +$ docker build . -t fluent-bit-multiinstance |
| 99 | +$ docker run -it --rm fluent-bit-multiinstance |
| 100 | +``` |
0 commit comments