prom-client icon indicating copy to clipboard operation
prom-client copied to clipboard

Counter reset that sets every value to zero

Open n-rook opened this issue 6 years ago • 9 comments

I have a counter metric with a single label. It is initialized to zero for all possible values of the label, as is a best practice for counter metrics. This happens immediately after the metric is defined.

In my unit testing framework, I reset all metrics after each test, to make it easy to test the metrics themselves. However, this erases my initialization of everything to zero. That means that if I try to check that the value of my metric at a label is zero, it fails, because the metric actually has no value at that label.

It would be nice to have a version of reset() which sets counter metrics to zero. I guess it could be called "zeroOut", or something like that, and then there would be a "zeroOutMetrics" like "resetMetrics".

I suppose this function should also work on other metric types, for consistency.

I would be happy to open a pull request if you believe it is a good idea.

n-rook avatar Dec 18 '18 01:12 n-rook

It is initialized to zero for all possible values of the label, as is a best practice for counter metrics.

I haven't seen that before, mind pointing to the docs or some example where it's stated that's a best practice?

SimenB avatar Dec 28 '18 13:12 SimenB

I got that impression from this blog post: https://www.robustperception.io/existential-issues-with-metrics

There's a brief mention of it in the official docs: https://prometheus.io/docs/practices/instrumentation/#avoid-missing-metrics

It's been useful to me when I've done it, personally.

n-rook avatar Jan 02 '19 15:01 n-rook

Thanks for the links!

Most Prometheus client libraries (including Go, Java, and Python) will automatically export a 0 for you for metrics with no labels.

(emphasis mine)

that's not the same thing you're wanting here. However, I personally don't really mind a utility function for this. @siimon @zbjornson?

SimenB avatar Jan 02 '19 16:01 SimenB

Ugh I dont really like the reset function overall and not sure on adding more functionality to it.

Making the Api more complex for the reset function makes people think you might need to reset metrics (we get issues open for that from time to time).

To fix your issue, can’t you recreate the metric for each test instead of resetting?

siimon avatar Jan 03 '19 18:01 siimon

I can't easily recreate the metric, because my module looks like this:


const metric = new promClient.Counter({
  name: 'some_metric',
  labelNames: ['this_label_takes_values_from_some_known_enum']
});
for (const l of knownLabels) {
  // Initialize counter to zero.
  metric.labels(l).inc(0);
}

// actual public functions which are exported and use the metric

That is, I initialize the metric on the top-level, so I can't easily rerun this code (because it isn't exported publicly.) I could export this code in a public function, but it's a shame to do so if only testing code wants to run it.

Of course, I realize it is easy for me (who does not maintain this module) to say "please extend the API to make my life more convenient, and then maintain it forever." So I will understand if you decide not to do this.

n-rook avatar Jan 04 '19 15:01 n-rook

Agree, and the histogram function already has this "zero" function, could just add it for counters. Relates to #455 and #438

nkov avatar Sep 14 '21 02:09 nkov

A workaround for now, until there's a design decision:

counter.inc({label1: "value1"}, 0)
gauge.set({label1: "value1"}, 0)

zbjornson avatar Sep 19 '21 17:09 zbjornson

Regarding the above workaround - what if I don't know all the possible label values but still want to see an increase when they are first incremented?

pawKer avatar May 03 '22 15:05 pawKer

Hi @SimenB , here is a situation to make me want to reset counter. Could you please help to provide some comments?

I have a script to get amount value of accepted connections and rejected connections, and report it to prom-client. And I don't want to stored last value at memory and calculate increment each time, so I want the reset it each time I got metrics data from this script.

whodoeshua avatar Sep 27 '22 09:09 whodoeshua