-
Notifications
You must be signed in to change notification settings - Fork 38.9k
Description
Given that automatic Micrometer context propagation for MDC is enabled in a WebFlux application,
When I call an endpoint that returns a Kotlin Flow,
Then I expect MDC values to be visible inside the flow-collector coroutine.
Actual: MDC values are only propagated correctly in suspend controller methods, not in Flow endpoints.
I'm using Spring Boot 4.0.3.
A complete app is in the linked repo (https://github.com/sam-ov/flow-context-propagation) but essentially I set up context propagation like this:
Hooks.enableAutomaticContextPropagation()
ContextRegistry.getInstance().registerThreadLocalAccessor(Slf4jThreadLocalAccessor())
When I call a suspend controller method, context propagation is working correctly. The coroutine context contains a PropagationContextElement, and the coroutine carries MDC context with it when it switches thread.
@GetMapping("/suspend")
suspend fun suspendEndpoint(): Something {
// ✅ MDC propagation is working here
}But when I call an endpoint that returns a Flow:
- The coroutine context does not contain
PropagationContextElement - The MDC context is either not present at all, or is lost when the coroutine suspends or switches thread
@GetMapping("/flow")
fun flowEndpoint(): Flow<Something> = flow {
// ❌ MDC propagation is not working here
}I would expect the coroutine that collects the Flow to have context propagation enabled in the same way as the coroutine that executes a suspend controller method, so I think this is a bug/omission in Spring's context propagation setup.
The minimal reproduction code is here: https://github.com/sam-ov/flow-context-propagation
The repository contains a passing test for the /suspend endpoint and a failing test for the /flow endpoint.