'In Spring Boot, how to reset the metrics registry before each test?
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.
Solution 1:[1]
It depends on the Meter registry used. If you are using a SimpleMeterRegistry you could do:
registry.getMeters().forEach((meter) -> registry.remove(meter))
Solution 2:[2]
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rogerdpack |
| Solution 2 | mrek |
| Solution 3 | Sanket Barapatre |
