How to Enable Actuator’s HttpTrace in Spring Boot 3

In this tutorial, you will learn how to enable the Actuator’s httpTrace endpoint in Spring Boot 3. In fact, /httptrace has been renamed to /httpexchanges, so this tutorial will teach you how to enable the Actuator’s /httpexchanges API endpoint instead of /httptrace.

The httpexchanges endpoint provides information about HTTP request-response exchanges. These HTTP exchanges are essentially requests and responses between a client and a server.

With the /httpexchanges actuator endpoint, you can retrieve information about the exchanges that have occurred, such as the HTTP method used, the status code returned, the request and response headers, and the request and response bodies. You can also replay previous exchanges or modify them to simulate different scenarios. To learn more, check out the documentation page.

Step 1. Add Actuator Dependency

The first step is to add actuator dependency to your project.

Maven Dependency

If your project is maven-based, then add the following dependency to pom.xml file.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Gradle Dependency

If your project is Gradle-based, add the following dependency to your build.gradle file.

ependencies {
    compile 'org.springframework.boot:spring-boot-starter-actuator'
}

Step 2. Enable /actuator/httpexchanges Endpoint

By default, the /actuator/httpexchanges endpoint is disabled in Spring Boot 3. To enable it, you will need to add the following to your application.properties file:

management.endpoints.web.exposure.include=httpexchanges

To enable all API endpoints, use the following property value instead:

management.endpoints.web.exposure.include=*

Step 3. Create InMemoryHttpExchangeRepository Bean

For the HttpExchanges API endpoint to work in Spring Boot 3, create a Bean of HttpExchangeRepository type. To do that, add to your project the following Configuration class.

import org.springframework.boot.actuate.web.exchanges.HttpExchangeRepository;
import org.springframework.boot.actuate.web.exchanges.InMemoryHttpExchangeRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ActuatorHttpExchangesConfiguration {
    @Bean
    public HttpExchangeRepository httpTraceRepository()
    {
        return new InMemoryHttpExchangeRepository();
    }
}

The HttpExchangeRepository interface is part of Spring Boot’s HTTP tracing support, which allows you to record and store information about HTTP exchanges that occur in your application. The InMemoryHttpExchangeRepository is a default implementation of this interface that stores the recorded HTTP exchanges in memory.

By creating a HttpExchangeRepository bean and configuring it with the InMemoryHttpExchangeRepository, you enable Spring Boot to record and store information about HTTP exchanges that occur in your application. This information can then be accessed and manipulated through the /httpexchanges actuator endpoint.

You can also create your own implementation of the HttpExchangeRepository interface if you want to store the recorded HTTP exchanges in a different location, such as a database or a file system.

Step 4. Access Actuator HttpExchanges Endpoint

At this moment, the /actuator/httpexchanges endpoint should be enabled. So your next step will be to Build and Run your Spring Boot application.

Once your application is up and running, open the following URL in the browser window:

http://localhost:8080/actuator/httpexchanges

Refresh the page, and you should get a similar response in response:

{
   "exchanges":[
      {
         "timestamp":"2022-12-29T22:03:56.047787Z",
         "request":{
            "uri":"http://localhost:8080/actuator/httpexchanges",
            "method":"GET",
            "headers":{
               "host":[
                  "localhost:8080"
               ],
               "upgrade-insecure-requests":[
                  "1"
               ],
               "accept":[
                  "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
               ],
               "user-agent":[
                  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15"
               ],
               "accept-language":[
                  "en-CA,en-US;q=0.9,en;q=0.8"
               ],
               "accept-encoding":[
                  "gzip, deflate"
               ],
               "connection":[
                  "keep-alive"
               ]
            }
         },
         "response":{
            "status":200,
            "headers":{
               "Content-Type":[
                  "application/vnd.spring-boot.actuator.v3+json"
               ],
               "Transfer-Encoding":[
                  "chunked"
               ],
               "Date":[
                  "Thu, 29 Dec 2022 22:03:56 GMT"
               ],
               "Keep-Alive":[
                  "timeout=60"
               ],
               "Connection":[
                  "keep-alive"
               ]
            }
         },
         "timeTaken":"PT0.086595S"
      }
   ]
}

I hope this tutorial was helpful to you. Check out the Spring Cloud and Spring Boot tutorials pages to learn more.

Happy learning!