How to instrument outgoing http requests with AWS Xray for Java Spring Boot 3.1 app?

171 Views Asked by At

I am in the process of migrating a java web application from Spring Boot 2.6 to 3.1. Previously, I was using the aws-xray-recorder-sdk-apache-http module to instrument the Apache HttpClient4 client that was being used to make outbound calls, however with the new version of Spring Boot I understand HttpClient4 is no longer supported and needs to be migrated to HttpClient5.

My problem is that I don't understand how to instrument the HttpClient5 client since the proxy wrapper provided by the AWS SDK seems designed for HttpClient4 only.

For example, bean definition with xray tracing enabled is:

package com.mysampleapp.httpclient.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.amazonaws.xray.proxies.apache.http.HttpClientBuilder;

@Configuration
@ConditionalOnProperty(name = "enable.xray.tracing", havingValue = "true")
public class MyXrayHttpClientBuilderConfiguration
{
  @Bean
  @Primary
  public HttpClientBuilder getHttpClientBuilder()
  {
    return HttpClientBuilder.create();
  }
}

However, elsewhere I want a bean of type httpclient5 builder, and this won't work:

package com.mysampleapp.httpclient;

import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class Application
{
    ...

    @Autowired
    // This won't pick up the bean defined above because it's not httpclient5
    private HttpClientBuilder httpClientBuilder;

    ...

    private ClientHttpRequestFactory clientHttpRequestFactory()
    {
        // If using HttpClient4, this method is not defined because it expects a httpclient5
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build());
        factory.setReadTimeout(readTimeOut);
        factory.setConnectTimeout(connectTimeOut);
        return factory;
    }
}

Am I missing something obvious here? The AWS Xray SDK must be able to instrument the latest Apache http clients, right?

0

There are 0 best solutions below