|
| 1 | +# Example: in_gdummy |
| 2 | + |
| 3 | +The following example code implements an input plugin that works with |
| 4 | +separated input collecting threads that is introduced in Fluent Bit 1.9. |
| 5 | +It describes how to share context from the |
| 6 | +specified instance configuration to the input callback. |
| 7 | + |
| 8 | +Every output plugin go through four callbacks associated to different phases: |
| 9 | + |
| 10 | +| Plugin Phase | Callback | |
| 11 | +|---------------------|----------------------------| |
| 12 | +| Registration | FLBPluginRegister() | |
| 13 | +| Initialization | FLBPluginInit() | |
| 14 | +| Input Callback | FLBPluginInputCallback() | |
| 15 | +| Exit | FLBPluginExit() | |
| 16 | + |
| 17 | +## Plugin Registration |
| 18 | + |
| 19 | +When Fluent Bit loads a Golang input plugin, it looks up and loads the registration |
| 20 | +callback that aims to populate the internal structure with plugin name and |
| 21 | +description: |
| 22 | + |
| 23 | +```go |
| 24 | +//export FLBPluginRegister |
| 25 | +func FLBPluginRegister(def unsafe.Pointer) int { |
| 26 | + return input.FLBPluginRegister(def, "gdummy", "dummy Go!") |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +## Plugin Initialization |
| 31 | + |
| 32 | +Before the engine starts, it initialize all plugins that were requested to start. |
| 33 | +Upon initialization a configuration context already exists, |
| 34 | +so the plugin can ask for configuration parameters or do any other internal checks. E.g: |
| 35 | + |
| 36 | +```go |
| 37 | +//export FLBPluginInit |
| 38 | +func FLBPluginInit(ctx unsafe.Pointer) int { |
| 39 | + return input.FLB_OK |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +The function must return FLB\_OK when it initialized properly or FLB\_ERROR if something went wrong. If the plugin reports an error, the engine will _not_ load the instance. |
| 44 | + |
| 45 | +## Input Callback |
| 46 | + |
| 47 | +When Fluent Bit wants to collect logs from Golang input plugin, the input callback will be triggered. |
| 48 | + |
| 49 | +The callback will send a raw buffer of msgpack data with it proper bytes length into Fluent Bit core. |
| 50 | + |
| 51 | +```go |
| 52 | +//export FLBPluginInputCallback |
| 53 | +func FLBPluginInputCallback(data *unsafe.Pointer, size *C.size_t) int { |
| 54 | + now := time.Now() |
| 55 | + // To handle nanosecond precision on Golang input plugin, you must wrap up time instances with input.FLBTime type. |
| 56 | + flb_time := input.FLBTime{now} |
| 57 | + message := map[string]string{"message": "dummy"} |
| 58 | + |
| 59 | + entry := []interface{}{flb_time, message} |
| 60 | + |
| 61 | + // Some encoding logs to msgpack payload stuffs. |
| 62 | + // It needs to Wait for some period on Golang input plugin side, until the new records are emitted. |
| 63 | + |
| 64 | + *data = unsafe.Pointer(&packed[0]) |
| 65 | + *size = C.size_t(len(packed)) |
| 66 | + return input.FLB_OK |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +> for more details about how to process the sending msgpack data into Fluent Bit core, please refer to the [in_gdummy.go](in_gdummy.go) file. |
| 71 | +
|
| 72 | +When done, there are three returning values available: |
| 73 | + |
| 74 | +| Return value | Description | |
| 75 | +|---------------|------------------------------------------------| |
| 76 | +| FLB\_OK | The data have been processed normally. | |
| 77 | +| FLB\_ERROR | An internal error have ocurred, the plugin will not handle the set of records/data again. | |
| 78 | +| FLB\_RETRY | A recoverable error have ocurred, the engine can try to flush the records/data later.| |
| 79 | + |
| 80 | +## Plugin Exit |
| 81 | + |
| 82 | +When Fluent Bit will stop using the instance of the plugin, it will trigger the exit callback. e.g: |
| 83 | + |
| 84 | +```go |
| 85 | +//export FLBPluginExit |
| 86 | +func FLBPluginExit() int { |
| 87 | + return input.FLB_OK |
| 88 | +} |
| 89 | +``` |
0 commit comments