使用 Filebeat 7.9.3 正确解析 k8s docker 容器 json 日志

问题描述 投票:0回答:2

我正在使用 Filebeat 7.9.3 作为 k8s 上的守护进程集。 我无法解析以 json 格式将日志写入 stdout 的 Springboot 应用程序的 docker 容器日志。 事实上,Springboot应用程序日志的每一行都是这样写的:

{ "@timestamp": "2020-11-16T13:39:57.760Z", "log.level": "INFO", "message": "Checking comment 'se' done = true", "service.name": "conduit-be-moderator", "event.dataset": "conduit-be-moderator.log", "process.thread.name": "http-nio-8081-exec-2", "log.logger": "it.koopa.app.ModeratorController", "transaction.id": "1ed5c62964ff0cc2", "trace.id": "20b4b28a3817c9494a91de8720522972"} 

但是/var/log/containers/下对应的docker日志文件是这样写日志的:

{
"log": "{\"@timestamp\":\"2020-11-16T11:27:32.273Z\", \"log.level\": \"INFO\", \"message\":\"Checking comment 'a'\", \"service.name\":\"conduit-be-moderator\",\"event.dataset\":\"conduit-be-moderator.log\",\"process.thread.name\":\"http-nio-8081-exec-4\",\"log.logger\":\"it.koopa.app.ModeratorController\",\"transaction.id\":\"9d3ad972dba65117\",\"trace.id\":\"8373edba92808d5e838e07c7f34af6c7\"}\n",
"stream": "stdout",
"time": "2020-11-16T11:27:32.274816903Z" 
} 

我总是在 filebeat 日志上收到此信息

Error decoding JSON: json: cannot unmarshal number into Go value of type map[string]interface {}

这是我的 filebeat 配置,它尝试从 docker 日志中解析 json 日志消息,其中我使用decode_json_fields 来尝试捕获 Elasticsearch 标准字段(我正在使用 co.elastic.logging.logback.EcsEncoder

filebeat.yml: |-
filebeat.inputs:
- type: container

  #json.keys_under_root: true
  json.overwrite_keys: true
  json.add_error_key: true
  json.message_key: log

  paths:
    - /var/log/containers/*.log

  include_lines: "conduit-be-moderator"

  processors:
    - decode_json_fields:
        fields: ["log"]
        overwrite_keys: true
    - add_kubernetes_metadata:
        host: ${NODE_NAME}
        in_cluster: true
        matchers:
          - logs_path:
              logs_path: "/var/log/containers/"

processors:
  - add_cloud_metadata:
  - add_host_metadata:

我该怎么办???

json elasticsearch kubernetes filebeat
2个回答
2
投票

由于处理器在输入的 JSON 解析器之前应用,因此您需要首先配置

decode_json_fields
处理器,这将允许您解码 json.log 字段。然后,您将能够将 json 配置应用于
message
字段的输入。比如:

filebeat.yml: |-
filebeat.inputs:
- type: container

  json.keys_under_root: true
  json.overwrite_keys: true
  json.add_error_key: true
  json.message_key: message

  paths:
    - /var/log/containers/*.log

  include_lines: "conduit-be-moderator"

  processors:
    - decode_json_fields:
        fields: ['log']
        expand_keys: true
    - add_kubernetes_metadata:
        host: ${NODE_NAME}
        in_cluster: true
        matchers:
          - logs_path:
              logs_path: "/var/log/containers/"

processors:
  - add_cloud_metadata:
  - add_host_metadata:

此配置假设您的所有日志都使用 JSON 格式。否则,您可能需要添加排除或包含正则表达式模式。


0
投票

字符串值末尾有

\n
,因此遇到了此错误。

https://github.com/elastic/beats/issues/20053#issuecomment-1899155624

© www.soinside.com 2019 - 2024. All rights reserved.