Config File Syntax (YAML)
This article describes the basic concepts of Fluentd configuration file syntax for yaml format.
Introduction: The Lifecycle of a Fluentd Event
Here is a brief overview of the lifecycle of a Fluentd event to help you understand the rest of this page:
The configuration file allows the user to control the input and output behavior of Fluentd by 1) selecting input and output plugins; and, 2) specifying the plugin parameters. The file is required for Fluentd to operate properly.
See also: Lifecycle of a Fluentd Event
YAML adoption
Fluentd starts to support YAML configuration format but this is not 1-by-1 correspondence for Fluentd config file syntax.
Normal Fluentd configuration syntax has the following the list of directives:
sourcedirectives determine the input sourcesmatchdirectives determine the output destinationsfilterdirectives determine the event processing pipelinessystemdirectives set system-wide configurationlabeldirectives group the output and filter for internal routingworkerdirectives limit to the specific workers@includedirectives include other files
In YAML configuration world, we reconstructed them for YAML format.
In YAML syntax, Fluentd will handle the two top level objects:
systemThe top level object that specifies system settingsconfigAnother top level object that defines data pipeline
Under config object, Fluentd will handle the following elements:
sourceelements determine the input sourcesmatchelements determine the output destinationsfilterelements determine the event processing pipelineslabelelements group the output and filter for internal routing
Special YAML elements
!includedefines including rules for other files!fluent/sdefines Fluentd string format that is equivalent for double quoted string!fluent/jsondefines Fluentd JSON format that is used for Hash type object$tagdefines tag for output plugin$labeldefines label routes for input plugin$argdefines directive arg's equivalent objects (e.g. <DIRECTIVE arg>)$namedefines label directive name equivalent objects (e.g. <label name>)$typespecifies@typeortypefor instantiating plugin type$log_levelspecifies@log_levelper plugin
NOTE: $log_level is supported since Fluentd v1.17.1 or v1.16.6. If you want to change log_level per plugin on 1.17.0/v1.16.5 or older versions, you can use log_level (without $) although it is deprecated.
Let's actually create a configuration file step by step.
1. source: where all the data come from
source: where all the data come fromFluentd input sources are enabled by selecting and configuring the desired input plugins using source elements. Fluentd standard input plugins include http and forward. The http provides an HTTP endpoint to accept incoming HTTP messages whereas forward provides a TCP endpoint to accept TCP packets. Of course, it can be both at the same time. You may add multiple source configurations as required.
Each source element must include a $type object to specify the input plugin to use.
Didn't find your input source? You can write your own plugin!
You can add new input sources by writing your own plugins. For further information regarding Fluentd input sources, please refer to the Input Plugin Overview article.
2. "match": Tell fluentd what to do!
The match element looks for events with matching tags and processes them. The most common use of the match element is to output events to other systems. For this reason, the plugins that correspond to the match element are called output plugins. Fluentd standard output plugins include file and forward. Let's add those to our configuration file.
Each match element must include a $tag and a $type parameter. Only events with a $tag matching the pattern will be sent to the output destination (in the above example, only the events with the tag myapp.access are matched. See the section below for more advanced usage). The $type parameter specifies the output plugin to use.
Just like input sources, you can add new output destinations by writing custom plugins. For further information regarding Fluentd output destinations, please refer to the Output Plugin Overview article.
3. "filter": Event processing pipeline
The filter element has the same syntax as match but filter could be chained for processing pipeline. Using filters, event flow is like this:
Let's add standard record_transformer filter to match example.
The received event {"event":"data"} goes to record_transformer filter first. The record_transformer filter adds host_param field to the event; and, then the filtered event {"event":"data","host_param":"webserver1"} goes to the file output plugin.
You can also add new filters by writing your own plugins. For further information regarding Fluentd filter destinations, please refer to the Filter Plugin Overview article.
4. Set system-wide configuration: the system element
system elementSystem-wide configurations are set by system element. Most of them are also available via command line options. For example, the following configurations are available:
log_levelsuppress_repeated_stacktraceemit_error_log_intervalsuppress_config_dumpwithout_sourceprocess_name(Only available insystemelement. No fluentd option)
Example:
See also System Configuration article for more detail.
process_name
process_nameIf this parameter is set, fluentd supervisor and worker process names are changed.
With this configuration, ps command shows the following result:
This feature requires Ruby 2.1 or later.
5. Group filter and output: the "label" element
The label element groups filter and output for internal routing. The label reduces the complexity of tag handling.
The label element's parameter is a builtin plugin parameter so $name parameter is needed.
Here is a configuration example:
Note that $label parameter value should be quoted and label element should contain config element within its elements.
In this configuration, forward events are routed to record_transformer filter / elasticsearch output and in_tail events are routed to grep filter / s3 output inside @SYSTEM label.
The $label parameter is useful for event flow separation without the $tag route rules.
@ERROR label
@ERROR labelThe @ERROR label is a builtin label used for error record emitted by plugin's emit_error_event API.
If $label with $name: '@ERROR' is set, the events are routed to this label when the related errors are emitted e.g. the buffer is full or the record is invalid.
@ROOT label
@ROOT labelThe @ROOT label is a builtin label used for getting root router by plugin's event_emitter_router API.
This label is introduced since v1.14.0 to assign a label back to the default route. For example, timed-out event records are handled by the concat filter can be sent to the default route.
6. Limit to specific workers: the worker element
worker elementWhen setting up multiple workers, you can use the worker element to limit plugins to run on specific workers.
This is useful for input and output plugins that do not support multiple workers.
You can use the $arg N or $arg N-M to specify workers. The number is a zero-based worker index.
See Multi Process Workers article for details about multiple workers.
Here is a configuration example:
The outputs of this config are as follows:
7. Reuse your config: the !include YAML tag
!include YAML tagThe element in separate configuration files can be imported using the !include element:
The !include YAML tag supports regular file path, glob pattern, and http URL conventions:
Note that for the glob pattern, files are expanded in alphabetical order. If there are a.conf and b.conf then fluentd parses a.conf first. But, you should not write the configuration that depends on this order. It is so error-prone, therefore, use multiple separate !include YAML tags for safety.
Share the Same Parameters
The !include YAML tag can be used under sections to share the same parameters:
Note that, in the middle of element case of !include YAML tag usage, users must use <<: syntax to include other YAML objects successfully.
Note on Match Order
Fluentd tries to match tags in the order that they appear in the config file. So, if you have the following configuration:
then myapp.access is never matched. Wider match patterns should be defined after tight match patterns.
Of course, if you use two same patterns, the second match is never matched. If you want to send events to multiple outputs, consider out_copy plugin.
The common pitfall is when you put a filter element after match element. It will never work since events never go through the filter for the reason explained above.
Embedding Ruby Expressions
When using YAML format syntax in Fluentd configuration, you can use !fluent/s "#{...}" to embed arbitrary Ruby code into match patterns. Here is an example:
If you set the environment variable FLUENTD_TAG to dev, this evaluates to app.dev.
Supported Data Types for Values
Please refer to: Supported Data Types for Values | Fluentd official document
Common Plugin Parameters
Please refer to: Common Plugin Parameters | Fluentd official document
Check Configuration File
The configuration file can be validated without starting the plugins using the --dry-run option:
Format Tips
This section describes some useful features for the configuration file.
Section
Points:
You can configure a section by using its name as the key.
You can configure multiple sections by using Array as the value.
Here are examples.
Parse Section:
Buffer Section:
Inject/Format Section:
Multiple Section:
Multiline support for " quoted string, array and hash values
You can write multiline values for " quoted string, array and hash values.
Fluentd assumes [ or { is a start of array / hash. So, if you want to set [ or { started but non-JSON parameter, please use ' or ".
Example # 1: mail plugin
Example # 2: map plugin
This restriction will be removed with the configuration parser improvement.
Embedded Ruby Code
You can evaluate the Ruby code with !fluent/s #{} in " quoted string. This is useful for setting machine information e.g. hostname.
In YAML config format, hostname and worker_id shortcuts are also available:
The worker_id shortcut is useful under multiple workers. For example, for a separate plugin id, add worker_id to store the path in s3 to avoid file conflict.
Helper methods use_nil and use_default are available:
Note that these methods not only replace the embedded Ruby code but the entire string with nil or a default value.
In double-quoted string literal, \ is the escape character
\ is the escape characterThe backslash \ is interpreted as an escape character. You need \ for setting ", \r, \n, \t, \ or several characters in double-quoted string literal.
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
Was this helpful?