我使用的是 Spring Boot 2.7.7,下面是我的 Maven 依赖项:
这是我在控制器中设置 DatadogConfig 的方法:
DatadogConfig config = new DatadogConfig() {
@Override
public Duration step() {
return Duration.ofSeconds(10);
}
@Override
public String get(String k) {
return null; // accept the rest of the defaults
}
};
logger.info(appConfig.getApiKey());
MeterRegistry registry = new DatadogMeterRegistry(config, Clock.SYSTEM);
我的application.yml设置如下:
management.metrics.export.datadog:
apiKey: "******"
# You will probably want disable Datadog publishing in a local development profile.
enabled: true
# The interval at which metrics are sent to Datadog. The default is 1 minute.
step: "1m"
server:
port: 8081
运行应用程序后,我确实看到 Meter 注册表正在尝试推送指标,但是在向控制器发送请求后,我收到如下所示的异常:
i.m.c.instrument.push.PushMeterRegistry : publishing metrics for DatadogMeterRegistry every 1m
i.m.datadog.DatadogMeterRegistry : An application key must be configured in order for unit information to be sent to Datadog.
........
io.micrometer.core.instrument.config.validate.ValidationException: datadog.apiKey was 'null' but it is required
at io.micrometer.core.instrument.config.validate.Validated$Either.orThrow(Validated.java:375) ~[micrometer-core-1.9.6.jar:1.9.6]
我确实遵循了这些帖子,但仍然收到错误 千分尺
我缺少或做错了什么配置?
您有两个
DatadogMeterRegistry
实例。 Spring Boot 会为您创建一个,然后您在属性文件中设置其配置值。您还可以手动创建一个实例(我不知道为什么),您只设置步骤(顺便说一句,您设置的两个步骤是不同的)。
解决办法:删除自己写的代码,不要手动创建注册表。如果出于某种原因必须创建注册表,则需要将其创建为
@Bean
。
您需要在 yml 或属性文件中设置以下属性。
management.datadog.metrics.export.apiKey=
management.datadog.metrics.export.application-key=
值得检查示例中共享的 yml 文件是否具有正确的格式缩进。