【问题标题】:Explode json object into multiple fields with ElasticSearch and Logstash使用 ElasticSearch 和 Logstash 将 json 对象分解为多个字段
【发布时间】:2018-06-17 23:02:37
【问题描述】:

继续我与 ElasticSearch 的爱恨交织,我正在尝试将信息从 pm2 (nodejs) 记录到我的弹性搜索集群。

当前设置如下:

Pm2 管理一些节点应用程序,它被配置为登录到 json。这是一个例子:

{"message":"{\n  \"className\": \"AppointmentBot\",\n  \"dialog\": \"query\",\n  \"step\": 2,\n  \"millis\": 1497,\n  \"extra\": \"\",\n  \"type\": \"logger\",\n  \"level\": \"info\",\n  \"message\": \"Client\",\n  \"timestamp\": \"08-01-2018 22:44:22 +00:00\"\n}\n","timestamp":"2018-01-08T22:44:22.350Z","type":"out","process_id":9,"app_name":"Chatbot-App-Dev"}

文件节拍配置

type: log
  enabled: true
  paths:
    /home/app/.pm2/logs/*.log
  fields:
    type: pm2

Logstash 配置:

if [fields][type] == "pm2" {
        json {
            source => "message"
            remove_field => ["message"]
        } 

        date {
            match => [ "timestamp", "yyyy-MM-dd HH:mm:ssZ" ]
            tag_on_failure => ["_dateparsefailure"]
        }
    }

这会在 Kibana/ElasticSearch 中产生类似的结果:

{
  "className": "AppointmentBot",
  "dialog": "query",
  "step": 2,
  "millis": 1497,
  "extra": "",
  "type": "logger",
  "level": "info",
  "message": "Client",
  "timestamp": "08-01-2018 22:44:22 +00:00"
}

现在,我想要的是让这些 JSON 字段不在一个字段中,而是每个字段都在一个字段中。

一些注意事项:

  • 我不想使用硬编码的变异,我认为这可以解决,因为我不想硬编码字段。有时对象可能不同。

基本上,我想解析 json,每个字段将其添加为索引中的新字段。可以是结构或根中的所有字段,我不介意。

【问题讨论】:

    标签: elasticsearch logstash pm2 filebeat


    【解决方案1】:

    在 Elastic 支持的帮助下,我终于解决了这个问题。问题是,如果你想引用 logstash 配置中的字段,你需要使用 [A][B] 表示法。基本上,我的第二个过滤器没有运行,因为我使用的是 pm2.message。

    这是我的最终配置,有效:

    json {
                source => "message"
                target => "[pm2]"
                remove_field => "message"
                skip_on_invalid_json => true
            }
    
            if "_jsonparsefailure" not in [tags]  {
                json {
                    source => "[pm2][message]"
                    target => "[pm2][data]"
                    remove_field => "[pm2][message]"
                    skip_on_invalid_json => true
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      • 2021-08-26
      • 1970-01-01
      • 2016-09-05
      相关资源
      最近更新 更多