重写 @timestamp 以在索引名称中获得正确的 %{+yyyy.MM.dd}

Override @timestamp to get correct correct %{+yyyy.MM.dd} in index name

我让 Filebeat 逐行读取 json 个文件,在每个 json 事件中,我已经有 timestamp 字段(格式:2021-03-02T04: 08:35.241632)

处理后,有一个新字段@timestamp(可能是Filebeat添加的元字段,等于当前时间),似乎索引模式%{+yyyy.MM.dd}https://www.elastic.co/guide/en/beats/filebeat/current/elasticsearch-output.html#index-option-es) was configured to that field. I want to override @timestamp with timestamp processor: https://www.elastic.co/guide/en/beats/filebeat/current/processor-timestamp.html但不起作用,可能是布局设置不正确?

- timestamp:
      field: timestamp
      layouts:
        - '2021-03-02T03:29:29.787331'
2021-03-07T11:29:39.382+0700    DEBUG   [processor.timestamp]   timestamp/timestamp.go:173  Failure parsing time field. {"error": "failed parsing time field timestamp='2021-03-02T03:29:29.787331'", "errorCauses": [{"error": "failed using layout [2021-03-02T03:29:29.787331] cannot parse [-03-02T03:29:29.787331] as [1]"}]}

我还尝试了另一种使用 Date.parse 解析时间戳的方法,但没有用,不确定 Filebeat 中实现的 ECMA 5.1 是否遗漏了什么:

var date = new Date.parse('2021-03-02T03:29:29.787331');
    event.Put('@metadata.index_suffix', date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate());
  "error": {
    "message": "TypeError: Not a constructor at process (/Users/thoong/Projects/shodan/shodan-trends/transform.js:25:16(106))"
  },

所以我的 timestamp 格式是 2021-03-02T03:29:29.787331,我想问一下处理器的正确 layouts 是什么,或者用 Date.parse 解析?

解决方案,这是来自@Val 答案的正确布局

- timestamp:
      field: timestamp
      layouts:
        - '2006-01-02T15:04:05.000000'

如果您的 timestamp 字段已经具有 ISO8601 格式,则无需指定 layouts 参数。

但是,如果您的 timestamp 字段有不同的布局,您必须在布局部分指定非常具体的参考日期,即 Mon Jan 2 15:04:05 MST 2006,您还可以提供 test 日期。 (more info)

所以在你的情况下它将是:

- timestamp:
    field: timestamp
    ignore_missing: true
    ignore_failure: true
    layouts:
      - '2006-01-02T15:04:05.000000'
    test:
      - '2021-03-02T03:29:29.787331'