I have tests and want to make assertions against micrometer metrics, but the tests run in random order so I want to reset or clear all the micrometer metrics before each test so my assertion are always correct.
Asked
Active
Viewed 9,355 times
10
3 Answers
9
It depends on the Meter registry used. If you are using a SimpleMeterRegistry
you could do:
registry.getMeters().forEach((meter) -> registry.remove(meter))

rogerdpack
- 62,887
- 36
- 269
- 388

checketts
- 14,167
- 10
- 53
- 82
-
3Same but more concise and works for any MeterRegistry: `registry.clear()`. Actually removes the meters rather than resets their values. – David Soroko Mar 07 '21 at 13:21
-
Turn this into a separate answer and I'll be happy to upvote. – Stefan Haberl Sep 02 '21 at 08:12
0
You should try to mock meter registry, since they are monitoring and should not affect your business logic and used for alerting. In case that doesn't help:
for gauge you can reset values
AtomicInteger myGauge = meterRegistry.gauge("numberGauge", new AtomicInteger(0));
myGauge.set(0);
for counter, reinitialize,
counter = meterRegistry.counter(counter.toString());
Reference: https://micrometer.io/docs/concepts#_manually_incrementingdecrementing_a_gauge

Sanket Barapatre
- 173
- 3
-
1meterRegistry.counter() wont reinitialize counter, it creates or return existing counter – voipp Jul 28 '21 at 18:00