-
Notifications
You must be signed in to change notification settings - Fork 38.1k
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
Spring Webflux: CoRouterFunctionDsl should expose the used CoroutineContext #27010
Comments
@sdeleuze Could you take a look at this one, please? |
@poutsma Do we provide a way to do the same thing on Reactive side via Reactor context? If not, what would that look like and would it b something we want to implement? |
@sdeleuze I think in a pure Reactor implementation this is easier since the call chain "stays inside the framework". It can be easily done by adding a |
I am not sure how this should be implemented and exposed, but indeed getting access to the |
I am considering adding a coRouter {
context { request ->
delay(1) // Can invoke suspending functions
CoroutineName(request.headers().firstHeader("Custom-Header")!!)
}
GET("/") {
// ...
}
} |
That seems like pretty much exactly what would help me! E.g. something like this is what I do at the moment) coRouter {
context { request ->
MDC.put("traceId", req.header("traceId"))
MDC.put("userId", req.awaitPrincipal().name)
// Not sure if NonCancellable will work like this, in my tests the mono() creator did not accept it
kotlinx.coroutines.slf4j.MDCContext() + NonCancellable
}
// mappings
} |
Merged, please try |
I just tested it in a small application and it worked fine! |
Affects: 5.3.7
I am using the
CoRouterFunctionDsl
to build a Spring Webflux application withsuspend
handlers in Kotlin. Recently I wanted to add some SLF4JMDC
values (e.g. an AWS X-Ray Trace id) which requires the usage of https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-thread-context-element/.MDC
isThreadLocal
based which does not play well with coroutines.Adding such a
ThreadContextElement
can be done in a generic way (so it doesn't have to be repeated in each handler) in the private methodasHandlerFunction
in the classCoRouterFunctionDsl
:but there is no way of doing that. For now I copied the class and changed the function myself. The data I need there is based on the subscriber context of the Mono (coming from a
WebFilter
) and theServerRequest
, so it would be neat to inject something likecoroutineContextProvider: (req: ServerRequest) -> Mono<CoroutineContext>
into the DSL:I hope that makes sense. If there is an easier solution that I oversaw I am of course also open to that.
The text was updated successfully, but these errors were encountered: