I want to log WebClient's request & response and have used ExchangeFilterFunction.orRequestProcessor to log the request and traceId is still being logged in here, but for the response, traceId is not being logged using ExchangeFilterFunction.ofResponseProcessor

Sample logs Spring traceId not being logged inside of ExchangeFilterFunction.ofResponseProcessor

Sample project to reproduce the issue https://github.com/russelbulanon/tracing-demo

Versions: - Java: 17 - Spring boot: 3.3.1 - Gradle: 8.8

Comment From: bclozel

Thanks for reaching out and for the minimal repro.

While not being ideal, this is unfortunately the best we can do here. We we do instrument the DefaultWebClient, we start the observation before creating the request, and stop it when the response is fully read/consumed. This properly times the exchange and produces the expected metrics.

On the other hand, the current observation context is set up for upstream operators and cannot be applied to operators further down the line. This explains why the request processor shows the traceId but the response processor doesn't. I couldn't find another place to set the context value; Mono<ClientResponse> exchange() is a public method and while it's been deprecated for a while, we cannot assume that developers will chain with another method of ours.