【问题标题】:datadog.apiKey was 'null' but it is requireddatadog.apiKey 为“空”,但它是必需的
【发布时间】:2022-02-05 10:14:14
【问题描述】:

我目前正在使用 Spring Boot 2.7.0-M1,以下是我在 build.gradle 中的依赖项:

    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-config'
    implementation 'org.springframework.boot:spring-boot-starter-cache'
    compileOnly 'org.projectlombok:lombok'
    implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    implementation 'io.micrometer:micrometer-registry-datadog:latest.release'
    datadogagent("com.datadoghq:dd-java-agent:$datadoghq_dd_version")
    implementation("com.datadoghq:dd-trace-api:${datadoghq_dd_version}")

但是,当我尝试设置 micrometer bean 时:

@Configuration
public class MeterRegistryConfig {
  @Bean
  MeterRegistry meterRegistry(ApplicationContext context) {
    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
      }
    };

    return new DatadogMeterRegistry(config, Clock.SYSTEM);
  }
}

我收到一个启动错误:

2022-02-05 05:07:56.457 ERROR 7897 --- [  restartedMain] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'webMvcMetricsFilter' defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfiguration.class]: Unsatisfied dependency expressed through method 'webMvcMetricsFilter' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'compositeMeterRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/CompositeMeterRegistryConfiguration.class]: Unsatisfied dependency expressed through method 'compositeMeterRegistry' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'meterRegistry' defined in class path resource [com/hbomax/ml/mlsvc/config/MeterRegistryConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.micrometer.core.instrument.MeterRegistry]: Factory method 'meterRegistry' threw exception; nested exception is io.micrometer.core.instrument.config.validate.ValidationException: datadog.apiKey was 'null' but it is required
2022-02-05 05:07:56.504  INFO 7897 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-02-05 05:07:56.520  WARN 7897 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
2022-02-05 05:07:56.541  INFO 7897 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

这是我的application.yaml

management:
  info:
    git:
      mode: full
  metrics:
    distribution:
      percentiles:
        http:
          server:
            requests: 0.5, 0.95, 0.99
      percentiles-histogram:
        http:
          server:
            requests: true
      sla:
        http:
          server:
            requests: 200ms, 400ms, 600ms
    export:
      datadog:
        step: 30s
        enabled: true
        api-key: xxxxxxx
    tags:
      application: ${spring.application.name}
      env: ${spring.profiles}

有人可以帮助我了解问题所在吗?我试着四处寻找,但没有得到任何解决方案。我尝试将api-key 替换为apiKey(我知道这很愚蠢)但没有成功。

有什么帮助吗?

【问题讨论】:

    标签: java spring spring-boot spring-mvc micrometer


    【解决方案1】:

    您在 application.yml 中使用 Spring Boot 的配置属性,但您也在创建自己的 DatadogMeterRegistry 不使用这些配置属性,因此它不会具有所需的 API 密钥配置。

    您可以完全删除 MeterRegistryConfig 类,而是使用 Spring Boot 将从配置属性自动配置的 DatadogMeterRegistry,因为您的类路径上有 micrometer-registry-datadog 依赖项。请参阅 Spring Boot 文档的 this section

    【讨论】:

    • 让我测试一下,然后回复你。感谢您的快速澄清!仪器新手:)
    • 你是对的。感谢您指出了这一点。抱歉迟到的评论。
    猜你喜欢
    • 1970-01-01
    • 2016-08-28
    • 2020-04-14
    • 2013-08-02
    • 2019-01-22
    • 2018-05-20
    • 2017-11-29
    • 2015-09-17
    相关资源
    最近更新 更多