concept correlation ID in category spring

This is an excerpt from Manning's book Spring Microservices in Action, 2E MEAP V07.
Log correlation. How do you tie together all the logs produced between services for a single user transaction? With this pattern, we’ll look at how to implement a correlation ID, which is a unique identifier that will be carried across all service calls in a transaction and can be used to tie together log entries produced from each service.
Figure 7.11 Adding a correlation ID to the licensing service call’s HTTP Header.
![]()
Tracking Filter. The TrackingFilter will be a pre-filter that will ensure that every request flowing from the gateway has a correlation ID associated with it. A correlation ID is a unique ID that gets carried across all the microservices that are executed when carrying out a customer request. A correlation ID allows us to trace the chain of events that occur as a call goes through a series of microservice calls.
Remember, the gateway executes the actual HTTP call on behalf of the service client and has the opportunity to inspect the response back from the target service call and then alter the response or decorate it with additional information. When coupled with capturing data with the pre-filter, a gateway post filter is an ideal location to collect metrics and complete any logging associated with the user’s transaction. We’ll want to take advantage of this by injecting the correlation ID that we’ve been passing around to our microservices back to the user.
We’re going to do this by using a gateway post filter to inject the correlation ID back into the HTTP response headers being passed back to the caller of the service. This way, we can pass the correlation ID back to the caller without ever having to touch the message body. The following listing 8.17 shows the code for building a post filter. This code can be found in /gatewayserver/src/main/java/com/optimagrowth/gateway/filters/ResponseFilter.java.
Listing 8.17 Inject the correlation ID into the HTTP response
@Configuration public class ResponseFilter { final Logger logger =LoggerFactory.getLogger(ResponseFilter.class); @Autowired FilterUtils filterUtils; @Bean public GlobalFilter postGlobalFilter() { return (exchange, chain) -> { return chain.filter(exchange).then(Mono.fromRunnable(() -> { HttpHeaders requestHeaders = exchange.getRequest().getHeaders(); String correlationId = filterUtils.getCorrelationId(requestHeaders); #A logger.debug("Adding the correlation id to the outbound headers. {}", correlationId); exchange.getResponse().getHeaders().add(FilterUtils.CORRELATION_ID, #B correlationId); logger.debug("Completing outgoing request for {}.", #C exchange.getRequest().getURI()); })); }; } }#A Grab the correlation ID that was passed in on the original HTTP request.
#B Inject the correlation ID into the response.

This is an excerpt from Manning's book Spring Microservices in Action.
Log correlation— How do you tie together all the logs produced between services for a single user transaction? With this pattern, we’ll look at how to implement a correlation ID, which is a unique identifier that will be carried across all service calls in a transaction and can be used to tie together log entries produced from each service.
TrackingFilter—The TrackingFilter will be a pre-filter that will ensure that every request flowing from Zuul has a correlation ID associated with it. A correlation ID is a unique ID that gets carried across all the microservices that are executed when carrying out a customer request. A correlation ID allows you to trace the chain of events that occur as a call goes through a series of microservice calls.
To implement this, you’re going to build a set of three classes into each of your microservices. These classes will work together to read the correlation ID (along with other information you’ll add later) off the incoming HTTP request, map it to a class that’s easily accessible and useable by the business logic in the application, and then ensure that the correlation ID is propagated to any downstream service calls.
The first class you’re going to build is the UserContextFilter class. This class is an HTTP servlet filter that will intercept all incoming HTTP requests coming into the service and map the correlation ID (and a few other values) from the HTTP request to the UserContext class. The following listing shows the code for the UserContext class. The source for this class can be found in licensing-service/src/main/java/com/thoughtmechanix/licenses/utils/UserContextFilter.java.