运行 集成测试时禁用 Spring Cloud Sleuth?

Disable Spring Cloud Sleuth when running Integration Tests?

当我在本地使用 org.springframework.cloud:spring-cloud-gcp-starter-trace:1.0.0.RELEASE 和 运行 集成测试时,我收到此错误消息:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stackdriverSender' defined in class path resource [org/springframework/cloud/gcp/autoconfigure/trace/StackdriverTraceAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [zipkin2.reporter.Sender]: Factory method 'stackdriverSender' threw exception; nested exception is java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

这是完全可以理解的,因为这个环境变量在本地不存在,而且我不想在 运行 测试时使用 Sleuth/Stackdriver 跟踪。我查看了 reference documentation 但我似乎只能找到有关如何为特定集成点(例如 RxJava、RestTemplate 等)禁用 Sleuth 的信息。但是如何才能完全禁用 Sleuth?

我试过设置 spring.sleuth.enabled=false 但这似乎没有任何区别。

我实际上是通过查看 source for the StackdriverTraceAutoConfiguration class 找到了答案。如果使用GCP解决方法是设置spring.cloud.gcp.trace.enabled=false。这将禁用对所有集成点的跟踪。

您可以禁用跟踪、日志记录并设置伪造的项目 ID,如下所示:

spring.cloud.gcp.project-id=fake-project-id
spring.cloud.gcp.logging.enabled=false
spring.cloud.gcp.trace.enabled=false

操作方法:使用 Zipkin 禁用 Webflux Sleuth 实施的所有测试

如果,您有这些依赖项:

implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
implementation 'org.springframework.cloud:spring-cloud-sleuth-zipkin'

然后,通过将 application.yml 添加到您的 test/resources 文件夹,完全禁用 Sleuth/Zipkin 以进行集成测试:

spring:
  sleuth: # All sleuth features disabled for integration tests
    enabled: false
    reactor:
      enabled: false
    web:
      enabled: false
    async:
      enabled: false
      client:
        template:
          enabled: false
  zipkin: # All zipkin features disabled for integration tests
    enabled: false