prom-client-net/prom-client

Option to reset counter

mwinkler opened this issue · 6 comments

Hi
I have a application that process data on a recurring base (scheduled task).
At the start i want to set/export all counter to 0.
If a counter has no label, its working fine (the counter gets exported with 0).
But I see no way to set a counter to 0, if it has a label.
Maybe a Reset() method on the counter could do that:

var counter = metricFactory.CreateCounter("myCounter", "help text", labelNames: new []{ "method", "endpoint"});
counter.WithLabels("GET", "/").Reset();

Thanks

Hi

If you are using v4 of the library you can just call WithLabels by providing your labels combination on start and it should be reported with 0 value.

var counter = metricFactory.CreateCounter("myCounter", "help text", labelNames: new []{ "method", "endpoint"}); counter.WithLabels("GET", "/");

Or if you want to have unlabelled sample reported - access to the Unlabelled property.

var counter = metricFactory.CreateCounter("myCounter", "help text", labelNames: new []{ "method", "endpoint"}); counter.Unlabelled;

@sanych-sun Yes, but we may reset counter any time, not just start from 0.

https://prometheus.io/docs/instrumenting/writing_clientlibs/#counter
Counter is a monotonically increasing counter. It MUST NOT allow the value to decrease, however it MAY be reset to 0 (such as by server restart).

On server (process) restart it will reset =). Do we need explicit API to reset the value? If we decide to move this way, I would prefer API to remove the sample from family instead. Because it will cover additional potential use-cases (create some sample for temporary job and remove it once the unit of work is completed). By removing sample we delete current value. If client's application want to have 0 value client's code should remove the sample and add new with the same labels again.

We might add it if there is any use-case for it.

@mwinkler we decided to move discussion regarding metrics reset and sample removing into separate issue. Have the suggestion to create empty samples work for you?

@sanych-sun Yes the given sample works for me. I just call .WithLabels() and the counter gets initalized with 0. Thanks.