Tracing Requests & Responses with Spring Boot Actuator

Spring Boot Actuator is a sub-project that provides endpoints allow you to monitor and interact with your application. You can take a look at complete list of endpoints but we will focus on trace and its customization in this post.

By default, trace endpoint is enabled for all HTTP requests and shows last 100 of them with 5 default properties:

So, when you hit /trace, the typical response will be like:

The question is: how can we customize these properties? Is there any way to exclude some of them or include even more properties? The answer is YES.

Here, you can see all available properties. Sometimes, the best documentation is the source code :).

In your configuration file you can specify the properties you want to trace:

management.trace.include = remote_address, parameters

Note that this will override default properties. If you want to keep them, you have to specify them along with additional properties you want.

In the introduction part we said that it’s enabled for all HTTP requests. It really is. If you played with it, probably you saw that requests made to /trace also traced. Let’s say you don’t want some endpoints to be traced such as /admin/**, /trace and endpoints provide static files like /css/** and /js/**.

To be able to prevent those endpoints to be traced, we need to extend WebRequestTraceFilter and override its shouldNotFilter method which inherited from OncePerRequestFilter.

shouldNotFilter’s default implementation was always returning false. Now with this implementation, it returns true for the endpoints we don’t want to trace. By annotating this class with @Component, we tell Spring to register it as a bean instead inherited WebRequestTraceFilter. No more configuration needed for this to work.

It’s not over yet, we need more customization!

In the introduction part we also said that, trace endpoint shows last 100 requests by default. Unfortunately, there is no way to change it from configuration file directly but still it’s a piece of cake.

By default, InMemoryTraceRepository is used as an implementation of TraceRepository. By extending it, we can expand the capacity, log requests explicitly or even persist them. It’s up to you.

Here is the source code of sample application.