Set Counter Value to 0

617 views
Skip to first unread message

Nipuni

unread,
Jul 7, 2020, 2:51:15 PM7/7/20
to Prometheus Users
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").labelNames(labels).register();
Gauge gauge = (Gauge) map.get(SERVER_UP);
gauge.labels(serviceName, serviceType).set(0);

How can this be done with Counters ?

Julien Pivotto

unread,
Jul 7, 2020, 5:06:03 PM7/7/20
to Nipuni, Prometheus Users
On 07 Jul 11:51, Nipuni wrote:
> 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").labelNames(labels).register();*
>
> * Gauge gauge = (Gauge) map.get(SERVER_UP);*
> * gauge.labels(serviceName, serviceType).set(0);*
>
>
> How can this be done with Counters ?

Just call the .labels() function without .inc()


>
> --
> You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/f3d920aa-31a7-4446-ab2d-c7b5d3fddd50o%40googlegroups.com.


--
Julien Pivotto
@roidelapluie

Nipuni Chandimali

unread,
Jul 8, 2020, 7:35:06 AM7/8/20
to Nipuni, Prometheus Users
Hi Julien,

Thank you very much for the response. I used the following code segment.

I register the Counter like this
static final Counter TOTAL_REQUESTS_RECEIVED = Counter.build(MetricConstants.PROXY_REQUEST_COUNT_TOTAL,"Total no.of requests").labelNames("test").register();

and use the following method in the server shutdown method.
TOTAL_REQUESTS_RECEIVED.labels();
But still the previous counter values are not set to 0.
Please help me to solve this. 
Thanks.

Julien Pivotto

unread,
Jul 8, 2020, 7:38:31 AM7/8/20
to Nipuni Chandimali, Prometheus Users
You did not pass values to labels (). You must give it the label value.

Message has been deleted

Arshad Ali Sayed

unread,
Jun 15, 2021, 2:24:26 AM6/15/21
to Prometheus Users
Hello All,

I am facing similar issue when using counters with labels. I have counters to track some events that only increases over time, like no of users submitted a form. These forms belongs to a year.
I want to track no of forms submitted for year, say 2019/2020/2021 etc. I am using labeled counter to track this.

I am using increase function to track no of forms submitted for particular year, in a given time range like increase( forms_submitted { year=2019 } [ 2h ] ).

Below is the observation
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.
Internally for labeled counters, there are independent counters (children).
What if we have initialise() method in Builder, which initialises the value of the counter for all labels while creating labeled counter ?

Bjoern Rabenstein

unread,
Jun 18, 2021, 6:40:45 PM6/18/21
to Arshad Ali Sayed, Prometheus Users
On 14.06.21 23:24, Arshad Ali Sayed wrote:
> What if we have initialise() method in Builder, which initialises the value
> of the counter for all labels while creating labeled counter ?

This should already be possible in all instrumentation libraries. as
Julien wrote earlier in the thread: "Just call the .labels() function
without .inc()"

--
Björn Rabenstein
[PGP-ID] 0x851C3DA17D748D03
[email] bjo...@rabenste.in
Reply all
Reply to author
Forward
0 new messages