流利:如何覆盖时间属性

arc*_*max 3 fluentd

盖兹......我们被卡住了...保释我们!:-)

我们正在使用Fluentd进行三步日志聚合管道.

[#1 - 尾部日志(原始日志)] - (TCP) - > [#2 - 将读取的日志解析为JSON] - (TCP) - > [#3 - 过滤并输出到Redis&Mongo]

我们不会在第一步将尾部日志转换为JSON.这主要是因为我们希望避免该服务器上的任何额外CPU消耗.我们拥有的日志行非常复杂,并且有意将延迟解析为步骤#2(在不同的群集/服务器上).

因此阶段#1发出:时间,标记和记录(原始日志行).我们在这里使用in_tail插件,因此默认情况下,'time'属性表示从文件中读取记录的时间.因此,在负载下,读取时间可能与日志行的实际时间戳不匹配.

JSON解析延迟到第二阶段.

在第二阶段,一旦我们将日志转换为JSON ...我们希望将阶段#1发送的'time'属性覆盖到JSON记录中的time属性.

我们在步骤#2使用Fluent-Plugin-Parser(https://github.com/tagomoris/fluent-plugin-parser).

我们如何覆盖时间属性并使FluentD使用它而不是步骤#1中读取的"时间"?

Kiy*_*ura 13

是的,您可以使用fluent-plugin-parser的未记录的功能"time_key"执行此操作,如下所示:

<source>
  type exec
  run_interval 3s
  format json
  command echo '{"message":"hello,2013-03-03 12:00:13"}'
  tag first
</source>

<match first>
  type parser
  key_name message
  time_key my_time
  time_format %Y-%m-%d %H:%M:%S
  format /^(?<some_field>[^,]*),(?<my_time>.*)/
  tag second
</match>

<match second>
  type stdout
</match>
Run Code Online (Sandbox Code Playgroud)

上面的代码片段的作用是:

  1. {"message":"hello,2013-03-03 12:00:13"}使用标记"first"每3秒生成一次消息.这是为了测试的目的.
  2. 与之相匹配<match first>.然后,解析器插件使用正则表达式解析名为"message"的字段.在你的情况下,它会format json.
  3. time_key my_time告诉解析器插件在"message"字段的解析值内寻找一个字段,如果它存在,则解析该字段time_format %Y-%m-%d %H:%M:%S.从现在开始,这是新时代
  4. 最后,我输出到stdout.

如果你运行上面的conf,你应该得到这样的输出:

root@ae4a398d41ef:/home/fluentd# fluentd -c fluent.conf
2014-05-31 00:01:19 +0000 [info]: starting fluentd-0.10.46
2014-05-31 00:01:19 +0000 [info]: reading config file path="fluent.conf"
2014-05-31 00:01:19 +0000 [info]: gem 'fluent-plugin-parser' version '0.3.4'
2014-05-31 00:01:19 +0000 [info]: gem 'fluentd' version '0.10.46'
2014-05-31 00:01:19 +0000 [info]: using configuration file: <ROOT>
  <source>
    type exec
    run_interval 3s
    format json
    command echo '{"message":"hello,2013-03-03 12:00:13"}'
    tag first
  </source>
  <match first>
    type parser
    key_name message
    time_key my_time
    time_format %Y-%m-%d %H:%M:%S
    format /^(?<some_field>[^,]*),(?<my_time>.*)/
    tag second
  </match>
  <match second>
    type stdout
  </match>
</ROOT>
2014-05-31 00:01:19 +0000 [info]: adding source type="exec"
2014-05-31 00:01:19 +0000 [info]: adding match pattern="first" type="parser"
2014-05-31 00:01:19 +0000 [info]: adding match pattern="second" type="stdout"
2013-03-03 12:00:13 +0000 second: {"some_field":"hello"}
2013-03-03 12:00:13 +0000 second: {"some_field":"hello"}
2013-03-03 12:00:13 +0000 second: {"some_field":"hello"}
2013-03-03 12:00:13 +0000 second: {"some_field":"hello"}
Run Code Online (Sandbox Code Playgroud)