【问题标题】:Parsing JSON event in Logstash在 Logstash 中解析 JSON 事件
【发布时间】:2019-03-08 11:06:57
【问题描述】:

我有以下格式的登录,它是一个带有嵌套字段的普通 json。

{
    "level": "info",
    "message": {
        "req": {
            "headers": {
                "host": "localhost:8080",
                "connection": "keep-alive",
                "x-forwarded-for": "192.168.1.1, 1.1.1.1",
                "x-forwarded-proto": "http"
            },
            "url": "/products?userId=493d0aec-a9a7-42a3",
            "method": "GET",
            "originalUrl": "/products?userId=493d0aec-a9a7-42a3",
            "params": {
                "0": "/products"
            },
            "query": {
                "userId": "493d0aec-a9a7-42a3"
            },
            "body": ""
        },
        "res": {
            "headers": {
                "traceid": "ac586e4e924048",
                "x-correlation-id": "57d7920d-b623-48f8",
                "content-type": "application/json;charset=UTF-8",
                "content-length": "2",
                "date": "Fri, 08 Mar 2019 09:55:45 GMT",
                "connection": "close"
            },
            "statusCode": 200,
            "body": "[]"
        },
        "gateway": "internal"
    },
    "correlationId": "57d7920d-b623-48f8",
    "timestamp": "2019-03-08T09:55:45.833Z"
}

如何使用 Filebeat 和 Logstash 正确解析它以将 Kibana 中的所有 json 字段视为单独(已解析)字段?我对具有嵌套 json 字段的“消息”字段有疑问。我可以毫无问题地解析在“消息”中有字符串但不是 json 的事件。

我的尝试:

1 .我试图告诉 Filebeat 它是一个具有以下配置的 json:
(并且在 LS 方面什么都不做)

filebeat.inputs:
- type: stdin
  json.keys_under_root: true
  json.add_error_key: true

结果对我来说很奇怪,因为我在 Kibana 中将“消息”作为字符串,其中所有 : 都替换为 =>

{
    "req" => {
        "originalUrl" => "/offers", "params" => {
            "0" => "/offers"
        }, "query" => {}, "body" => "", "headers" => {
            "accept-encoding" => "gzip", "user-agent" => "okhttp/3.8.1", "x-consumer-id" => "f2a6e4cd-2224-4535

“消息”之外的其他字段被正确解析

2 。我在 Filebeat 方面什么也没做,在 LS 中使用过滤器:

json {   
    source => "message"    
    target => "message_json"    
} 

Kibana 中根本没有出现日志,我在 LS 中出现以下错误:

[2019-03-08T09:55:47,084][WARN][logstash.outputs.elasticsearch] 可以 不将事件索引到 Elasticsearch。 {:status=>400, :action=>["index", {:_id=>nil, :_index=>"filebeat-6.5.0-2019.03.08-sdx", :_type=>"doc", :routing=>nil}, #], :response=>{"index"=>{"_index"=>"filebeat-6.5.0-2019.03.08-sdx", "_type"=>"doc", "id"=>"ERS6XGkBgE-US7A6Mvt", "status"=>400, "error"=>{"type"=>"mapper_parsing_exception", "reason"=>"失败 解析 [keyword] 类型的字段 [json.message]", "caused_by"=>{"type"=>"illegal_state_exception", "reason"=>"拿不到 START_OBJECT 上的文本在 1:461"}}}}} [2019-03-08T09:55:47,085][WARN ][logstash.outputs.elasticsearch] 无法将事件索引到 弹性搜索。 {:status=>400, :action=>["index", {:_id=>nil, :_index=>"filebeat-6.5.0-2019.03.08-sdx", :_type=>"doc", :routing=>nil}, #], :response=>{"index"=>{"_index"=>"filebeat-6.5.0-2019.03.08-sdx", "_type"=>"doc", "id"=>"EhS6XGkBgE-US7A6Mvt", "status"=>400, "error"=>{"type"=>"mapper_parsing_exception", "reason"=>"失败 解析 [keyword] 类型的字段 [json.message]", "caused_by"=>{"type"=>"illegal_state_exception", "reason"=>"拿不到 1:461 START_OBJECT 上的文本"}}}}}

如果“消息”字段是字符串(不是 json),这个过滤器对我来说很好。

任何想法如何解析“消息字段”中的嵌套 json?

【问题讨论】:

    标签: json elasticsearch logstash elastic-stack filebeat


    【解决方案1】:

    我在通过 logstash 解析 json 时遇到问题。

    我为这个问题苦苦挣扎了一段时间。并未能解决到logstash。

    但幸运的是,我们在 elasticsearch 本身中有 ingested node

    我想就您的问题提出我的解决方案:

    你制作管道(非常简单的管道):

    {
        "description": "Parse JSON log",
        "processors": [
          {
            "json": {
              "field": "message",
              "target_field": "message-json"
            }
          },
          {
            "remove": {
              "field": "message"
            }
          },
          {
            "append": {
              "field": "tags",
              "value": [
                "isjsonlog"
              ]
            }
          }
        ],
        "on_failure": [
          {
            "append": {
              "field": "tags",
              "value": [
                "nonjsonlog"
              ]
            }
          }
        ]
      }
    

    在你的输出插件中,你配置:

    elasticsearch {
        hosts => [ localhost ]
        index => "filebeat-%{+YYYY.MM.dd}"
        manage_template => false
        pipeline => "your_pipeline_name"
      }
    

    你忘记了 json 解析的问题。

    如果通过配置filebeat使用filebeatyou able to send json logs direct to pipeline

     output.elasticsearch:
        ....
        pipelines:
           - pipeline: "your pipeline name"
              when:
                contains:
                   message: "{"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-29
      相关资源
      最近更新 更多