如何配置弹簧千分尺标签

问题描述

博客文章"Micrometer: Spring Boot 2's new application metrics collector"中提到了"The importance of dimensionality"

但是,它没有足够的示例说明如何在Spring Boot应用程序中设置相关标签

现在,我正在使用Spring Boot 2的Micrometer接口提供Prometheus就绪的输出,而无需进行任何基于Java的配置。例如,设置“主机”标签是否需要我编写一些代码?那会是什么样?

这是我的整个配置:

management:
  metrics:
    export:
      prometheus:
        enabled: true
  endpoints:
    web:
      exposure:
        include: info,health,metrics,prometheus

解决方法

metricsCommonTags有一个相关示例,如下所示:

@Bean
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
    return registry -> {
        String hostname = "unknown";
        try { hostname = InetAddress.getLocalHost().getHostName(); }
        catch (Exception e) {}
        registry.config().commonTags("host",hostname);
    };
}