Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic context propagation fails in error handler ErrorWebExceptionHandler #34357

Closed
deepak-chhetri opened this issue Feb 3, 2025 · 3 comments
Labels
for: external-project Needs a fix in external project in: web Issues in web modules (web, webmvc, webflux, websocket) status: invalid An issue that we don't feel is valid theme: observability An issue related to observability and tracing

Comments

@deepak-chhetri
Copy link

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 3, 2025
@bclozel
Copy link
Member

bclozel commented Feb 3, 2025

Can you reproduce the problem without Spring Cloud Gateway? If so, please provide a minimal sample application that reproduces the problem so we can have a look.
If you can't reproduce the problem without Spring Cloud Gateway, please report the problem on this issue tracker - a minimal sample project would be also required.

Let us know how this goes. Thanks!

@bclozel bclozel added status: waiting-for-feedback We need additional information before we can continue in: web Issues in web modules (web, webmvc, webflux, websocket) theme: observability An issue related to observability and tracing labels Feb 3, 2025
@deepak-chhetri
Copy link
Author

@bclozel
Thanks for replying..
We found below issue reported in Spring Cloud Gateway community itself which suggests a probable fix using new property spring.reactor.context-propagation=auto introduced in SpringBoot 3.2.x onwards:
spring-cloud/spring-cloud-gateway#3564

We integrated the suggested fix in our API Gateway application, after removing micrometer context-propagation changes, and the logs messages from error handler contained the MDC contextual information. However, the original issue of non-reactive sync MDC logging started popping up.
We brought back micrometer context-propagation changes back with the above property set to auto value in our attempt to fix the original issue however the error log messages again started missing MDC contextual information 😢
We are observing logs closely to find out more anomalies with the micrometer context-propagation and property changes.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Feb 4, 2025
@bclozel
Copy link
Member

bclozel commented Feb 4, 2025

Thanks for the feedback.
I'm closing this now in favor of spring-cloud/spring-cloud-gateway#3564.

If you can reproduce the problem without Spring Cloud Gateway or if the Gateway team manages to isolate it to a Framework problem we will reopen this issue.

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Feb 4, 2025
@bclozel bclozel added status: invalid An issue that we don't feel is valid for: external-project Needs a fix in external project and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project Needs a fix in external project in: web Issues in web modules (web, webmvc, webflux, websocket) status: invalid An issue that we don't feel is valid theme: observability An issue related to observability and tracing
Projects
None yet
Development

No branches or pull requests

3 participants