micrometerdistributed-tracingspring-resttemplatespring-boot-3micrometer-tracing

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


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?


Solution

  • 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();
      }