Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prometheus counters: How to get current value with golang client?

Tags:

go

prometheus

I am using counters to count the number of requests. Is there any way to get current value of a prometheus counter?

My aim is to reuse existing counter without allocating another variable.

Golang prometheus client version is 1.1.0.

like image 586
Prisacari Dmitrii Avatar asked Sep 16 '19 08:09

Prisacari Dmitrii


People also ask

What is Promauto?

Overview. Package promauto provides alternative constructors for the fundamental Prometheus metric types and their … Vec and …Func variants. The difference to their counterparts in the prometheus package is that the promauto constructors return Collectors that are already registered with a registry.

How do Prometheus counters work?

Prometheus' rate function calculates at what rate the counter increases per second over a defined time window. The following PromQL expression calculates the per-second rate of job executions over the last minute². Our job runs at a fixed interval, so plotting the above expression in a graph results in a straight line.

What is Go_gc_duration_seconds?

go_gc_duration_seconds – calls out to debug.ReadGCStats() with PauseQuantile set to 5, which returns us the minimum, 25%, 50%, 75%, and maximum pause times. Then it manualy creates a Summary type from pause quantiles, NumGC var and PauseTotal seconds. It's cool how well GCStats struct fits the prom's Summary type. [

What is gauge in Prometheus?

A gauge is a metric that represents a single numerical value that can arbitrarily go up and down. Gauges are typically used for measured values like temperatures or current memory usage, but also "counts" that can go up and down, like the number of concurrent requests.


2 Answers

It's easy, have a function to fetch Prometheus counter value


import (
    "github.com/prometheus/client_golang/prometheus"
    dto "github.com/prometheus/client_model/go"
    "github.com/prometheus/common/log"
)

func GetCounterValue(metric *prometheus.CounterVec) float64 {
    var m = &dto.Metric{}
    if err := metric.WithLabelValues("label1", "label2").Write(m); err != nil {
        log.Error(err)
        return 0
    }
    return m.Counter.GetValue()
}
like image 54
Deep Nirmal Avatar answered Oct 17 '22 17:10

Deep Nirmal


Currently there is no way to get the value of a counter in the official Golang implementation.

You can also avoid double counting by incrementing your own counter and use an CounterFunc to collect it.

Note: use integral type and atomic to avoid concurrent access issues

// declare the counter as unsigned int
var requestsCounter uint64 = 0

// register counter in Prometheus collector
prometheus.MustRegister(prometheus.NewCounterFunc(
    prometheus.CounterOpts{
        Name: "requests_total",
        Help: "Counts number of requests",
    },
    func() float64 {
        return float64(atomic.LoadUint64(&requestsCounter))
    }))

// somewhere in your code
atomic.AddUint64(&requestsCounter, 1)
like image 5
Michael Doubez Avatar answered Oct 17 '22 17:10

Michael Doubez