我们如何在普罗米修斯度量序列中添加自己的时间戳?

我正在尝试添加自己的时间戳,而不是普罗米修斯的时间戳。

例如:

node_value{endpoint=“https”,instance=“xx.xx.xx.xx”,job=“prometheus,node=”node1“}

489846 @1610014796.199

489933 @1610014826.199

要求:node_value(上图)是一个具有两个值和时间戳的指标(由prometheus添加的刮擦时间戳),而不是刮取时间戳,我想添加我自己的时间戳,我从第三方获取。我们对此有规定吗?

注意:我正在使用golang prometheus客户端。


犯罪嫌疑人X
浏览 257回答 1
1回答

MMMHUHU

使用prometheus golang客户端中提供的NewMetricWithTimestamp,这是可能的,但是为了公开它,你必须做一些代码。首先,您必须编写一个新的 promehetus 收集器来实现收集器接口,然后提供逻辑来设置指标的自定义时间戳。假设我们将使用名为 :my_metricmyCollectorpackage mainimport (&nbsp; &nbsp; "net/http"&nbsp; &nbsp; "time"&nbsp; &nbsp; "github.com/prometheus/client_golang/prometheus"&nbsp; &nbsp; "github.com/prometheus/client_golang/prometheus/promhttp"&nbsp; &nbsp; "github.com/prometheus/common/log")type myCollector struct {&nbsp; &nbsp; metric *prometheus.Desc}func (c *myCollector) Describe(ch chan<- *prometheus.Desc) {&nbsp; &nbsp; ch <- c.metric}func (c *myCollector) Collect(ch chan<- prometheus.Metric) {&nbsp; &nbsp; // your logic should be placed here&nbsp; &nbsp; t := time.Date(2009, time.November, 10, 23, 0, 0, 12345678, time.UTC)&nbsp; &nbsp; s := prometheus.NewMetricWithTimestamp(t, prometheus.MustNewConstMetric(c.metric, prometheus.CounterValue, 123))&nbsp; &nbsp; ch <- s}func main() {&nbsp; &nbsp; collector := &myCollector{&nbsp; &nbsp; &nbsp; &nbsp; metric: prometheus.NewDesc(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "my_metric",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "This is my metric with custom TS",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; }&nbsp; &nbsp; prometheus.MustRegister(collector)&nbsp; &nbsp; http.Handle("/metrics", promhttp.Handler())&nbsp; &nbsp; log.Info("Beginning to serve on port :8080")&nbsp; &nbsp; http.ListenAndServe(":2112", nil)}现在,如果您检查您看到的这个,以及您想要的时间戳:localhost:2112/metrics...# HELP my_metric This is my metric with custom TS# TYPE my_metric countermy_metric 123 1257894000012...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go