prom-client-net / prom-client

.NET client for Prometheus
MIT License
127 stars 21 forks source link

Option to reset counter #118

Closed mwinkler closed 4 years ago

mwinkler commented 4 years ago

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

sanych-sun commented 4 years ago

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;

phnx47 commented 4 years ago

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

phnx47 commented 4 years ago

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).

sanych-sun commented 4 years ago

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.

sanych-sun commented 4 years ago

@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?

mwinkler commented 4 years ago

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