简体   繁体   English

如何根据执行器指标的指标名称配置附加标签

[英]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?无论如何我可以配置哪些标签添加到@Timed 指标? I know overriding MebMvcTagsProvider or WebMvcTagsContributor classes will return a standard set of tags common for all mvc metrics.我知道重写 MebMvcTagsProvider 或 WebMvcTagsContributor 类将返回所有 mvc 指标通用的标准标签集。 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:然后我们有一个 rest api 像这样:

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

and another api like this:和另一个 api 像这样:

@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因此,在调用这两个 api 之后,我们将获得这些指标,它们都具有相同的标签集

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

but can we return different set of tags for both of these metrics, like this:但是我们能否为这两个指标返回不同的标签集,如下所示:

metric-1(tag1="val1", status=201) metric-1(tag1="val1", status=201)
metric-2(tag2="val2", status=200) 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?我找到了一个 WebMvcMetricsFilter class ,这是生成计时器的所有指标,有什么方法可以利用它来配置基于指标名称返回的标签吗?

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.一种选择(因为它是 spring 并且可能有很多方法可以做到这一点)是声明一个千分尺MeterFilter并根据名称更新标签。

@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.从 micrometer 查看本文档(第 6.2 节),了解如何转换指标。 You can add, update or remove tags this way.您可以通过这种方式添加、更新或删除标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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