Fluentd
Search…
parser
The parser filter plugin "parses" string field in event records and mutates its event record with the parsed result.
It is included in the Fluentd's core.

Example Configurations

1
<filter foo.bar>
2
@type parser
3
key_name log
4
<parse>
5
@type regexp
6
expression /^(?<host>[^ ]*) [^ ]* (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)$/
7
time_format %d/%b/%Y:%H:%M:%S %z
8
</parse>
9
</filter>
Copied!
filter_parser uses built-in parser plugins and your own customized parser plugin, so you can reuse the predefined formats like apache2, json, etc. See Parser Plugin Overview for more details
With this example, if you receive this event:
1
time:
2
injected time (depends on your input)
3
record:
4
{"log":"192.168.0.1 - - [05/Feb/2018:12:00:00 +0900] \"GET / HTTP/1.1\" 200 777"}
Copied!
The parsed result will be:
1
time
2
05/Feb/2018:12:00:00 +0900
3
record:
4
{"host":"192.168.0.1","user":"-","method":"GET","path":"/","code":"200","size":"777"}
Copied!

Plugin Helpers

Parameters

<parse> Section

This is a required subsection. Specifies the parser type and related parameter.
For more details, see Parse Section Configurations.

key_name

type
default
version
string
required parameter
0.14.9
Specifies the field name in the record to parse.
This parameter supports nested field access via record_accessor syntax.

reserve_time

type
default
version
bool
false
0.14.9
Keeps the original event time in the parsed result.
1
<filter foo.bar>
2
@type parser
3
key_name log
4
reserve_time true
5
<parse>
6
@type json
7
</parse>
8
</filter>
Copied!
With above configuration, here is the result:
1
# input data: {"key":"value","log":"{\"time\":1622473200,\"user\":1}"}
Copied!
Above incoming event is parsed as:
1
time:
2
2021-06-01 00:00:00.000000000 +0900
3
4
record:
5
{
6
"user": 1
7
}
Copied!
The value of time field (1622473200) is reserved as event time (2021-06-01 00:00:00.000000000 +0900).
Without reserve_time, the result is:
1
# input data: {"key":"value","log":"{\"time\":1622473200,\"user\":1}"}
Copied!
Above incoming event is parsed as:
1
time:
2
2021-06-24 14:33:35.475115751 +0900 (It vary on parsed timestamp)
3
4
record:
5
{
6
"user": 1
7
}
Copied!
The value of parsed timestamp is set as event time. The value of time field is discarded.

reserve_data

type
default
version
bool
false
0.14.9
Keeps the original key-value pair in the parsed result.
1
<filter foo.bar>
2
@type parser
3
key_name log
4
reserve_data true
5
<parse>
6
@type json
7
</parse>
8
</filter>
Copied!
With above configuration, here is the result:
1
# input data: {"key":"value","log":"{\"user\":1,\"num\":2}"}
2
# output data: {"key":"value","log":"{\"user\":1,\"num\":2}","user":1,"num":2}
Copied!
Without reserve_data, the result is:
1
# input data: {"key":"value","log":"{\"user\":1,\"num\":2}"}
2
# output data: {"user":1,"num":2}
Copied!

remove_key_name_field

type
default
version
bool
false
1.2.2
Removes key_name field when parsing is succeeded.
1
<filter foo.bar>
2
@type parser
3
key_name log
4
reserve_data true
5
remove_key_name_field true
6
<parse>
7
@type json
8
</parse>
9
</filter>
Copied!
With above configuration, here is the result:
1
# input data: {"key":"value","log":"{\"user\":1,\"num\":2}"}
2
# output data: {"key":"value","user":1,"num":2}
Copied!

replace_invalid_sequence

type
default
version
bool
false
0.14.9
If true, invalid string is replaced with safe characters and re-parse it.

inject_key_prefix

type
default
version
string
false
0.14.9
Stores the parsed values with the specified key name prefix.
1
<filter foo.bar>
2
@type parser
3
key_name log
4
reserve_data true
5
inject_key_prefix data.
6
<parse>
7
@type json
8
</parse>
9
</filter>
Copied!
With above configuration, here is the result:
1
# input data: {"log": "{\"user\":1,\"num\":2}"}
2
# output data: {"log":"{\"user\":1,\"num\":2}","data.user":1, "data.num":2}
Copied!

hash_value_field

type
default
version
string
false
0.14.9
Stores the parsed values as a hash value in a field.
1
<filter foo.bar>
2
@type parser
3
key_name log
4
hash_value_field parsed
5
<parse>
6
@type json
7
</parse>
8
</filter>
Copied!
With above configuration, result is below:
1
# input data: {"log": "{\"user\":1,\"num\":2}"}
2
# output data: {"parsed":{"user":1,"num":2}}
Copied!

emit_invalid_record_to_error

type
default
version
bool
true
0.14.0
Emits invalid record to @ERROR label. Invalid cases are:
    key does not exist
    the format is not matched
    an unexpected error
You can rescue unexpected format logs in the @ERROR label.
If you want to ignore these errors, set false.

FAQ

suppress_parse_error_log is missing. What are the alternatives?

Since v1, parser filter does not support suppress_parse_error_log parameter because parser filter uses the @ERROR feature instead of internal logging to rescue invalid records. If you want to simply ignore invalid records, set emit_invalid_record_to_error false.
See also emit_invalid_record_to_error parameter.

Learn More

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 modified 3mo ago