Add a clear() method to metric objects to remove all labelsets#642
Add a clear() method to metric objects to remove all labelsets#642csmarchbanks merged 1 commit intoprometheus:masterfrom antoinearbouin:master
Conversation
csmarchbanks
left a comment
There was a problem hiding this comment.
Hello, thank you for the contribution!
Typically the best way to test metrics is to create a separate registry for each test for the best isolation, or to unregister the metric from a shared registry at the end of the test. Would one of those methods work for your use case?
|
It does not work in my case because my metrics are global variables shared in my code. So changing the registry does not reset values stored in the metric. from prometheus_client import Counter, CollectorRegistry, REGISTRY
from pytest import fixture
RUN_COUNTER = Counter('connector_run', labelnames=['tenant'])
def my_function():
RUN_COUNTER.labels('tenantid').inc()
# do things
@fixture
def metric_registry():
registry = CollectorRegistry()
registry.register(RUN_COUNTER)
return registry
def test1(metric_registry):
my_function()
# other asserts
def test2(metric_registry):
my_function()
run_count = metric_registry.get_sample_value('connector_run_total', labels={'tenant': 'tenantid'})
assert run_count == 1.0(the code is not as simple so I need to keep When I run this code My issue come from the fact that this is not the registry which store the metric value but @fixture
def metric_registry():
RUN_COUNTER.reset()
registry = CollectorRegistry()
registry.register(RUN_COUNTER)
return registry |
csmarchbanks
left a comment
There was a problem hiding this comment.
That makes sense thanks! I left one question/suggestion, but generally I am happy to add a function like this.
prometheus_client/metrics.py
Outdated
| with self._lock: | ||
| del self._metrics[labelvalues] | ||
|
|
||
| def reset(self): |
There was a problem hiding this comment.
What would you think of naming this remove_all instead of reset to match remove? I could imagine reset also meaning something like "reset all values to zero" or something else.
There was a problem hiding this comment.
Actually, according to https://prometheus.io/docs/instrumenting/writing_clientlibs/#labels, I think it should be called clear().
There was a problem hiding this comment.
If the documentation say so. I'll rename it clear.
This method can be usefull to test metrics. Signed-off-by: Antoine Arbouin <aarbouin@antidot.net>
Hello @brian-brazil,
I wanted to test metric values in my application unit tests but I didn't figured out how to do it (I probably missed something). That's why I propose to add this simple
reset()method. Do you think it can be useful? Or is there a better way to test than reseting metrics at the beginning of the test?And thank you for this very useful lib!