【问题标题】:json array parsing issue with logstashlogstash 的 json 数组解析问题
【发布时间】:2017-02-06 07:46:16
【问题描述】:

我们希望使用 JSON Array 格式的 logstash 的 http 插件来实现服务请求跟踪。

我们在尝试解析 JSON 数组时遇到以下错误:

错误:

:message=>"gsub mutation is only applicable for Strings, skipping", :field=>"message", :value=>nil, :level=>:debug, :file=>"logstash/filters/mutate.rb", :line=>"322", :method=>"gsub"}
:message=>"Exception in filterworker", "exception"=>#<LogStash::ConfigurationError: Only String and Array types are splittable. field:message is of type = NilClass>

我的 json 数组是:

{
    "data": [
        {
            "appName": "DemoApp",
            "appVersion": "1.1",
            "deviceId": "1234567",
            "deviceName": "moto e",
            "deviceOSVersion": "5.1",
            "packageName": "com.DemoApp",
            "message": "testing null pointer exception",
            "errorLog": "null pointer exception"
        },
        {
            "appName": "DemoApp",
            "appVersion": "1.1",
            "deviceId": "1234567",
            "deviceName": "moto e",
            "deviceOSVersion": "5.1",
            "packageName": "com.DemoApp",
            "message": "testing illegal state exception",
            "errorLog": "illegal state exception"
        }
    ]
}

我的 logstash 配置是:

    input {
            http {
            codec => "plain"
            }
    }
    filter{
            json {
                  source => "message"
                 }
            mutate { gsub => [ "message", "},", "shr" ] }
            split {
                  terminator => "shr"
                  field => "data"
           }
    }
    }


output {
 stdout { codec => "json" }
    gelf{
        host => localhost
        facility => "%{type}"
        level =>["%{SeverityLevel}", "INFO"]
        codec => "json"
    }
       file{
        path => "/chroot/result.log"
}
}

任何帮助将不胜感激。

【问题讨论】:

  • 我认为你会在正则表达式中转义特殊字符:mutate { gsub =&gt; [ "message", "\}\,\r\n\r\n\{", "\}shr\{" ] }
  • @Kadir 我也尝试了您的上述建议。但我仍然遇到同样的错误。

标签: json logstash graylog


【解决方案1】:

Logstash 有一个名为 message 的默认元数据字段。因此,您的 json message 字段与该字段重叠。考虑将 json 字段名称 message 更改为另一个。

另一个选项可能使用target 设置并引用目标字段,例如:

json { source => "message" target => "data"}
mutate { gsub => [ "[data][message]", "\}\,\r\n\r\n\{", "\}shr\{" ] }

我希望这会有所帮助。

【讨论】:

  • @kair 也尝试了上述配置...仍然遇到相同的错误。 {:timestamp=&gt;"2017-02-07T11:02:00.192000+0530", :message=&gt;"gsub mutation is only applicable for Strings, skipping", :field=&gt;"[data][message]", :value=&gt;nil, :level=&gt;:debug, :file=&gt;"logstash/filters/mutate.rb", :line=&gt;"322", :method=&gt;"gsub"}
  • 好的,我现在可以拆分我的数据了……已经相应地编辑了我的配置。我现在面临一个新问题。我的字段被重命名为:data_appName,而不仅仅是 appName。关于如何避免这种情况并获得所需字段名称的任何建议。我也尝试了以下方法来实现它:` mutate { rename => {"data_appName" => "appName"} }` 但仍然没有积极的输出。
  • 据我所知mutate { rename =&gt; {"[data][appName]" =&gt; "appName"} } 应该可以工作。更好的方法是像上面那样重命名所有字段,然后删除 data 字段。 mutate { rename =&gt; [ "[data][appName]", "appName", "[data][appVersion]", "appVersion", .. .. ] remove_field =&gt; "data" }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-22
  • 2021-07-03
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
相关资源
最近更新 更多