Description
Spring: 6.1.1
Spring Boot: 3.2.0
Spring Cloud Gateway: 4.1.0
Project Reactor Core: 3.6.0
Micrometer context-propagation: 1.1.2
We have develop API Gateway application using Spring Cloud Gateway framework.
We have used Logback library for logging purpose and integrated MDC for contextual diagnostic information (like request tracking id) propagation in log messages.
We have used automatic context propagation feature of context-propagation library in order to implement MDC contextual diagnostic mapping in reactive style.
Application Main Class Code Snippet:
@PostConstruct
public void init() {
Hooks.enableAutomaticContextPropagation();
final String key = "REQ_TRACKING_ID_KEY";
ContextRegistry.getInstance().registerThreadLocalAccessor(
key,
() -> MDC.get(),
value -> MDC.put(key, value),
() -> MDC.remove(key))
);
}
Filter class with Reactive Context Write:
public Mono<Void> filter(final ServerWebExchange exchange, final GatewayFilterChain chain) {
final String reqTrackingId = UUID.randomUUID().toString();
MDC.put("REQ_TRACKING_ID_KEY", reqTrackingId);
return chain.filter(exchange).contextWrite(Context.of("REQ_TRACKING_ID_KEY", reqTrackingId));
}
Exception is thrown in the filter that is after request tracking id is uploaded in Reactive Context above.
The automatic context propagation is working correctly in the normal processing of request by the application using MDC and logback libraries, however it seems not working when any exception/error is thrown during the request processing.
We have implemented custom exception handler by implementing ErrorWebExceptionHandler handle(exchange, exception) method. Now we observe that the request tracking id is not getting logged in any of log messages written in the statements of custom exception handler.
We found following similar issue reported in this community however it is marked as fixed in Spring 6.1.0-M1 release:
#30013
Tried to search for the proper solution for the issue however unable to find the fix for it.
Any help would be highly appreciated. Thanks in advance.