Aditi
Aditi

Reputation: 55

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!!

Upvotes: 2

Views: 7213

Answers (2)

bhavdeep
bhavdeep

Reputation: 36

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/

Upvotes: 2

Jonatan Ivanov
Jonatan Ivanov

Reputation: 6931

Your traceId is different because the tracing information is not propagated, check the request headers, by default it should contain the W3C traceparent header. If it doesn't you are using an http client that is not instrumented or instrumentation is disabled. If so please use an instrumented client/enable instrumentation (WebClient, RestTemplate, OpenFeign, etc.).

If you want to use B3, you need to set it in your properties, see the docs (you can use multiple types if you want): https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#application-properties.actuator.management.tracing.propagation.type

management.tracing.propagation.type=B3

Upvotes: 1

Related Questions