如何在 logstash 中覆盖来自 json 的时间戳字段

how to override timestamp field coming from json in logstash

我有以下类型的 json 与我一起使用 filebeat {"@timestamp":"2017-02-10T06:30:51.424Z","beat":{"hostname":"myhostname","name":"mydevice-logger","version":"5.2.0"},"fields":{"device_type":"mydevice","env":"staging"},"metricset":{"module":"system","name":"cpu","rtt":211},"system":{"cpu":{"cores":4,"idle":{"pct":0.000000},"iowait":{"pct":0.000000},"irq":{"pct":0.000000},"nice":{"pct":0.000000},"softirq":{"pct":0.000000},"steal":{"pct":0.000000},"system":{"pct":0.000000},"user":{"pct":0.000000}}},"tags":["automata","box"],"type":"metricbeat-test-log"}

在弹性搜索中转储

我的 logstash(版本 5.1.1)配置包含如下所示的输入、过滤器和输出 -

input { 
  beats {
        port => 5046
        codec => json
  }
}

filter {
    if ...{}
    else if [type] == "metricbeat-test-log" {

      date {
        match => ["@timestamp", "ISO8601"]
      }

      }
    }

}


output {
    if ...{}
    else if [type] == "metricbeat-test-log" {
        stdout { codec => rubydebug   }
    }
} 

类型正确,但日期过滤器不起作用。 @timestamp 最终始终采用当前时间戳。我想用 json 中的原始 @timestamp 替换它。

您应该在日期过滤器中使用目标设置:

来自https://www.elastic.co/guide/en/logstash/5.1/plugins-filters-date.html#plugins-filters-date-target

目标

值类型为字符串

默认值为“@timestamp”

将匹配的时间戳存储到给定的目标字段中。如果未提供,则默认更新事件的@timestamp 字段。

我在这个旧线程 https://discuss.elastic.co/t/replace-timestamp-with-actual-timestamp-from-log-file/72017 之后得到了答案,它解释了上面日志行中的 "@timestamp":"2017-02-10T06:30:51.424Z" 是 @timestamp 字段及其值的 JSON 表示。按照建议,我在 filebeat 中添加了 json 配置,它对我有用。

- input_type: log
  paths:
    - /var/logs/mylogs/*.log
  fields:
    environment: testing
  document_type: test-metric-document

  json.keys_under_root: true   # added this line 
  json.overwrite_keys: true    # added this line 

虽然我对这个解决方案不满意,因为我真正需要的是同时获取时间戳、logstash 事件(在其他一些变量中)和登录@timestamp 的时间戳。