如何使用千分尺追踪测试中的SimpleTracer

问题描述 投票:0回答:1

我正在尝试使用 Spring Boot 3 + Micrometer Tracing 为应用程序编写一个简单的测试

休息控制器:

@RestController
@RequestMapping("/customers2")
@Slf4j
public class CustomerController {

    @Autowired
    WebClient.Builder webClientBuilder;

    @GetMapping
    public String hello() {
        log.info("Hello Controller2 called...");

        webClientBuilder
                .baseUrl("http://localhost:8080")
                .build()
                .method(HttpMethod.GET)
                .uri(uriBuilder -> uriBuilder.path("/customers").build())
                .exchange()
                .block();

        return "Hello World2";
    }

有史以来最简单的测试课程

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@EnableAutoConfiguration
@EnableTestObservation
class DemoApplicationTests {

    @Autowired
    SimpleTracer tracer;

    @Autowired
    WebClient.Builder webClientBuilder;

    @Test
    void contextLoads() {
        webClientBuilder
                .build()
                .method(HttpMethod.GET)
                .uri(uriBuilder -> uriBuilder.path("/customers2").build())
                .exchange()
                .block();

        System.out.println("hola");
    }

}

测试配置:

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@AutoConfigureObservability
@Import({
        EnableTestObservation.ObservationTestConfiguration.class
})
public @interface EnableTestObservation {

    @TestConfiguration
    class ObservationTestConfiguration {

        @Bean
        SimpleTracer simpleTracer() {
            return new SimpleTracer();
        }

    }

}

当我运行测试时,我得到这个:

类 io.micrometer.tracing.test.simple.SimpleTraceContext 不能 转换为 io.micrometer.tracing.brave.bridge.BraveTraceContext 类 (io.micrometer.tracing.test.simple.SimpleTraceContext 和 io.micrometer.tracing.brave.bridge.BraveTraceContext 处于未命名状态 加载器'app'的模块)

spring-boot spring-boot-actuator micrometer-tracing
1个回答
0
投票

我无法让 SimpleTracer 工作,但我可以通过更改

EnableTestObservation
注释来使测试工作。我将 Tracer bean 实现为 Brave Tracer 与 SimpleTracer。

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@AutoConfigureObservability
public @interface EnableTestObservation {

  @TestConfiguration
  class ObservationTestConfiguration {

    @Autowired
    ObservationRegistry observationRegistry;

    @Bean
    Tracer simpleTracer() {
      return ZipkinBraveSetup.builder()
          .register(observationRegistry)
          .getBuildingBlocks()
          .getTracer();
    }
  }

}

我还必须删除 pom 中的一些依赖项,以便找到正确的 bean 来连接 Tracer。由于某种原因,测试类路径上存在多个跟踪器实现。我只是想使用 Brave,所以我排除了其他人:

<dependency>
      <groupId>io.micrometer</groupId>
      <artifactId>micrometer-tracing-integration-test</artifactId>
      <exclusions>
        <exclusion>
          <groupId>io.micrometer</groupId>
          <artifactId>micrometer-tracing-bridge-otel</artifactId>
        </exclusion>
        <exclusion>
          <groupId>io.micrometer</groupId>
          <artifactId>micrometer-tracing-reporter-wavefront</artifactId>
        </exclusion>
        <exclusion>
          <groupId>io.zipkin.reporter2</groupId>
          <artifactId>zipkin-reporter</artifactId>
        </exclusion>
      </exclusions>
      <scope>test</scope>
    </dependency>
© www.soinside.com 2019 - 2024. All rights reserved.