【问题标题】:Fluentd - Using source tag as indexFluentd - 使用源标签作为索引
【发布时间】:2017-09-27 02:10:01
【问题描述】:

我有一个在 Docker 引擎上运行的 Fluentd 和 Elasticsearch 设置。我有大量服务要登录到 Fluentd。

我想做的是为我运行的每个服务创建一个标签,并将该标签用作 Elasticsearch 中的索引。这是我的设置:

<source>
  @type forward
  port 24224
  bind 0.0.0.0
</source>

<match docker.service1>
  @type elasticsearch
  host "172.20.0.3"
  port 9200
  index_name service1
  type_name fluentd
  flush_interval 10s
</match>

<match docker.service2>
  @type elasticsearch
  host "172.20.0.3"
  port 9200
  index_name service2
  type_name fluentd
  flush_interval 10s
</match>

等等。

必须为我创建的每个服务都包含一个新的匹配标签会很烦人,因为我希望能够在不更新我的 fluentd 配置的情况下添加新服务。有没有办法做这样的事情:

<source>
  @type forward
  port 24224
  bind 0.0.0.0
</source>

<match docker.**>
  @type elasticsearch
  host "172.20.0.3"
  port 9200
  index_name $(TAG)
  type_name fluentd
  flush_interval 10s
</match>

我在哪里使用 $(TAG) 变量来表明我希望 Tag 名称成为索引的名称?

我已经从我找到的答案 here: ${tag_parts[0]} 中尝试过这个。这实际上是作为我的索引打印的。所以我的索引是“${tag_parts[0]}”。

提前致谢。

【问题讨论】:

    标签: elasticsearch docker fluentd


    【解决方案1】:

    我发现我需要导入另一个 Elasticsearch 插件。这是我使用的匹配标记的示例:

    <match>
       @type elasticsearch_dynamic
       host "172.20.0.3"
       port 9200
       type_name fluentd
       index_name ${tag_parts[2]}
       flush_interval 10s
       include_tag_key true
       reconnect_on_error true
    </match>
    

    我导入了@elasticsearch_dynamic 插件而不是@elasticsearch 插件。然后,我可以使用 ${tag_parts} 东西。

    include_tag_key 会将标签包含在 json 数据中。

    阅读documentation会有所帮助

    【讨论】:

    • 我也想做同样的事情,但 elasticsearch_dynamic 被标记为已弃用,他们现在不鼓励这种方法。您是否还有这种需求,是否找到了更好的方法?
    【解决方案2】:

    我遇到了同样的问题,此处提供的解决方案已被弃用。我最终做的是:

    添加一个转换过滤器,将您想要的索引名称添加为记录上的键

    <filter xxx.*>
      @type record_transformer
      enable_ruby true
      <record>
        index_name ${tag_parts[1]}-${time.strftime('%Y%m')}
      </record>
    </filter>
    

    然后在你配置的 elasticsearch 输出中

    <match xxx.*>
      @type elasticsearch-service
      target_index_key index_name
      index_name fallback-index-%Y%m
    
    

    如果记录缺少 index_name 键,将使用此处的备用 index_name,但这绝不应该发生。

    【讨论】:

      猜你喜欢
      • 2021-02-12
      • 2019-07-13
      • 2018-10-29
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 2012-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多