datadog.apiKey 是 'null' 但它是必需的

datadog.apiKey was 'null' but it is required

我目前正在使用 spring 启动 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}")

但是,当我尝试设置千分尺 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}

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

有帮助的人吗?

您在 application.yml 中使用 Spring Boot 的配置属性,但您也在制作自己的 DatadogMeterRegistry 不使用这些配置属性,因此它不会需要 API 键配置。

您可以完全删除 MeterRegistryConfig class,而是使用 Spring Boot 将 auto-configure 从配置属性中 DatadogMeterRegistry,因为您有 micrometer-registry-datadog 依赖于您的 class 路径。请参阅 Spring 引导文档的 this section