解析器部分的 Fluentd 时间格式不起作用

Fluentd time format in parser section doesn't work

嗨,Fluentd 专家和用户!

我发现 Fluentd 解析器无法解析我的 json 格式的时间字段。

坦率地说,我不知道通过解析器后时间应该是什么样子,因为我从来没有成功过。我参考的配置是这个 link: https://docs.fluentd.org/configuration/parse-section#time-parameters

我的测试用例很简单,输入是一个带有时间字段 {"hello":"world", "time":"1991-02-19 00:00:00"} 的虚拟 Json。

但是不管我怎么修改时间格式,或者故意把输入的时间格式写错,都不会影响最后的输出,而且最后的输出好像只是把时间字段打印成字符串。

这是我的流利配置:

<source>
  @type dummy
  dummy {"hello":"world", "time":"1991-02-19 00:00:00"}
  tag sample
  <parse>
    @type json
    time_type string
    time_format "%Y-%m-%dT%H:%M:%S"
    time_key "time"
    keep_time_key true
  </parse>
</source>

<match sample>
  @type stdout
  @id out_stdout
</match>

输出为:

2022-04-12 01:44:01.034400852 +0000 sample: {"hello":"world","time":"1991-02-19 00:00:00"}
2022-04-12 01:44:02.035744518 +0000 sample: {"hello":"world","time":"1991-02-19 00:00:00"}
2022-04-12 01:44:03.037178309 +0000 sample: {"hello":"world","time":"1991-02-19 00:00:00"}
2022-04-12 01:44:04.038867836 +0000 sample: {"hello":"world","time":"1991-02-19 00:00:00"}
....

可以看到虽然输入的时间字段少了一个'T',但是没有报错和警告,输出只是把原始字符串送出去。

如果我将 'T' 放回输入,输出也会得到 'T'。配置和输出为:

<source>
  @type dummy
  dummy {"hello":"world", "time":"1991-02-19T00:00:00"}
  tag sample
  <parse>
    @type json
    time_type string
    time_format "%Y-%m-%dT%H:%M:%S"
    time_key "time"
    keep_time_key true
  </parse>
</source>

<match sample>
  @type stdout
  @id out_stdout
</match>
2022-04-12 01:59:56.026574094 +0000 sample: {"hello":"world","time":"1991-02-19T00:00:00"}
2022-04-12 01:59:57.028190722 +0000 sample: {"hello":"world","time":"1991-02-19T00:00:00"}
2022-04-12 01:59:58.029744904 +0000 sample: {"hello":"world","time":"1991-02-19T00:00:00"}
2022-04-12 01:59:59.031296625 +0000 sample: {"hello":"world","time":"1991-02-19T00:00:00"}
...

所以我很困惑,fluentd的解析器中的时间格式真的有用吗?而且我不确定这是Fluentd的问题,还是Plugin的问题,还是我的配置有误。

非常感谢!

dummy/sample 输入插件不支持 <parse> 部分。

只需 运行 fluentd 使用您的配置并在日志中观察到这些警告:

2022-04-12 19:44:56 +0500 [warn]: section <parse> is not used in <source> of sample plugin
2022-04-12 19:44:56 +0500 [warn]: section <parse> is not used in <source> of sample plugin
2022-04-12 19:44:56 +0500 [warn]: section <parse> is not used in <source> of sample plugin
2022-04-12 19:44:56 +0500 [warn]: section <parse> is not used in <source> of sample plugin
2022-04-12 19:44:56 +0500 [warn]: section <parse> is not used in <source> of sample plugin

相反,您可能想要添加类型为 parser configured for json 格式的 <filter> 部分。

您应该始终检查日志是否有任何问题。此外,您可以打开带有 -v 标志的调试日志或带有 -vv 标志的跟踪日志。

--dry-run 标志非常方便地验证配置文件,例如:

fluentd -c fluent.conf --dry-run

fluentd --help 将为您提供可用标志的完整列表。