Capturing Failed Request Info in Spring Boot Services

How to capture failed web request information in spring boot Microservices

Venkat Duddu
Chegg
Published in
2 min readAug 12, 2019

--

Often, the lack of request information makes it difficult to debug or reproduce issues happening in the production environment. Backend developers typically rely on the stack trace in the logs and make a calculated guess based upon what web request resulted in a given exception. Having complete request information (payload, headers, etc.) for every exception happening in the service would help reproduce the issue in a local environment, and achieve faster response time to production issues. This tutorial will walk you through a solution we had at Chegg to solve this problem.

1. Request Info Model

The first step is to think through all of the information that would make sense to log in case of a failed request, and then build a model. Sample request info model looks like this:

Note: @RequestScope annotation for this model to have lifecycle bound to the current web request.

2. Exception handlers

@ControllerAdvice annotation allows us to handle the exceptions in web request processing. You need to modify exception handlers to indicate that request processing has failed. Below is the example of the exception handler for java.lang.Exception. Since we expect to have custom exception handlers for every known exception, this handler has Order with LOWEST_PRECEDENCE.

3. Servlet Request filter

We made use of Spring’s AbstractRequestLoggingFilter to capture the web request information. The example servlet filter below shows the implementation to capture the request information and log at the end of request processing only for failed requests.

4. Register RequestContextFilter

The last step is to configure RequestContextFilter and register as “bean” in the spring application context. AbstractRequestLoggingFilter allows you to control what information is captured. It is critical to make sure that you configure it so that you are not capturing any sensitive information from a web request.

Example log

Below is the sample request information log for a filed request. You can fine-tune the information to be logged by configuring the RequestContextFilter.

Example failed request information from production system

--

--