我已经花了几天时间,但无法让它工作,这是春季仪器的新手。
我有一个 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/micrometer和https://micrometer.io/docs/registry/statsD。如何为我的代码创建一个 bean,或者如何使用 Spring Boot 的自动配置注册表?