Class HttpTraceExchangeFilterFunction

java.lang.Object
com.fayupable.logged.spring.http.HttpTraceExchangeFilterFunction
All Implemented Interfaces:
org.springframework.web.reactive.function.client.ExchangeFilterFunction

public final class HttpTraceExchangeFilterFunction extends Object implements 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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    @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 of request before delegating to next.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods 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 of request before delegating to next.
      Specified by:
      filter in interface org.springframework.web.reactive.function.client.ExchangeFilterFunction
      Parameters:
      request - the outgoing request
      next - delegates the actual exchange
      Returns:
      the response Mono returned by next