Class HttpTraceExchangeFilterFunction
- All Implemented Interfaces:
org.springframework.web.reactive.function.client.ExchangeFilterFunction
ExchangeFilterFunction that writes the current @Logged
call chain's trace id and depth into an outgoing WebClient
request's headers, via HttpTraceHeaderCarrier.
Thread caveat, unlike HttpTraceClientHttpRequestInterceptor
and FeignTraceRequestInterceptor: WebClient is
reactive, and filter(org.springframework.web.reactive.function.client.ClientRequest, org.springframework.web.reactive.function.client.ExchangeFunction) runs whenever the returned Mono is
actually subscribed to — which, if the request is built with
subscribeOn/publishOn or otherwise composed across
schedulers, may not be the same thread that constructed the
WebClient call in application code. In that case, this filter
captures whatever FlowContext is
active on the thread that actually triggers the exchange, which is not
guaranteed to be the calling thread's. For the common case of a
@Logged method calling WebClient and blocking on the
result (for example via .block()), the subscribing thread and the
calling thread are the same, and this behaves exactly like the
RestTemplate and Feign integrations. Full correctness across an
arbitrarily composed reactive chain would require bridging
FlowContext through Reactor's own Context, which this library
does not yet do (see the reactive Publisher return type
limitation documented on LoggedTargetGuardBeanPostProcessor).
Register this on the specific WebClient instance you want to
carry trace context across service calls:
WebClient webClient = WebClient.builder()
.filter(new HttpTraceExchangeFilterFunction())
.build();
Not wired in automatically: a consuming application decides which
WebClient instances should carry trace context, the same way it
decides which methods get @Logged in the first place.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription@NonNull reactor.core.publisher.Mono<org.springframework.web.reactive.function.client.ClientResponse> filter(@NonNull org.springframework.web.reactive.function.client.ClientRequest request, @NonNull org.springframework.web.reactive.function.client.ExchangeFunction next) Writes the current call chain's trace headers onto a copy ofrequestbefore delegating tonext.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.web.reactive.function.client.ExchangeFilterFunction
andThen, apply
-
Constructor Details
-
HttpTraceExchangeFilterFunction
public HttpTraceExchangeFilterFunction()
-
-
Method Details
-
filter
public @NonNull reactor.core.publisher.Mono<org.springframework.web.reactive.function.client.ClientResponse> filter(@NonNull org.springframework.web.reactive.function.client.ClientRequest request, @NonNull org.springframework.web.reactive.function.client.ExchangeFunction next) Writes the current call chain's trace headers onto a copy ofrequestbefore delegating tonext.- Specified by:
filterin interfaceorg.springframework.web.reactive.function.client.ExchangeFilterFunction- Parameters:
request- the outgoing requestnext- delegates the actual exchange- Returns:
- the response
Monoreturned bynext
-