How to Write Parser Plugin
Fluentd supports pluggable and customizable formats for input plugins. The plugin filenames prefixed parser_
are registered as Parser Plugins.
See Plugin Base Class API for details on the common APIs for all the plugin types.
Here is an example of a custom parser that parses the following newline-delimited log format:
like this:
While it is not hard to write a regular expression to match this format, it is tricky to extract and save key names.
Here is the code to parse this custom format (let's call it time_key_value
). It takes one optional parameter called delimiter
, which is the delimiter for key/value pairs. It also takes time_format
to parse the time
string.
Save this code as parser_time_key_value.rb
in a loadable plugin path.
With in_tail
configured as:
this log line:
will be parsed as:
For more details on <parse>
, see Parse Section Configurations.
How To Use Parsers From Plugins
Parser plugins are designed to be used with other plugins, like Input, Filter and Output. There is a Parser plugin helper solely for this purpose:
See Parser Plugin Helper API for details.
Methods
Parser plugins have a method to parse input (text) data to a structured record (Hash
) with time.
#parse(text, &block)
#parse(text, &block)
It gets input data as text
, and call &block
to feed the results of the parser. The input text
may contain two or more records so that means the parser plugin might call the &block
two or more times for one argument.
Parser plugins must implement this method.
Writing Tests
Fluentd parser plugin has one or more points to be tested. Others (parsing configurations, controlling buffers, retries, flushes, etc.) are controlled by the Fluentd core.
Fluentd also provides the test driver for plugins. You can easily write tests for your own plugins:
Overview of Tests
Testing for parser plugins is mainly for:
Validation of configuration (i.e.
#configure
)Validation of the parsed time and record
To make testing easy, the plugin test driver provides a logger and the functionality to override the system and parser configurations, etc.
The lifecycle of plugin and test driver is:
Instantiate plugin driver which then instantiates the plugin
Configure plugin
Run test code
Assert results of tests by data provided by the driver
For:
configuration tests, repeat steps # 1-2
full feature tests, repeat steps # 1-4
See Testing API for Plugins for details.
If this article is incorrect or outdated, or omits critical information, please let us know. Fluentd is an open-source project under Cloud Native Computing Foundation (CNCF). All components are available under the Apache 2 License.
Last updated