1

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.?

Arun Gowda
  • 2,721
  • 5
  • 29
  • 50
  • I am not familiar with Spring, however, it looks like it adds custom headers. That can be achieved through custom interceptors, refer: https://stackoverflow.com/questions/48327796/apache-httpclient-custom-dynamic-header-on-every-request Far as I can recollect, there is nothing ready made for including trace headers – Ironluca Aug 23 '23 at 08:49
  • Yes. Writing custom interceptor is the last option I'd be looking at. – Arun Gowda Aug 23 '23 at 08: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 – Ironluca Aug 23 '23 at 08:59
  • I'll take a look into that. Thanks. – Arun Gowda Aug 23 '23 at 09:09

1 Answers1

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.

Arun Gowda
  • 2,721
  • 5
  • 29
  • 50