我已经花了几天的时间,但我不能让它工作,这是春天的新仪器。
我有一个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。
我想要的是让我的自定义注册表包含跨应用程序定义的自定义指标,并正确注册并在metrics端点下可用,然后可以将其导出到StatsD。你知道我在上面遗漏了什么吗?
我关注了https://www.baeldung.com/micrometer和https://micrometer.io/docs/registry/statsD这两个文档。如何为我的代码创建bean,或者如何通过Spring Boot使用自动配置的注册表?
发布于 2019-06-27 00:11:30
Spring Boot的微米自动配置将自动调用任何MeterBinder bean,以将它们的仪表绑定到自动配置的MeterRegistry。有了类路径上必要的StatsD依赖项,这将是一个基于StatsD的注册表。我建议使用这种自动配置,而不是自己配置。按照目前的情况,您将拥有一个自动配置的注册表和您自己的注册表。如果您将注册表公开为Spring bean,则自动配置的注册表将后退且不会被创建。
我建议删除您的StatsdConfig和StatsdMeterRegistry,并使用自动配置。然后,您可以使用MeterBinder bean绑定计数器。这将使你的应用程序的主类看起来像这样:
@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);
}
}https://stackoverflow.com/questions/56735269
复制相似问题