MMMHUHU
使用prometheus golang客户端中提供的NewMetricWithTimestamp,这是可能的,但是为了公开它,你必须做一些代码。首先,您必须编写一个新的 promehetus 收集器来实现收集器接口,然后提供逻辑来设置指标的自定义时间戳。假设我们将使用名为 :my_metricmyCollectorpackage mainimport ( "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 countermy_metric 123 1257894000012...