Fluentd

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

<filter foo.bar>
  @type parser
  key_name log
  <parse>
    @type regexp
    expression /^(?<host>[^ ]*) [^ ]* (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)$/
    time_format %d/%b/%Y:%H:%M:%S %z
  </parse>
</filter>

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

filter_parser receives events which are already split, so multiline cannot join them into one record. See multiline for the details.

With this example, if you receive this event:

time:
injected time (depends on your input)
record:
{"log":"192.168.0.1 - - [05/Feb/2018:12:00:00 +0900] \"GET / HTTP/1.1\" 200 777"}

The parsed result will be:

time
05/Feb/2018:12:00:00 +0900
record:
{"host":"192.168.0.1","user":"-","method":"GET","path":"/","code":"200","size":"777"}

Plugin Helpers

Parameters

See Common 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.

<filter foo.bar>
  @type parser
  key_name log
  reserve_time true
  <parse>
    @type json
  </parse>
</filter>

With above configuration, here is the result:

# input data:  {"key":"value","log":"{\"time\":1622473200,\"user\":1}"}

Above incoming event is parsed as:

time:
2021-06-24 14:33:35.475115751 +0900 (It varies โ€” original event time)

record:
{
  "user": 1
}

The original event time is kept. The value of the parsed time field (1622473200) is discarded as event time.

Without reserve_time, the result is:

# input data:  {"key":"value","log":"{\"time\":1622473200,\"user\":1}"}

Above incoming event is parsed as:

time:
2021-06-01 00:00:00.000000000 +0900

record:
{
  "user": 1
}

The value of the time field (1622473200) is set as event time (2021-06-01 00:00:00.000000000 +0900).

reserve_data

type default version
bool false 0.14.9

Keeps the original key-value pair in the parsed result.

<filter foo.bar>
  @type parser
  key_name log
  reserve_data true
  <parse>
    @type json
  </parse>
</filter>

With above configuration, here is the result:

# input data:  {"key":"value","log":"{\"user\":1,\"num\":2}"}
# output data: {"key":"value","log":"{\"user\":1,\"num\":2}","user":1,"num":2}

Without reserve_data, the result is:

# input data:  {"key":"value","log":"{\"user\":1,\"num\":2}"}
# output data: {"user":1,"num":2}

reserve_data also changes what happens to a record that cannot be parsed. See emit_invalid_record_to_error for details.

remove_key_name_field

type default version
bool false 1.2.2

Removes key_name field when parsing succeeds.

<filter foo.bar>
  @type parser
  key_name log
  reserve_data true
  remove_key_name_field true
  <parse>
    @type json
  </parse>
</filter>

With above configuration, here is the result:

# input data:  {"key":"value","log":"{\"user\":1,\"num\":2}"}
# output data: {"key":"value","user":1,"num":2}

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 nil 0.14.9

Stores the parsed values with the specified key name prefix.

<filter foo.bar>
  @type parser
  key_name log
  reserve_data true
  inject_key_prefix data.
  <parse>
    @type json
  </parse>
</filter>

With above configuration, here is the result:

# input data:  {"log": "{\"user\":1,\"num\":2}"}
# output data: {"log":"{\"user\":1,\"num\":2}","data.user":1, "data.num":2}

hash_value_field

type default version
string nil 0.14.9

Stores the parsed values as a hash value in a field.

<filter foo.bar>
  @type parser
  key_name log
  hash_value_field parsed
  <parse>
    @type json
  </parse>
</filter>

With above configuration, result is below:

# input data:  {"log": "{\"user\":1,\"num\":2}"}
# output data: {"parsed":{"user":1,"num":2}}

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.

Whether an invalid record is also passed to the next filter or output depends on reserve_data and on the case:

invalid case reserve_data false reserve_data true
key does not exist record is discarded original record is passed through
the format is not matched record is discarded original record is passed through, unparsed
an unexpected error record is discarded record is discarded

"Key does not exist" also covers a key_name field whose value is null. "The format is not matched" means that the parser returns no record, as the json and regexp parsers do for a value they cannot parse. "An unexpected error" means that the parser raises an error. For example, the json and regexp parsers raise an error when the time field cannot be parsed with time_format. The regexp parser also raises an error on an invalid byte sequence while replace_invalid_sequence is false. A record whose parsing raises an error is discarded even with reserve_data true.

emit_invalid_record_to_error does not change this. Since it is true by default, a record passed through with reserve_data true is also emitted to the @ERROR label, so the same record goes to both. If you only want to pass such records through, set emit_invalid_record_to_error false:

<filter foo.bar>
  @type parser
  key_name log
  reserve_data true
  emit_invalid_record_to_error false
  <parse>
    @type json
  </parse>
</filter>

With above configuration, here is the result for a value which is not JSON:

# input data:  {"key":"value","log":"not json"}
# output data: {"key":"value","log":"not json"}

With above configuration, a record whose parsing raises an error is discarded without an error event or a log.

If hash_value_field is set, an empty hash is stored in that field of a record passed through. The record emitted to the @ERROR label does not get the empty hash.

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

results matching ""

    No results matching ""