I'm trying to add my own timestamp instead of Prometheus's timestamp.
for ex:
node_value{endpoint="https",instance="xx.xx.xx.xx",job="prometheus,node="node1"}
489846 @1610014796.199
489933 @1610014826.199
Requirement: node_value(above) is a metric with two values and timestamp(Scrape timestamp added by prometheus), Instead of scrape timestamp I want to add my own timestamp which I'm fetching from third-party. Do we have the provision for that?
Note: I'm using golang prometheus client.
This is possible using NewMetricWithTimestamp provided in the prometheus golang client, but for exposing it, you have to do a little code.
At first, you have to write a new promehetus collector that implements Collector interface, then you provide your logic to set the custom timestamp for your metric.
Assume we are going to create a metric my_metric
that has custom timestamp, using a collector called 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)
}
Now if you check the localhost:2112/metrics
you saw this, with your desired timestamp:
.
.
.
# HELP my_metric This is my metric with custom TS
# TYPE my_metric counter
my_metric 123 1257894000012
.
.
.