spring-bootmicrometerdistributed-tracingmicrometer-tracing

Different TraceId's are generated in Micrometer for single HTTP request


I am recently trying to upgrade spring-boot version 2.7.3 and 2.7.10 to 3.1.0. Since spring-cloud-sleuth is not supported by spring-boot v3.x.x, we have to use micrometer for distributed tracing. However, the traceId is different in both the service but with sleuth it was same.

I have added below dependencies in pom.xml:

    <dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-tracing-bridge-brave</artifactId>
    <version>1.1.1</version>
    </dependency>
    <dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>context-propagation</artifactId>
    <version>1.0.3</version>
    </dependency>
    <dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-observation</artifactId>
    </dependency>

and updated the imports of Tracer to io.micrometer.tracing.Tracer.

When I am sending request from postman to Service A which calls service B(using restTemplate), in the logs I can see the traceId's are different. TraceId is extracted using below code:

    @Autowired 
    io.micrometer.tracing.Tracer tracer;
    String traceId = tracer.currentSpan().context().traceId();

Can anyone please help what is that I am missing that the traceId's are different. Earlier with spring-cloud-sleuth the X-B3-TraceId/ SpanId headers were added automatically but they are not added with micrometer. Do I need to add them manually and if so, then what should be the name of these headers? Thanks in advance!!


Solution

  • Please check that RestTemplate that you are using is created with RestTemplateBuilder like below.

    @Configuration public class Configurator {

    @Bean
    public RestTemplate getRestTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
    

    }

    This is explained by Sergey Kargopolov in article: https://www.appsdeveloperblog.com/micrometer-and-zipkin-in-spring-boot/