简体   繁体   English

在spring boot 2中配置micrometer-registry-statsd

[英]Configure micrometer-registry-statsd in spring boot 2

I've spent a few days and could not get it working, new to instrumentation in spring. 我已经花了几天时间,无法让它工作,春天的仪器新手。

I have a spring boot 2 app. 我有一个春季启动2应用程序。 In pom.xml I defined: pom.xml我定义了:

<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-registry-statsd</artifactId>
  <version>1.1.5</version>
</dependency>

In application.conf : application.conf

management.metrics.export.statsd.host=localhost
management.metrics.export.statsd.port=8125
management.metrics.export.statsd.flavor=etsy
management.metrics.export.statsd.step=2m
management.metrics.export.statsd.enabled=true
management.endpoints.web.exposure.include=health,metrics

In the app when it starts I want to export a new metric (counter): 在应用程序启动时,我想导出一个新的指标(计数器):

@SpringBootApplication
public class MyApplication {

  private static final Logger LOG = LoggerFactory.getLogger(MyApplication.class);

  private static final StatsdConfig config = new StatsdConfig() {
    @Override
    public String get(String k) { return null; }
    @Override
    public StatsdFlavor flavor() { return StatsdFlavor.ETSY; }
  };

  private static final MeterRegistry registry = new StatsdMeterRegistry(config, Clock.SYSTEM);

  public static void main(String[] args) {
    // globalRegistry is composite hence was hoping they will unite into one
    Metrics.globalRegistry.add(registry);

    Counter myCounter = Counter
        .builder("myCounter")
        .description("indicates instance count of the object")
        .tags("dev", "performance")
        .register(registry);
//      .register(Metrics.globalRegistry);

    myCounter.increment(2.0);
    LOG.info("Counter: " + myCounter.count());
    SpringApplication.run(MyApplication.class, args);
  }

}

If it is coded like above, it's not available under http://localhost:8081/actuator/metrics/myCounter . 如果它是如上编码的,则在http:// localhost:8081 / actuator / metrics / myCounter下不可用 But if I uncomment .register(Metrics.globalRegistry); 但如果我取消注释.register(Metrics.globalRegistry); and comment the previous line then http://localhost:8081/actuator/metrics/myCounter contains the metric, but its value is 0.0 instead of 2.0 . 并注释前一行然后http:// localhost:8081 / actuator / metrics / myCounter包含指标,但其值为0.0而不是2.0

What I want is to have my custom registry containing custom metrics defined across application and to be correctly registered and available under the metrics endpoint then it can be exported to StatsD. 我想要的是让我的自定义注册表包含跨应用程序定义的自定义指标,并在指标端点下正确注册和可用,然后可以将其导出到StatsD。 Would you know what am I missing in the above? 你知道上面我错过了什么吗?

I followed these docs https://www.baeldung.com/micrometer and https://micrometer.io/docs/registry/statsD . 我按照这些文档https://www.baeldung.com/micrometerhttps://micrometer.io/docs/registry/statsD How to create a bean for my code or alternatively how to use the auto-configured registry by Spring Boot? 如何为我的代码创建一个bean,或者如何使用Spring Boot自动配置的注册表?

Spring Boot's Micrometer auto-configuration will automatically call any MeterBinder beans to bind their meters to the auto-configured MeterRegistry . Spring Boot的Micrometer自动配置将自动调用任何MeterBinder bean将其仪表绑定到自动配置的MeterRegistry With the necessary StatsD dependencies on the classpath, which you already have, this will be a StatsD-based registry. 使用已有的类路径上必需的StatsD依赖项,这将是一个基于StatsD的注册表。 I would recommend using this auto-configuration rather than configuring things yourself. 我建议使用这种自动配置,而不是自己配置。 As things stand, you will have both an auto-configured registry and your own. 事实上,您将拥有一个自动配置的注册表和您自己的注册表。 The auto-configured registry would back off and not be created if you exposed your registry as a Spring bean. 如果您将注册表公开为Spring bean,则自动配置的注册表将退回并且不会创建。

I would recommend removing your StatsdConfig and StatsdMeterRegistry and using the auto-configuration instead. 我建议删除你的StatsdConfigStatsdMeterRegistry并使用自动配置。 You can then use a MeterBinder bean to bind your counter. 然后,您可以使用MeterBinder bean绑定您的计数器。 This will leave your application's main class looking something like this: 这将使您的应用程序的主类看起来像这样:

@SpringBootApplication
public class MyApplication {

    @Bean
    public MeterBinder exampleMeterBinder() {
        return (meterRegistry) -> Counter.builder("myCounter")
            .description("indicates instance count of the object")
            .tags("dev", "performance")
            .register(meterRegistry);
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication, args);
    }

}

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

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