Writing Golang Fluent Bit Input Plugins
Context
Fluent Bit is a highly reliable and memory-efficient pipeline data processing engine. It is widely used as a low-forwarder in the Cloud-native landscape and supports metrics and traces as core features. While Fluent Bit itself is written in C, it provides extensible interfaces that allow input, filter, and output plugins to be written in various languages, including Golang.
At Calyptia, we have developed a Golang library to support writing input plugins for Fluent Bit. You can find the library at https://github.com/calyptia/plugin.
How to Write a Golang Input Plugin
To demonstrate how easy it is to extend Fluent Bit’s input capabilities, let’s consider a scenario where we want to monitor the status of GitHub and send an alert to a Slack channel in case of an outage. We will break down the implementation into the following steps:
- Fetch the GitHub status API.
- Filter the response to check if the status is operational. We will implement this using a Fluent Bit Lua filter.
- Dispatch a message to Slack. We will utilize the Fluent Bit Slack output plugin for this part.
For Step 1, we will implement a custom Fluent Bit input plugin using Golang.
Implementing the Plugin
Implementing an input plugin in Golang requires implementing a simple interface defined in the github.com/calyptia/plugin
package:
|
|
You can find further information on implementing this interface in the github.com/calyptia/plugin
repository.
Here’s an example implementation of a Fluent Bit input plugin for monitoring GitHub status:
|
|
In this example, we implement the Collect
method, which periodically fetches the GitHub status information and sends it as a message. We use the time
and net/http
packages to make the API request and process the response using JSON encoding.
Building the Plugin
To build the plugin, use the following command:
|
|
If you are using an M1-based machine, compile the plugin with the following command:
|
|
The resulting github_status.so
file should be placed in the /fluent-bit/etc
directory.
Configuration
To complete Steps 2 and 3, you need a Fluent Bit configuration that includes filtering the input records and sending them to Slack. Here’s an example Fluent Bit configuration (fluent-bit.yaml
) that uses the Go Fluent Bit GitHub Status plugin:
|
|
Replace https://hooks.slack.com/xxxx
with the actual webhook URL for your Slack integration.
Additionally, create a plugins.conf
file with the following content:
|
|
Running the Plugin
Finally, run the latest Fluent Bit release with the plugin loaded and executed using the following command:
|
|
Adjust the volume mounts ($(pwd)
represents the current directory) and the Fluent Bit image tag according to your environment.
With this configuration, the Go Fluent Bit GitHub Status plugin will periodically check the GitHub status and rewrite the record’s “status” field to indicate whether all systems are operational or if there are issues. The output can be sent to a Slack channel using the slack
output plugin or modified to fit your specific use case.
Conclusion
Congratulations! You have successfully written a Golang input plugin for Fluent Bit. This demonstrates how easy it is to extend Fluent Bit’s functionality using Golang and integrate custom features into your data processing pipeline. Feel free to explore other possibilities and experiment with different plugins and configurations to suit your specific requirements. The plugin works flawless, it runs every 5 minutes and reports back the status to the slack channel.
Links
The source code of this experiment can be found here: https://github.com/niedbalski/go-fluentbit-github-status.