Defer ExchangeFilterFunction execution in WebClient
Prior to this commit, the `DefaultWebClient` would execute the configured `ExchangeFilterFunction` as the reactive pipeline is assembled during subscription. This means that if imperative code is executed in a filter function, it won't be aware of the current observation through the local scope. For example, when automatic context propagation is enabled for Reactor operators, the logger MDC will not know about the current traceId/spanId. This commit ensures that client filter functions execution is deferred during the actual client exchange. Fixes gh-33559
This commit is contained in:
@@ -453,8 +453,9 @@ final class DefaultWebClient implements WebClient {
|
||||
ClientRequest request = requestBuilder.build();
|
||||
observationContext.setUriTemplate((String) request.attribute(URI_TEMPLATE_ATTRIBUTE).orElse(null));
|
||||
observationContext.setRequest(request);
|
||||
Mono<ClientResponse> responseMono = filterFunction.apply(exchangeFunction)
|
||||
.exchange(request)
|
||||
final ExchangeFilterFunction finalFilterFunction = filterFunction;
|
||||
Mono<ClientResponse> responseMono = Mono.defer(
|
||||
() -> finalFilterFunction.apply(exchangeFunction).exchange(request))
|
||||
.checkpoint("Request to " +
|
||||
WebClientUtils.getRequestDescription(request.method(), request.url()) +
|
||||
" [DefaultWebClient]")
|
||||
|
||||
Reference in New Issue
Block a user