Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to remove all metrics in the defaultRegistry #950

Open
michaelschuett-tomtom opened this issue Apr 8, 2024 · 6 comments
Open

How to remove all metrics in the defaultRegistry #950

michaelschuett-tomtom opened this issue Apr 8, 2024 · 6 comments

Comments

@michaelschuett-tomtom
Copy link

michaelschuett-tomtom commented Apr 8, 2024

Hello I was trying to figure out a good way to test some metrics in our application but I can't find a good way to clean out the default registry since it's not recreated during tests with mvn. This causes some tests to fail if for instance you use the same "test_metric" name twice. Does anyone have any example to clear out the registry as I couldn't find anything when looking through the code.

It looks like this is not possible based on this. So i guess my general question is how does one test with this library in use?

@fstab
Copy link
Member

fstab commented Apr 8, 2024

You could create a new PrometheusRegistry registry = new PrometheusRegistry() for each test, and then register your metrics with that registry with .register(registry).

Does that work for you?

@michaelschuett-tomtom
Copy link
Author

michaelschuett-tomtom commented Apr 8, 2024

This does work for me I had to do a small refactor so that our metric class by default uses the default registry but then during tests we optionally pass PrometheusRegistry() when initializing the metrics class. This works out well now since all of our metrics are centralized through this class but it would get a little messy if any custom metrics are added outside of it.

The above solves my immediate issue although it would be nice if a clear()/reset() function existed on the prometheus registry for just this purpose so we don't have to expose this for testing. If the defaultRegistry in PrometheusRegistry was exposed for users to overwrite that would also work but maybe this causes other issues?

@holubec-petr
Copy link

Hi, I also ran into this problem. Our metrics can be enabled or disabled based on a user's request so I need to unregister all registered collectors on such a request. It was possible in simpleclient using the clear() method but it is missing in the new 1.x version.

I will provide PR with the clear() method in a few minutes.

@mimaison
Copy link
Contributor

Having a method to clear the registry would be really helpful for tests. Sure we can inject a new PrometheusRegistry instance but that requires further refactoring to adapt code that used to use simpleclient.

@dhoard
Copy link
Collaborator

dhoard commented Jun 24, 2024

PrometheusRegistry clear() / reset() methods will cause test issues when using JUnit5 in parallel execution mode.

@der-eismann
Copy link

I'd love to see that added back as well. I was trying to upgrade the Java client in the Zookeeper project, however I am struggling to refactor the tests to make them work with a new PrometheusRegistry() before each run (see here).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants