Class HttpTraceClientHttpRequestInterceptor

java.lang.Object
com.fayupable.logged.spring.http.HttpTraceClientHttpRequestInterceptor
All Implemented Interfaces:
org.springframework.http.client.ClientHttpRequestInterceptor

public final class HttpTraceClientHttpRequestInterceptor extends Object implements org.springframework.http.client.ClientHttpRequestInterceptor
ClientHttpRequestInterceptor that writes the current @Logged call chain's trace id and depth into an outgoing RestTemplate request's headers, via HttpTraceHeaderCarrier.

RestTemplate is purely synchronous/blocking, so — unlike HttpTraceExchangeFilterFunction for WebClient — there is no ambiguity about which thread's FlowContext is captured: it is always the thread that made the RestTemplate call.

Register this on the specific RestTemplate instance you want to carry trace context across service calls:


 RestTemplate restTemplate = new RestTemplate();
 restTemplate.getInterceptors().add(new HttpTraceClientHttpRequestInterceptor());
 

Not wired in automatically: a consuming application decides which RestTemplate 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 org.springframework.http.client.ClientHttpResponse
    intercept(@NonNull org.springframework.http.HttpRequest request, @org.jspecify.annotations.NonNull byte[] body, @NonNull org.springframework.http.client.ClientHttpRequestExecution execution)
    Writes the current call chain's trace headers onto request before delegating to execution.

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface org.springframework.http.client.ClientHttpRequestInterceptor

    andThen, apply
  • Constructor Details

    • HttpTraceClientHttpRequestInterceptor

      public HttpTraceClientHttpRequestInterceptor()
  • Method Details

    • intercept

      public @NonNull org.springframework.http.client.ClientHttpResponse intercept(@NonNull org.springframework.http.HttpRequest request, @org.jspecify.annotations.NonNull byte[] body, @NonNull org.springframework.http.client.ClientHttpRequestExecution execution) throws IOException
      Writes the current call chain's trace headers onto request before delegating to execution.
      Specified by:
      intercept in interface org.springframework.http.client.ClientHttpRequestInterceptor
      Parameters:
      request - the outgoing request
      body - the outgoing request body, passed through unchanged
      execution - delegates the actual request execution
      Returns:
      the response returned by execution
      Throws:
      IOException