Jarle Hansen
10/11/2023, 12:23 PMoverride fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
return Mono.deferContextual {
if (it.hasKey(MYID)) {
MDC.putCloseable(MYID, it.get(MYID))
}
chain.filter(exchange)
}
}
The @RestControntroller functions are all suspended, and we are running netty (so full webflux). In the async
code I have also added the MDCContext(), to make sure the MDC values are passed in. Is this a good solution? Are there any other pitfalls I need to be aware of? Or does anyone have a better implementation for this scenarioJacob
10/11/2023, 1:16 PMJarle Hansen
10/11/2023, 1:22 PMKlitos Kyriacou
10/11/2023, 3:00 PMputCloseable
without taking its return value is of no more use than a plain put
.Jarle Hansen
10/12/2023, 6:16 AMJarle Hansen
10/12/2023, 7:17 AM@Component
@Order(Ordered.LOWEST_PRECEDENCE)
class MyMdcFilter : CoWebFilter() {
override suspend fun filter(exchange: ServerWebExchange, chain: CoWebFilterChain) =
withContext(MDCContext()) {
this.coroutineContext[ReactorContext]?.context?.let {
if (it.hasKey(MYID)) {
MDC.put(
MYID, it.get(MYID)
)
}
}
chain.filter(exchange)
}
}
I can see that the MDC is populated, but is empty by the time the @RestController function is calledKlitos Kyriacou
10/12/2023, 8:46 AMJarle Hansen
10/12/2023, 8:49 AMBen
10/12/2023, 4:19 PMJarle Hansen
10/13/2023, 6:32 AMwithLoggingContext
in the context of the controller function