When using WebClient in servlet environment, MDC is one of the pain point that requires boilerplate code. Since RestTemplate is in maintenance mode, more and more apps would choose to use WebClient. It would be very helpful that spring provides out of the box support for MDC for the use of WebClient. Then, it would be a smooth ride migrating RestTemplate to WebClient.

Since spring already detects underlying logging framework, it could be implemented agnostic to the actual logging framework.

What needs to be implemented:

  • Pass MDC values from main thread to reactor thread In servlet environment, it is required to propagate MDC context values from servlet thread to reactor thread. To implement, ExchangeFilterFunction or some sort of hook which should apply as the first action of response operators.

  • Pass around MDC values to subscriber context within reactor schedulers/operators My suggestion to reactor-addons. https://github.com/reactor/reactor-addons/issues/219

If such boilerplate code is provided from spring, then, spring-boot may auto-configure the MDC support.

Relates to https://github.com/reactor/reactor-core/issues/1985

Comment From: northernbird

Hello, I also need this feature. Could someone provide any updated info?

Comment From: Anonymous-Coward

The problem exists for filters in spring cloud gateway too, not just for WebClient. I looked at the logback code on github. I suppose the problem can be solved at the very root of it if spring webflux injects its own MDC adapter implementation - that one can bind to the exchange, rather than the default which binds to the thread.

Comment From: bclozel

Closing as this is not specific to WebClient, but rather a general context propagation issue with ThreadLocals. This should be addressed in Reactor directly. Work has been done in Spring GraphQL in that direction.