Fluentd - 如何解析消息为 JSON 格式解析且消息为文本的日志;不会因为解析错误而迷路

时间:2021-06-27 12:35:21

标签: fluentd efk

我有来自某些服务的某些 JSON 格式的日志消息;然后这个 fluentd 过滤器能够正确解析它。然而有了这个;它会丢弃来自消息字段不是正确 JSON 的其他组件的所有其他日志。

 <source>
      @type tail
      @id in_tail_container_logs
      path /var/log/containers/*.log
      pos_file /var/log/fluentd-containers.log.pos
      tag "#{ENV['FLUENT_CONTAINER_TAIL_TAG'] || 'kubernetes.*'}"
      exclude_path "#{ENV['FLUENT_CONTAINER_TAIL_EXCLUDE_PATH'] || use_default}"
      read_from_head true
      #https://github.com/fluent/fluentd-kubernetes-daemonset/issues/434#issuecomment-752813739
      #<parse>
      #  @type "#{ENV['FLUENT_CONTAINER_TAIL_PARSER_TYPE'] || 'json'}"
      #  time_format %Y-%m-%dT%H:%M:%S.%NZ
      #</parse>
      #https://github.com/fluent/fluentd-kubernetes-daemonset/issues/434#issuecomment-831801690
      <parse>
        @type cri
        <parse> # this will parse the neseted feilds properly - like message in JSON; but if mesage is not in json then this is lost
          @type json 
        </parse>
      </parse>
      #emit_invalid_record_to_error # when nested logging fails, see if we can parse via JSON
      #tag backend.application
    </source>

enter image description here

但是所有其他没有正确 JSON 格式的消息都会丢失;

如果我注释掉类型 cri 中的嵌套解析部分;然后我得到所有日志;但不会进一步解析消息为 JSON 格式的日志。特别是严重性字段。请参阅下面屏幕截图中的最后两行

  <parse>
        @type cri
   </parse>

enter image description here

为了克服这一点;如果某些日志的嵌套解析失败,我尝试使用 LABEL @ERROR;其消息不是 JSON 格式 - 我仍然需要在 Kibana 中以文本形式查看 pod 名称和其他详细信息和消息;但是,使用以下配置,它只能解析消息为正确 JSON 格式的日志

    <source>
      @type tail
      @id in_tail_container_logs
      path /var/log/containers/*.log
      pos_file /var/log/fluentd-containers.log.pos
      tag "#{ENV['FLUENT_CONTAINER_TAIL_TAG'] || 'kubernetes.*'}"
      exclude_path "#{ENV['FLUENT_CONTAINER_TAIL_EXCLUDE_PATH'] || use_default}"
      read_from_head true
      #https://github.com/fluent/fluentd-kubernetes-daemonset/issues/434#issuecomment-752813739
      #<parse>
      #  @type "#{ENV['FLUENT_CONTAINER_TAIL_PARSER_TYPE'] || 'json'}"
      #  time_format %Y-%m-%dT%H:%M:%S.%NZ
      #</parse>
      #https://github.com/fluent/fluentd-kubernetes-daemonset/issues/434#issuecomment-831801690
      <parse>
        @type cri
        <parse> # this will parse the neseted feilds properly - like message in JSON; but if mesage is not in json then this is lost
          @type json 
        </parse>
      </parse>
      #emit_invalid_record_to_error # when nested logging fails, see if we can parse via JSON
      #tag backend.application
    </source>


    <label @ERROR> # when nested logs fail this is not working
      <filter **>
        @type parser
        key_name message
        <parse>
          @type none
        </parse>
      </filter>
      <match kubernetes.var.log.containers.elasticsearch-kibana-**> #ignore from this container
        @type null
      </match>
    </label>

enter image description here

如何获取已解析为 JSON 格式的消息的日志;以及谁的消息是文本;不会迷路吗?

此处配置(最后提交)https://github.com/alexcpn/grpc_templates.git

1 个答案:

答案 0 :(得分:1)

解决这个问题的一种方法是在使用cir插件解析它们之前准备日志,为此您需要执行以下步骤

  • 收集容器日志并用给定的标签标记它们。
  • 使用 rewrite_tag_filter 将日志分类为 JSON 和无 JSON 日志。和正则表达式。
  • 使用 cri 解析 JSON 日志
  • 解析无 JSON 日志

配置示例(未测试)

## collect row logs from files 
<source>
  @type tail
  @id in_tail_container_logs
  path /var/log/containers/*.log
  pos_file /var/log/fluentd-containers.log.pos
  tag kubernetes.*
  exclude_path "#{ENV['FLUENT_CONTAINER_TAIL_EXCLUDE_PATH'] || use_default}"
  read_from_head true
  format json
</source>

# add metadata to the records (container_name, image etc..)
<filter kubernetes.**>
  @type kubernetes_metadata
</filter>

# classify the logs to different categories 
<match kubernetes.**>
 @type rewrite_tag_filter
 <rule>
   key message
   pattern /^\{(.+_\}$/
   tag json.$1
 </rule>
 <rule>
   key message
   pattern ^\{(.+_\}$
   tag nonejson.$1
   invert true
 </rule>
</match>

# filter or match logs that match the json tag
<filter json.**>
</filter>
<match json.**>
</match>

# filter or match logs that match the none json tag
<filter nonejson.**>
</filter>
<match nonejson.**>
</match>
相关问题