4

I've been attempting to set up distributed tracing using Spring Micrometer. To propagate the traceId across various services, it appears necessary to configure our HTTP clients to include this information in the headers.

For the RestTemplate client, Spring conveniently offers an interceptor that can be set up as shown below:

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

@Bean
public RestTemplate restTemplate(HttpTracing httpTracing) {
    return new RestTemplateBuilder()
            .interceptors(TracingClientHttpRequestInterceptor.create(httpTracing))
            .build();
}

But I couldn't find a similar one for Apache http client. I am not sure if I should write my custom interceptor that does this?

Or is one available already provided by a library.?

3
  • I am not familiar with Spring, however, it looks like it adds custom headers. That can be achieved through custom interceptors, refer: stackoverflow.com/questions/48327796/… Far as I can recollect, there is nothing ready made for including trace headers Commented Aug 23, 2023 at 8:49
  • Yes. Writing custom interceptor is the last option I'd be looking at. Commented Aug 23, 2023 at 8:51
  • :) I suppose then you have to get on with it, there is really nothing ready made. You certainly could look at the Spring interceptor for the core mechanics, that would help Commented Aug 23, 2023 at 8:59

1 Answer 1

0

Alright so I am using Zipkin's brave as the tracing vendor. During my investigation into potential instrumentation support, I came across relevant information in this resource

Although the documentation there gave me an idea on how to use it, the code example was calling a wrong method.

Here's how I configured my http client5.

  1. First add the dependency in pom.xml

      <dependency>
         <groupId>io.zipkin.brave</groupId>
         <artifactId>brave-instrumentation-httpclient5</artifactId>
     </dependency>
    
  2. Then Configure the Apache http client as below.

     @Bean
     public HttpTracing create(Tracing tracing) {
         return HttpTracing
                 .newBuilder(tracing)
                 .build();
     }
    
     @Bean
     public CloseableHttpClient httpClient(HttpTracing httpTracing) {
         HttpClientBuilder httpClientBuilder = HttpClients.custom();
    
         return HttpClient5Tracing.newBuilder(httpTracing)
                 .build(httpClientBuilder);
     }
    

In addition to Zipkin's 'brave,' another recommended tracing vendor according to Spring is Opentelemetry. Nevertheless, as of now, it appears that their instrumentation libraries are still in the process of being developed, as indicated by their maven repo.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.