fluentd:多个过滤器和匹配项的一个来源

fluentd: one source for several filters and matches

我有来源:

<source>
    @type tail
    tag service
    path /tmp/l.log
    format json
    read_from_head true
</source>

我想在上面做几个过滤器,match它到几个输出:

<source>
    @type tail
    tag service.pi2
    path /tmp/out.log
    format json
    read_from_head true
</source>

<source>
    @type tail
    tag service.data
    path /tmp/out.log
    format json
    read_from_head true
</source>

<filter service.data>
   # some filtering
</filter>

<filter service.pi2>
   # some filtering
</filter>

<match service.data>
  @type file
  path /tmp/out/data
</match>

<match service.pi2>
  @type file
  path /tmp/out/pi
</match>

到目前为止,为了使一切正常,我必须使用不同的标签复制 source。我可以使它从一个源定义工作吗?

您可以尝试使用插件 copy and relabel 来实现这一点。示例配置如下所示。

//One Source
<source>
    @type tail
    tag service
    path /tmp/l.log
    format json
    read_from_head true
</source>

//Now Copy Source Events to 2 Labels
<match service>
  @type copy
  <store>
    @type relabel
    @label @data
  </store>
  <store>
    @type relabel
    @label @pi2
  </store>
</match>

//@data Label, you can perform desired filter and output file
<label @data>
  <filter service>
    ...
  </filter>
  <match service>
    @type file
    path /tmp/out/data
  </match>
</label>

//@pi2 Label, you can perform desired filter and output file
<label @pi2>
  <filter service>
    ...
  </filter>
  <match service>
    @type file
   path /tmp/out/pi
  </match>
</label>

这篇 Routing 示例文章通过重写标签等方式提供了更多方法,但对我来说,我喜欢使用标签,上面看起来很简单。

我已经测试了上面的配置并且它工作正常。让我知道你的想法:)。

我是用 rewrite_tag_filter 做的。

首先我通过 TCP

创建了 source
<source>
  @type tcp
  tag tcp.price-parser
  port 20001
  bind 0.0.0.0
  <parse>
    @type json
  </parse>
</source>

第二步是匹配tcp.price-parcer标签并用JSON数据重写标签

<match tcp.price-parser>
  @type rewrite_tag_filter
  <rule>
    key tag
    pattern /(info|error)/
    tag .${tag}
  </rule>
</match>

它的重要设置rule并匹配它。如果不匹配,fluentd 不会走得更远。 所以我的 keytag。此密钥来自 JSON。例如 JSON:

{"tag":"info","message":"My first message"}

并规则 pattern 正则表达式 JSON tag 具有 /(info|error)/ 值的键。如果找到 infoerror 我们可以重写 fluentd tag。 所以 tag .${tag} 等于 info.tcp.price-parsererror.tcp.price-parser.

现在可以匹配重写标签了

<match info.tcp.price-parser>
  @type slack
  token xoxb-***
  channel price-parser
  username la
  icon_emoji :ghost:
  message "%s"
  message_keys message
  flush_interval 5s
</match>