0

I have a running code with spring boot(3.1.3) with zipkin. However in IntegrationTests trace information is not collected by zipkin. I am using these dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-tracing-bridge-brave</artifactId>
    <version>1.1.4</version>
</dependency>
<dependency>
    <groupId>io.zipkin.reporter2</groupId>
    <artifactId>zipkin-reporter-brave</artifactId>
    <version>2.16.4</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-testcontainers</artifactId>
    <scope>test</scope>
</dependency>

the application.properties file contains these properties:

spring.application.name=spring-micrometer-tracing-demo
management.endpoints.web.exposure.include=health,info,prometheus
management.zipkin.tracing.endpoint=http://localhost:9411/api/v2/spans
management.zipkin.tracing.read-timeout=60
management.tracing.sampling.probability=1.0
spring.zipkin.baseUrl=http://localhost:9411/
management.tracing.enabled=true

and my integration test is as follows:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Testcontainers
class DemoApplicationTests {

    @LocalServerPort
    int port;


    @Container
    //@RestartScope
    static GenericContainer<?> zipkinConatiner = new GenericContainer<>("openzipkin/zipkin:2.24-arm64")
            .withReuse(false)
            .withExposedPorts(9411)
            .waitingFor(Wait.forHttp("/api/v2/spans?serviceName=anything").withStartupTimeout(Duration.of(2, ChronoUnit.MINUTES )))
            .withNetwork(Network.SHARED)
            .withNetworkAliases("zipkin");


    @Test
    void testRestEndpoint() {
        System.err.println("http://localhost:%s/api/v2/spans?serviceName=spring-micrometer-tracing-demo".formatted(getPort()));
        RestTemplate restTemplate = new RestTemplate();
        IntStream.rangeClosed(0,99).forEach(i ->{
            restTemplate.getForObject("http://localhost:%s/status".formatted(port), String.class);
        });
        String status = restTemplate.getForObject("http://localhost:%s/status".formatted(port), String.class);
        Assertions.assertEquals("OK", status);
        String zipkinResult = restTemplate.getForObject("http://localhost:%s/api/v2/spans?serviceName=spring-micrometer-tracing-demo".formatted(getPort()), String.class);
        Assertions.assertFalse("[]".equals(zipkinResult));
        sleep(90);
    }

    private static void sleep(int sn) {
        try {
            Thread.sleep(sn * 1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @DynamicPropertySource
    static void zipkinProperties(DynamicPropertyRegistry registry) {
        Integer p = getPort();
        registry.add("spring.zipkin.baseUrl",()-> "http://localhost:%s".formatted(p));
        registry.add("management.zipkin.tracing.endpoint", () -> "http://localhost:%s/api/v2/spans".formatted(p));
    }

    private static Integer getPort() {
        return zipkinConatiner.getMappedPort(9411);
    }

}

the full-code is in https://github.com/ozayduman/spring-boot-3-zipkin I have tried to run the test with testcontainers but it did not work. Then I wrote a SpringIntegrationTest I did not work either. I also provided the same application properties through DynamicPropertySource.

Ozay Duman
  • 447
  • 3
  • 6

0 Answers0