首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在spring boot 2中配置micrometer registry statsd

在spring boot 2中配置micrometer registry statsd
EN

Stack Overflow用户
提问于 2019-06-24 19:08:02
回答 1查看 3.7K关注 0票数 0

我已经花了几天的时间,但我不能让它工作,这是春天的新仪器。

我有一个spring boot 2应用程序。在pom.xml中我定义了:

代码语言:javascript
复制
<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-registry-statsd</artifactId>
  <version>1.1.5</version>
</dependency>

application.conf

代码语言:javascript
复制
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

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

代码语言:javascript
复制
@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

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-27 00:11:30

Spring Boot的微米自动配置将自动调用任何MeterBinder bean,以将它们的仪表绑定到自动配置的MeterRegistry。有了类路径上必要的StatsD依赖项,这将是一个基于StatsD的注册表。我建议使用这种自动配置,而不是自己配置。按照目前的情况,您将拥有一个自动配置的注册表和您自己的注册表。如果您将注册表公开为Spring bean,则自动配置的注册表将后退且不会被创建。

我建议删除您的StatsdConfigStatsdMeterRegistry,并使用自动配置。然后,您可以使用MeterBinder bean绑定计数器。这将使你的应用程序的主类看起来像这样:

代码语言:javascript
复制
@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);
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56735269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档