Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we add our own timestamp in Prometheus metric series?

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.

like image 243
akshay sharma Avatar asked Oct 30 '25 20:10

akshay sharma


1 Answers

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
.
.
.
like image 191
meshkati Avatar answered Nov 02 '25 02:11

meshkati