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.

Answer from svenwltr on Stack Overflow
🌐
Robust Perception
robustperception.io › setting-a-prometheus-counter
Setting a Prometheus Counter – Robust Perception | Prometheus Monitoring Experts
package main import "github.com/prometheus/client_golang/prometheus" type MyCollector struct { counterDesc *prometheus.Desc } func (c *MyCollector) Describe(ch chan<- *prometheus.Desc) { ch <- c.counterDesc } func (c *MyCollector) Collect(ch chan<- prometheus.Metric) { value := 1.0 // Your code to fetch the counter value goes here.
Discussions

Set Prometheus Counter value to 0 - Stack Overflow
I want to set Prometheus Counter value to 0 when the server restarts in a manner similar to, private static final Gauge SERVER_UP = Gauge.build(MetricConstants.SERVER_UP, "Server status"). More on stackoverflow.com
🌐 stackoverflow.com
Allow setting counter value when exposing existing counts
I am just started to use prometheus, and your library and have come across this issue. Counter only allows you to increase the value which is fine if you actually using it to count things but break... More on github.com
🌐 github.com
13
September 19, 2016
go - Prometheus counters: How to get current value with golang client? - Stack Overflow
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 More on stackoverflow.com
🌐 stackoverflow.com
How to take the previous counter value and add it to the new one in case it resets to zero on Grafana?
Typically, you would use rate, which accounts for counters resets. Is there any reason not to use rate? What's the use case, just out of curiosity? More on reddit.com
🌐 r/PrometheusMonitoring
8
2
July 21, 2023
Top answer
1 of 2
2

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.

2 of 2
1

Counter value can be set by accessing private _value property. I strongly recommend avoiding this solution, unless you know for sure that the values you are going to set will only increase and not decrease. Otherwise use Gauge instead.

from prometheus_client import Counter
c = Counter("foo", "bar")
c._value.set(50)
c._value.get()
50
c._value.set(101)
c._value.get()
101

# the same with labels
cd = Counter("bar", "foo", labelnames=['name'])
cd.labels(name="spam")._value.set(20)

What can go wrong with this approach

The main problem is the counter reset. Whenever PromQL functions (such as rate(), increase()) detect a counter decrease, it is considered as a reset. Consider this simple example:

my_metric            1 2 3 4 3
increase(my_metric)  0 1 1 1 3

So if you decrease a counter value, some functions will treat it as if counter became 0 and then instantly went up to whichever value it is now. This will ruin all calculations on that counter, especially if it's a big one (like sent bytes, requests handled, etc).

🌐
HexDocs
hexdocs.pm › prometheus_ex › Prometheus.Metric.Counter.html
Prometheus.Metric.Counter — Prometheus.ex v5.1.0
Raises Prometheus.UnknownMetricError exception if a counter for spec can't be found.<br> Raises Prometheus.InvalidMetricArityError exception if labels count mismatch. Resets the value of the counter identified by spec.
🌐
Stack Overflow
stackoverflow.com › questions › 62782007 › set-prometheus-counter-value-to-0
Set Prometheus Counter value to 0 - Stack Overflow
private static final Gauge SERVER_UP = Gauge.build(MetricConstants.SERVER_UP, "Server status").labelNames(labels).register(); Gauge gauge = (Gauge) map.get(SERVER_UP); gauge.labels(serviceName, serviceType).set(0);
🌐
Prometheus
prometheus.io › docs › concepts › metric_types
Metric types | Prometheus
In the future, Prometheus might ... is a cumulative metric that represents a single monotonically increasing counter whose value can only increase or be reset to zero on restart....
🌐
INNOQ
innoq.com › en › blog › 2019 › 05 › prometheus-counters
Prometheus Counters and how to deal with them – INNOQ
May 20, 2019 - We can add some tags (as they are called in Micrometer) or labels (the same concept in Prometheus) to our counter and set the appropriate attribute values every time we increase it.
🌐
GitConnected
levelup.gitconnected.com › prometheus-counter-metrics-d6c393d86076
Working With Prometheus Counter Metrics | Level Up Coding
February 28, 2022 - Plotting the raw counter value results in a line that only goes up. This line will just keep rising until we restart the application. When the application restarts, the counter is reset to zero. ... The counter is reset to zero when the application restarts. Lucky for us, PromQL (the Prometheus Query Language) provides functions to get more insightful data from our counters.
Find elsewhere
🌐
GitHub
github.com › prometheus-net › prometheus-net › issues › 32
Allow setting counter value when exposing existing counts · Issue #32 · prometheus-net/prometheus-net
September 19, 2016 - I am just started to use prometheus, and your library and have come across this issue. Counter only allows you to increase the value which is fine if you actually using it to count things but breaks down if you have an existing counter you are trying to expose though prometheus.
Author: prometheus-net
🌐
Google Groups
groups.google.com › g › prometheus-users › c › Wy4AwTklIs0
Set Counter Value to 0
First If we query prometheus, they query result is empty. On first inc(), it is incremented to 0. after second inc() the sum is 1. As a solution, I have initialised the counter to zero (by doing inc(0) ) for all labels in the application code as I know the labels in advance. If we can initialise the counter at the time of building it it could have solve the above issue. I know that counter can not have set() method.
🌐
Tom Gregory
tomgregory.com › the-four-types-of-prometheus-metrics
The 4 Types Of Prometheus Metrics | Tom Gregory
December 2, 2019 - Although we'll be looking at the Java version in this article, the concepts you'll learn will translate to the other languages too. The counter metric type is used for any value that increases, such as a request count or error count.
🌐
SigNoz
signoz.io › guides › how to manage prometheus counters - best practices for servers
How to Manage Prometheus Counters - Best Practices for Servers | SigNoz
July 25, 2024 - Monitor and troubleshoot: Set up alerts and analyze data regularly to ensure accuracy. ... Prometheus doesn't handle counter resets automatically. It's up to you to use functions like increase() or rate() to calculate meaningful values across resets.
🌐
client_python
prometheus.github.io › client_python › instrumenting › counter
Counter | client_python
April 9, 2026 - A Counter tracks a value that only ever goes up. Use it for things you count — requests served, errors raised, bytes sent. When the process restarts, the counter resets to zero. If your value can go down, use a Gauge instead. from prometheus_client import Counter c = Counter('my_failures', ...
🌐
OneUptime
oneuptime.com › home › blog › how to implement prometheus counter best practices
How to Implement Prometheus Counter Best Practices
January 30, 2026 - When your application restarts, the counter resets to zero. Prometheus handles this gracefully in rate() and increase() functions by detecting the reset and accounting for it. The key insight: raw counter values are rarely useful.
🌐
Reddit
reddit.com › r/prometheusmonitoring › how to take the previous counter value and add it to the new one in case it resets to zero on grafana?
r/PrometheusMonitoring on Reddit: How to take the previous counter value and add it to the new one in case it resets to zero on Grafana?
July 21, 2023 -

So I have a counter metric, I aggregate it by sum based on two label values. My question is, after the application restarts the counter is going to reset to zero, but on grafana I want to keep the counter persistent, meaning that when the counter becomes zero, I want to take the previous value and add it to the new counter value.

So if counter metric is 5.0, application restarts and now the counter metric is 0, I basically want to take previous value 5 and add it to the current value 0.

Does this make sense? I don't know how to do it.

🌐
Better Stack
betterstack.com › community › questions › how-to-manage-prometheus-counters
How To Manage Prometheus Counters | Better Stack Community
November 29, 2024 - from prometheus_client import Counter # Define a counter request_counter = Counter('http_requests_total', 'Total HTTP requests', ['method', 'endpoint']) The labels argument allows tracking metrics by specific dimensions. Increment counters using the .inc() method whenever an event occurs. You can increment by one or by a specific value:
🌐
Torsten Mandry
torstenmandry.github.io › Prometheus-Counters
Prometheus Counters and how to deal with them | Torsten Mandry
May 20, 2019 - We can add some tags (as they are called in Micrometer) or labels (the same concept in Prometheus) to our counter and set the appropriate attribute values every time we increase it.
🌐
Prometheus
prometheus.io › docs › tutorials › understanding_metric_types
Understanding metric types | Prometheus
Counter is a metric value that can only increase or reset i.e. the value cannot reduce than the previous value.
🌐
GitHub
github.com › prometheus › client_golang › blob › main › prometheus › counter.go
client_golang/prometheus/counter.go at main · prometheus/client_golang
The value reported is determined by calling the given function · // from within the Write method. Take into account that metric collection may · // happen concurrently. If that results in concurrent calls to Write, like in · // the case where a CounterFunc is directly registered with Prometheus, the ·
Author: prometheus