如何为默认的 Spring Boot 2 指标定义附加或自定义标签? [英] How to define additional or custom tags for default Spring Boot 2 metrics?

查看:92
本文介绍了如何为默认的 Spring Boot 2 指标定义附加或自定义标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我使用 Micrometer 切换到 Spring Boot 2.当我获得这些闪亮的新指标时,我与我们的 DevOps 人员进行了交谈,我们开始将它们导出到 Telegraf.

Recently I switched to Spring Boot 2 with Micrometer. As I got these shiny new metrics, I talked with our DevOps guys and we started exporting them to Telegraf.

为了区分不同的应用程序和应用程序节点,我们决定使用标签.这对于自定义指标非常有效,但现在我开始考虑预定义.为了对默认指标实现相同的效果,我还需要能够为它们添加额外的标签.

To distinguish between different applications and application nodes, we decided to use tags. This works perfectly for custom metrics, but now I started thinking about the pre-defined. To achieve the same for default metrics, I need the ability to add extra tags for them as well.

是否有可能实现这一目标?我这样做对吗?

Is it possible to achieve this? Am I doing this right?

我尝试了下一种方法:

@Component
public class MyMetricsImpl implements MyMetrics {

    @Autowired
    protected MyProperties myProperties;
    @Autowired
    protected MeterRegistry meterRegistry;

    @PostConstruct
    public void initialize() {
        this.meterRegistry.config()
                .commonTags(commonTags());
    }

    @Override
    public List<Tag> commonTags() {
        List<Tag> tags = new ArrayList<>();
        tags.add(Tag.of("application", myProperties.getApplicationName()));
        tags.add(Tag.of("node", myProperties.getNodeName()));
        return tags;
    }
}

问题是我的指标表现正确,甚至一些引导指标(至少 http.server.requests)看到我的标签.但是 jvm.*system.*tomcat.* 和许多其他标签仍然没有所需的标签.

The problem is that my metrics behave correctly and even some of the Boot's metrics (at least http.server.requests) see my tags. But jvm.*, system.*, tomcat.* and many others still don't have the needed tags.

推荐答案

如果您正在寻找通用标签支持,您可以通过注册一个 MeterFilter 来实现.

If you are looking for common tags support, you can do it by registering a MeterFilter doing it.

请参阅此提交这个分支 为例.

随着即将推出的Spring Boot 2.1.0.M1,您可以使用以下属性:

With the upcoming Spring Boot 2.1.0.M1, you can use the following properties:

management.metrics.tags.*= # Common tags that are applied to every meter.

参见参考 了解详情.

由于问题已更新,我使用基于 MeterFilter 的方法检查了更新的问题,并确认其工作方式如下:

As the question has been updated, I checked the updated question with this MeterFilter-based approach and confirmed it's working as follows:

请求:http://localhost:8080/actuator/metrics/jvm.gc.memory.allocated

回复:

{
  "name" : "jvm.gc.memory.allocated",
  "measurements" : [ {
    "statistic" : "COUNT",
    "value" : 1.98180864E8
  } ],
  "availableTags" : [ {
    "tag" : "stack",
    "values" : [ "prod" ]
  }, {
    "tag" : "region",
    "values" : [ "us-east-1" ]
  } ]
}

我没有检查更新问题中提供的方法,但我只会使用经过验证的基于 MeterFilter 的方法,除非有任何理由坚持使用该方法.

I didn't check the approach which has been provided in the updated question but I'd just use the proven MeterFilter-based approach unless there's any reason to stick with the approach.

我研究了该方法并能够使用 这个分支.

I looked into the approach and was able to reproduce it with this branch.

@PostConstruct 中应用通用标签为时已晚,因为一些指标已经注册.http.server.requests 起作用的原因是它将在第一个请求中注册.尝试在 过滤器的应用要点,如果您对此感兴趣.

It's too late to apply common tags in @PostConstruct as some metrics have been registered already. The reason why http.server.requests works is that it will be registered with the first request. Try to put a breakpoint on the point of filters' application if you're interested in it.

简而言之,尝试上述方法,类似于即将推出的 Spring Boot 开箱即用支持.

In short, try the above approach which is similar to the upcoming Spring Boot out-of-box support.

这篇关于如何为默认的 Spring Boot 2 指标定义附加或自定义标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆