简体   繁体   中英

How to configure additional tags depending on metric name for actuator metrics

Is there anyway I can configure what tags to add to @Timed metrics? I know overriding MebMvcTagsProvider or WebMvcTagsContributor classes will return a standard set of tags common for all mvc metrics. But is there any way I can exclude or include some tags with dynamic values for some metrics? Like in this case:

WebMvcTagsProvider object -> provides a set of tags whose values like status etc. are set here -> tag1, val1; tag2, val2

and then we have a rest api like this:

@Timed("metric.1")
public String api1() {}

and another api like this:

@Timed("metric.2")
public String api2() {}

so with this, after invoking both the apis, we'll get these metrics, both having same set of tags

metric-1(tag1="val1", tag2="val2", status=201)
metric-2(tag1="val1", tag2="val2", status=200)

but can we return different set of tags for both of these metrics, like this:

metric-1(tag1="val1", status=201)
metric-2(tag2="val2", status=200)

I found a WebMvcMetricsFilter class which's what all the metrics are going through to generate timers, is there some way I can make use of this to configure what tags are returned based on metric name?

Can someone please help me out in this?

One option (since it is spring and there could be many ways of doing this) is to declare a micrometer MeterFilter and update the tags based on names.

@Bean
public MeterFilter meterFilter() {
    return new MeterFilter() {
        @Override
        public Meter.Id map(Meter.Id id) {
            if(id.getName().equals("metric-1")) {
                return id.withTags(Tags.of("foo", "bar"));
            }
            // more conditions here
            return id;
        }
    };
}

Check this documentation (section 6.2) from micrometer on how to transform metrics. You can add, update or remove tags this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM