prom-client
prom-client copied to clipboard
Counter reset that sets every value to zero
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.
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?
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.
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?
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?
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.
Agree, and the histogram function already has this "zero" function, could just add it for counters. Relates to #455 and #438
A workaround for now, until there's a design decision:
counter.inc({label1: "value1"}, 0)
gauge.set({label1: "value1"}, 0)
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?
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.