0

我已经花了几天时间,但无法让它工作,这是春季仪器的新手。

我有一个 Spring Boot 2 应用程序。在pom.xml我定义:

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

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

在应用程序启动时,我想导出一个新指标(计数器):

@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);
  }

}

如果它像上面那样编码,它在http://localhost:8081/actuator/metrics/myCounter下不可用。但是,如果我取消注释.register(Metrics.globalRegistry);并注释上一行,则http://localhost:8081/actuator/metrics/myCounter包含该指标,但它的值0.0不是2.0.

我想要的是让我的自定义注册表包含跨应用程序定义的自定义指标,并在指标端点下正确注册和可用,然后可以将其导出到 StatsD。你知道我在上面遗漏了什么吗?

我关注了这些文档https://www.baeldung.com/micrometerhttps://micrometer.io/docs/registry/statsD。如何为我的代码创建一个 bean,或者如何使用 Spring Boot 的自动配置注册表?

4

1 回答 1

0

Spring Boot's Micrometer auto-configuration will automatically call any MeterBinder beans to bind their meters to the auto-configured MeterRegistry. With the necessary StatsD dependencies on the classpath, which you already have, this will be a StatsD-based registry. 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.

I would recommend removing your StatsdConfig and StatsdMeterRegistry and using the auto-configuration instead. You can then use a MeterBinder bean to bind your counter. 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);
    }

}
于 2019-06-26T16:11:29.513 回答