javaspringspring-cloud-sleuthspring-micrometermicrometer-tracing

TracingClientHttpRequestInterceptor equivalent for Apache http client


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


Solution

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