10

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.

David Soroko
  • 8,521
  • 2
  • 39
  • 51
lfmunoz
  • 822
  • 1
  • 9
  • 16

3 Answers3

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
8

You can use clear() method

registry.clear();
mrek
  • 1,655
  • 1
  • 21
  • 34
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

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