我正在尝试添加自己的时间戳,而不是Prometheus的时间戳。
例如:
node_value {endpoint =“ https”,instance =“ xx.xx.xx.xx”,job =“ prometheus,node =” node1“}
489846 @ 1610014796.199
489933 @ 1610014826.199
要求:node_value(above)是一个具有两个值和时间戳的度量标准(Scrope时间戳由prometheus添加),而不是scrape时间戳,我想添加自己的时间戳,该时间戳是我从第三方获取的。我们有准备吗?
注意:我使用的是golang prometheus客户端。
这是可能的使用NewMetricWithTimestamp在普罗米修斯golang客户提供的,但对揭露它,你必须做一些代码。
首先,您必须编写一个新的promehetus收集器,该收集器实现了Collector接口,然后提供逻辑来设置度量标准的自定义时间戳。
假设我们将my_metric
使用称为的收集器来创建具有自定义时间戳记的指标myCollector
:
package main
import (
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
)
type myCollector struct {
metric *prometheus.Desc
}
func (c *myCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.metric
}
func (c *myCollector) Collect(ch chan<- prometheus.Metric) {
// your logic should be placed here
t := time.Date(2009, time.November, 10, 23, 0, 0, 12345678, time.UTC)
s := prometheus.NewMetricWithTimestamp(t, prometheus.MustNewConstMetric(c.metric, prometheus.CounterValue, 123))
ch <- s
}
func main() {
collector := &myCollector{
metric: prometheus.NewDesc(
"my_metric",
"This is my metric with custom TS",
nil,
nil,
),
}
prometheus.MustRegister(collector)
http.Handle("/metrics", promhttp.Handler())
log.Info("Beginning to serve on port :8080")
http.ListenAndServe(":2112", nil)
}
现在,如果您选中,localhost:2112/metrics
则会看到此时间戳以及所需的时间戳:
.
.
.
# HELP my_metric This is my metric with custom TS
# TYPE my_metric counter
my_metric 123 1257894000012
.
.
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句