首页 > 解决方案 > 使用 Prometheus 指标存储时间戳

问题描述

我想制作一个自定义指标来将datetime.now存储在 Prometheus 指标中,有人知道怎么做吗?我试图用gauge来做,但设置的预定义函数似乎只适用于double

标签: prometheus

解决方案


以python为例:

import time
import prometheus_client as prom
g = prom.Gauge("name", "description")
g.set(time.time())
prom.write_to_textfile('metrics.txt', prom.REGISTRY)

'metrics.txt' 将包含以下内容:

# HELP name description
# TYPE name gauge
name 1.6287478272555726e+09

指标的值和time.time()两者都是纪元时间(又名unixtime),就像 Prometheustime()函数一样。


推荐阅读