Skip to content

Out of the box MDC support in WebClient #24416

Closed
@ttddyy

Description

@ttddyy
Contributor

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. Add-on to support MDC reactor/reactor-addons#219

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

Relates to reactor/reactor-core#1985

Activity

northernbird

northernbird commented on Dec 14, 2020

@northernbird

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

Anonymous-Coward

Anonymous-Coward commented on May 13, 2021

@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.

added
in: webIssues in web modules (web, webmvc, webflux, websocket)
on Nov 8, 2021
bclozel

bclozel commented on Feb 18, 2022

@bclozel
Member

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.

added
status: declinedA suggestion or change that we don't feel we should currently apply
and removed on Feb 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @bclozel@rstoyanchev@ttddyy@Anonymous-Coward@spring-projects-issues

        Issue actions

          Out of the box MDC support in WebClient · Issue #24416 · spring-projects/spring-framework