micrometer - spring boot 3 - rest template - traceids are not propagated as expected

1k views Asked by At

I have two spring boot services A and B.

Service A uses a RestTemplate to call an endpoint in Service B.

RestTemplate and the call in Service A looks like below:

  @Bean
  public RestTemplate simpleRestTemplate() {
    return new RestTemplate();
  }



restTemplate.exchange(
          endpoint,
          HttpMethod.GET,
          HttpEntity.EMPTY,
          new ParameterizedTypeReference<>() {
          }
      );

Now, I notice that the traceId logged in Service A is different from the one logged in Service B. How do I make the traceId Service A to propagate to Service B when an endpoint is called using the RestTemplate?

2

There are 2 answers

1
Prudvinath On BEST ANSWER

I'm adding a solution that worked for me. Hope it helps someone.

I added gradle dependency below:

[group: 'io.zipkin.brave', name: 'brave-instrumentation-spring-web']

I built RestTemplate as below:

 @Bean
  public HttpTracing create(final Tracing tracing) {
    return HttpTracing
        .newBuilder(tracing)
        .build();
  }

  @Bean
  public RestTemplate simpleRestTemplate(final HttpTracing httpTracing) {
    return  new RestTemplateBuilder()
        .interceptors(TracingClientHttpRequestInterceptor.create(httpTracing))
        .build();
  }
2
Jonatan Ivanov On

Here you are explicitly telling Spring Framework that you want to use a RestTemplate that you don't want to be instrumented.

Please read the docs, you need to create a RestTemplate bean from the builder that Spring provides:

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