Adding test utils to reset metrics #961
tillprochaska
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have started instrumenting a Python app I’m maintaining. To ensure correctness of the collected metrics I’ve added a few basic unit tests.
As metrics are typically defined as module globals, testing them in isolation requires resetting them before each test. I’ve been using the following as part of the setup hook in my test cases which seems to work as expected:
The only alternative I’ve come across so far seems to be storing the initial value of a metric at the beginning of a test, then asserting the difference is as expected.
This can get quite verbose and complex, especially when you need to test multiple metrics or different permutations of label values. Ideally, I’d like to be able to do something like this:
There are multiple GitHub issues discussing similar features. I understand that adding methods to reset metrics to the core API would probably add confusion and might result in people using these methods outside of unit tests, violating Prometheus conventions.
But would you be open to add test helpers like this to a separate, clearly named module as a convenience? I’d be happy to open a PR to contribute this.
For starters, I think methods like
reset_registry(registry: CollectorRegistry)
andreset_metric(metric: MetricWrapperBase)
would be helpful.Beta Was this translation helpful? Give feedback.
All reactions