Fluentd - 使用源标记作为索引

Fluentd - Using source tag as index

我在 Docker 引擎上设置了 Fluentd 和 Elasticsearch 运行ning。我有大量服务想登录到 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) 变量来指示我希望标签名称成为索引的名称?

我已经根据找到的答案尝试了此操作 here:${tag_parts[0]}。这是按字面意思印刷的,作为我的索引。所以我的索引是“${tag_parts[0]}”。

提前致谢。

我发现我需要导入另一个 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

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

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

<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,但绝不会发生这种情况。