Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How update counter: set new value after avery request, NOT increment new value to previous value?

want to create Prometheus client by python

Tried to use this module

Took this code

from prometheus_client import Counter
c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
c.labels(method='get', endpoint='/').inc()
c.labels(method='post', endpoint='/submit').inc()

All good, BUT I want to set new value after avery request, NOT increment new value to previous value.

How can make it?

like image 937
Nikolay Baranenko Avatar asked Oct 21 '25 03:10

Nikolay Baranenko


1 Answers

You have to use a Gauge and not a Counter.

Example from the README:

from prometheus_client import Gauge
g = Gauge('my_inprogress_requests', 'Description of gauge')
g.inc()      # Increment by 1
g.dec(10)    # Decrement by given value
g.set(4.2)   # Set to a given value

See also Prometheus metric types: https://prometheus.io/docs/concepts/metric_types/

Counter

A counter is a cumulative metric that represents a single numerical value that only ever goes up. A counter is typically used to count requests served, tasks completed, errors occurred, etc. Counters should not be used to expose current counts of items whose number can also go down, e.g. the number of currently running goroutines. Use gauges for this use case.

Gauge

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 running goroutines.

like image 188
svenwltr Avatar answered Oct 23 '25 00:10

svenwltr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!