Search code examples
micrometerspring-micrometer

How to use the same Counter object for multiple metric names using different tags


I want to record counts for multiple metric names like metricA, metricB, metricC so on and so forth. One option is to create multiple counter objects like this

Counter
.builder("metricA")
.register(registry);

However, this is going to lead to a lot of objects which I am not OK with.

I was wondering if there is a way to use the same counter object and record counts for multiple metrics.

Is there a way to create counter and then create tags on the fly fetching the same counter object and increment on those tags separately ?

The idea is to use the plot the same metric (meter) in the monitoring tool and have the separation around the tags in the same graph.

Or if there is a way to create a Counter meter with lets say 10 tags and increment a tag at a time ?


Solution

  • A meter in Micrometer is identified by its unique name and tag combination. A meter holds one or metric data-types to expose one or more metrics specific to that meter. E.g. a counter only holds one data-type: a count of occurences. Some other meters like timers expose multiple metrics. At least a count, a sum and a rolling max.

    In your case, you can't use a counter to record multiple/different counts.

    So if you have a family of metrics belonging together, you can "group" them by the same meter name, but give them a tag with specific values to reason about the different "family members" (drill-down). (Or not by reasoning just about the metric name without tags - in case your monitoring solution allows for that.)

    E.g.

    myops{op=foo}
    myops{op=bar}
    myops{op=baz}