Logstash 8.0@时间戳,精度为纳秒



我需要帮助来了解如何使用另一个字段的内容设置@timestamp字段,该字段包含纳秒精度的日期和时间。我试过使用日期匹配,但我不清楚范围必须使用哪种模式:

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

其中timestamp_nano是具有纳秒精度的时间戳(例如"2022-01-20T12:00:0.123456789Z">(

通过使用ISO8601,精度为毫秒,其余数字不会在@timestamp字段中报告。

比赛中使用哪种模式?

提前感谢

这个答案适用于8.0.0-rc1,因为8.0还没有发布。日期过滤器仍在使用Joda库,该库的精度仅限于毫秒。引入纳秒精度的PR修复了底层LogStash Timestamp类,使其能够使用纳秒精度,但不是所有使用纳秒精度的类。

如果我运行

input { generator { count => 1 lines => [ '' ] } }
output { stdout { codec => rubydebug { metadata => false } } }

然后我得到@timestamp 的微秒精度

"@timestamp" => 2022-01-22T01:14:15.881459Z,

然而,时间戳可能比这更准确。如果我使用json编解码器

input { generator { count => 1 lines => [ '{ "@timestamp": "2022-01-10T12:13:14.123456789"}' ] codec => json } }
output { stdout { codec => rubydebug { metadata => false } } }

我得到纳秒精度的

"@timestamp" => 2022-01-10T17:13:14.123456789Z,

如果你想使用另一个字段的值,你可以做一些类似的事情

input { generator { count => 1 lines => [ '' ] } }
filter {
mutate { add_field => { "foo" => "2022-01-10T12:13:14.123456789" } }
mutate { add_field => { "bar" => '{ "@timestamp": "%{foo}" } ' } }
json { source => "bar" }
}

@badger上面的回答很好,但我更喜欢在Logstash 8.x 中使用Ruby的单步执行

input { generator { count => 1 lines => [ '' ] } }
filter {
mutate { add_field => { "[@timestamp_nanoseconds]" => "2022-08-11T10:10:10.123456789Z" } }
ruby {
code => "event.set('[@timestamp]', LogStash::Timestamp.new(event.get('[@timestamp_nanoseconds]')) )"
}
}
output { stdout {} }

我获得了纳秒精度的原生@时间戳

"@timestamp" => 2022-08-11T10:10:10.123456789Z,

我在Elasticsearch:中添加了一个管道,得到了相同的结果

{
"description": "mypipeline",
"processors": [
{
"date": {
"field": "timestamp_nano",
"formats": ["ISO8601"],
"output_format": "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSZ"
}
}
]
}

时间戳_ nano包括一个具有纳秒精度的时间戳。

在LogT中,@timestamp可以通过使用此处所示的ruby脚本来更新,它很有效,但作者表示该脚本无法优化。

BRs

最新更新