Search code examples
javaspring-bootmetricsmicrometerspring-micrometer

Counter with tags - How to group by dimension/tag?


I am trying to understand spring micromenter's group by dimension/tag functionality. I am using StepMeterRegistry and incrementing the counter but don't see desired result.

create counters as,

final MeterRegistry myRegistry = new StepMeterRegistry(config, Clock.SYSTEM) {...};

final Counter myCounter1 = myRegistry.counter("myTestCounter", "tag1", "value1");
final Counter myCounter2 = myRegistry.counter("myTestCounter", "tag2", "value2");
final Counter myCounter1n2 = myRegistry.counter("myTestCounter", "tag1", "value1", "tag2", "value2");

increment counters as,

myCounter1.increment();
myCounter2.increment();
myCounter1n2.increment();

when i print (after step duration),

myCounter1.measure(); => value=1.0
myCounter2.measure(); => value=1.0
myCounter1n2.measure(); => value=1.0

whereas i was expecting (after step duration),

myCounter1.measure(); => value=2.0
myCounter2.measure(); => value=2.0
myCounter1n2.measure(); => value=1.0

Is my understanding correct ? Or how can i realize group by (or) select by functionality ?


Solution

  • You can find Counters by tags with MeterRegistry.find() and aggregate them on your own. AFAICT there's no out-of-box-support for a aggregated value by tags and I'd expect the aggregation happens in a monitoring system.

    See this test to understand how it works.