Promtail – 时间戳解析(日志时间戳与 Grafana 时间戳不匹配)

huangapple go评论90阅读模式
英文:

Promtail - timestamp parsing (not matching log timestamp and grafana ts)

问题

我一直在努力找到正确的格式来处理 promtail 配置中的时间戳。

我的日志格式如下:

2022-08-02 16:46:02.141 内容

我的 promtail 配置如下:

pipeline_stages:
  - multiline:
      firstline: '^\d{4}-\d{2}-\d{2}'
      max_wait_time: 10s
  - regex:
      expression: '(?P<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3})(.*)$'
  - labels:
      time:
  - timestamp:
      source: time
      format: 2006-01-02 15:04:05,999

使用这个配置,我在 Grafana 中仍然得到了一个错误的 "ts" 字段(该值与日志中的值不匹配)。
我不知道格式是否正确,我尝试了很多组合,但没有达到预期的效果。在 Go Playground 中,该值被解析为 "2022-08-02 16:46:02.141 +0000 UTC",没有出现错误 - 这个值是正确的吗?如何使 Grafana 中的 "ts" 值与日志中的值相同?

昨天我尝试了调试模式,在 promtail 日志中出现了以下错误:

level=error ts=2022-08-03T12:37:14.977444831Z caller=client.go:380 component=client host=localhost:3100 msg="final error sending batch" status=400 error="server returned HTTP status 400 Bad Request (400): entry for stream '{filename=\"/opt/loki/my.log\", job=\"varlogs\", stream=\"stdout\", time=\"2022-08-03 14:38:49,150\"}' has timestamp too new: 2022-08-03T14:38:49Z"

我以为 loki 可以处理这个问题(unordered_writes 默认为 true),这是我的格式问题还是 loki 配置的问题?

非常感谢任何帮助。

英文:

I've been struggling to get correct format for handling timestamp in promtail config.

Format of my log:

2022-08-02 16:46:02.141 content

My promtail config:

pipeline_stages:
  - multiline:
      firstline: &#39;^\d{4}-\d{2}-\d{2}&#39;
      max_wait_time: 10s
  - regex:
      expression: &#39;(?P&lt;time&gt;\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3})(.*)$&#39;
  - labels:
      time:
  - timestamp:
      source: time
      format: 2006-01-02 15:04:05,999

With this conf I'm still getting "ts" field in Grafana with incorrect value (the value doesn't match with the value from log).
I don't know if the format is correct, I tried many combinations but nothing worked as expected. In go playground the value was parsed as "2022-08-02 16:46:02.141 +0000 UTC" without errors - it it ok? How to make the "ts" value in grafana same as the value in log?

I tried debug mode yesterday and in promtail log was error like:

level=error ts=2022-08-03T12:37:14.977444831Z caller=client.go:380 component=client host=localhost:3100 msg=&quot;final error sending batch&quot; status=400 error=&quot;server returned HTTP status 400 Bad Request (400): entry for stream &#39;{filename=\&quot;/opt/loki/my.log\&quot;, job=\&quot;varlogs\&quot;, stream=\&quot;stdout\&quot;, time=\&quot;2022-08-03 14:38:49,150\&quot;}&#39; has timestamp too new: 2022-08-03T14:38:49Z&quot;

I thought that loki can handle this (unordered_writes as true by default), is this problem of my format or loki conf?

Any help would be greatly appreciated.

答案1

得分: 2

我明白了,这个链接 https://github.com/grafana/loki/issues/2639 对我帮助很大。关键在于时间戳阶段的位置。

 - timestamp:
     source: time
     format: 2006-01-02 15:04:05,999
     location: &quot;Europe/Berlin&quot;
英文:

I figured it out, this https://github.com/grafana/loki/issues/2639 helped me a lot. It was all about location in timestamp stage.

 - timestamp:
     source: time
     format: 2006-01-02 15:04:05,999
     location: &quot;Europe/Berlin&quot;

huangapple
  • 本文由 发表于 2022年8月3日 01:03:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/73211167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定