使用 Prometheus Python 客户端时出现时间戳错误 - "Error on ingesting samples that are too old or are too far into the future"

Timestamps error while working with Prometheus Python client - "Error on ingesting samples that are too old or are too far into the future"

我正在尝试将性能测试结果历史记录导入 Prometheus,但在使用官方 Python Prometheus 客户端时遇到了一个奇怪的问题。

这样的代码可以正常工作:

dt_now = datetime.datetime.now(tz=pytz.timezone('UTC'))
gobj = GaugeMetricFamily('FooMetricGood', '')
gobj.add_metric([], 123, timestamp=dt_now.timestamp())
yield gobj

不要这样:

dt_format = '%Y-%m-%d_%H-%M-%S.%f %z'
dt_custom_str = '2021-11-11_18-12-59.000000 +0000'
dt_parsed_from_custom = datetime.datetime.strptime(dt_custom_str, dt_format)
gobj = GaugeMetricFamily('FooMetricNotWorking', '')
gobj.add_metric([], 789987, timestamp=dt_parsed_from_custom.timestamp())
yield gobj

我在 Prometheus 日志中收到如下警告:

prometheus-prometheus-1  | ts=2021-11-11T13:41:01.895Z caller=scrape.go:1563 level=warn component="scrape manager" scrape_pool=services target=http://192.168.64.1:8080/metrics msg="Error on ingesting samples that are too old or are too far into the future" num_dropped=1

大约 2 周前,我试图在 GitHub here 中用完整的工作代码示例和不工作的代码示例提出问题,但完全没有得到任何答案。

任何帮助将不胜感激。

更新 : 在回答和评论之后,我重新检查了一些细节。

如果我现在尝试来自 datetime 的两个指标(参见上面 GitHub link 的代码示例)和 datetime 取自字符串 '2021-12-13_00-34-59.000000 +0000' 所有它们在 Prometheus Python 客户端 Web 界面中显示为:

# HELP FooMetricGood 
# TYPE FooMetricGood gauge
FooMetricGood 123.0 1639475119451
# HELP FooMetricGoodToo 
# TYPE FooMetricGoodToo gauge
FooMetricGoodToo 456.0 1639475119451
# HELP FooMetricNotWorkingNew 
# TYPE FooMetricNotWorkingNew gauge
FooMetricNotWorkingNew 789987.0 1639355699000

但是在 Prometheus 服务器的日志中我看到:

prometheus-prometheus-1  | ts=2021-12-14T09:51:35.524Z caller=scrape.go:1611 level=debug component="scrape manager" scrape_pool=services target=http://192.168.64.1:8080/metrics msg="Out of bounds metric" series=FooMetricNotWorkingNew
prometheus-prometheus-1  | ts=2021-12-14T09:51:35.524Z caller=scrape.go:1563 level=warn component="scrape manager" scrape_pool=services target=http://192.168.64.1:8080/metrics msg="Error on ingesting samples that are too old or are too far into the future" num_dropped=1

据我所知,在 Prometheus 中 Python 时间戳以毫秒为单位,所以我比较了它们

FooMetricGood 1639475119451
FooMetricNotWorkingNew 1639355699000

并得到:

1639475119451 - 1639355699000 = 119420451 milliseconds = (119420451 / 1000 / 60 / 60) hours = 33.1723475

因此根据 Prometheus Python 当前时间仅在错误的指标时间戳后 33 小时。

我尝试调整日期并将其设置为 2021-12-14_08-34-59.000000 +0000,现在只差 1.2913288888888887 小时,但仍然无法正常工作。

我能够成功执行您的代码并在 prometheus 中查询指标: https://replit.com/@pygeek1/ComplicatedAcidicAxis#main.py

我怀疑 Prometheus 服务器有问题。确保服务器时间设置正确。 1 年前曾报告过类似问题,原因是服务器时间不断变化:https://github.com/prometheus/prometheus/issues/6554

看来我找到了麻烦的根源。

我找到了 Prometheus 服务器设置:

  scrape_interval: 1m
  scrape_timeout: 10s

是他们的描述:

Every 5 minutes (scrape_interval) Prometheus will get the metrics from the given URL. It will try 30 seconds (scrape_timeout) to get the metrics if it can't scrape in this time it will time out.

因此,即使我的指标只有一小时 - 对于服务器来说仍然太旧了。

现在的问题是你不能设置 scrape_timeout 多于 scrape_interval。因此,对于一个月前的数据,我似乎需要等待一个月才能让服务器收集这些指标。

我厌倦了普罗米修斯,我读过的大多数消息来源都说很难将历史导入其中,所以我切换到 VictoriaMetrics 并使用相同的普罗米修斯 Python 客户端并通过例如,将 VictoriaMetrics 选项 -search.cacheTimestampOffset 增加到 10080m0s(1 周)我很容易导入测试数据集来证明这是可能的。