Package com.fayupable.logged.spring.http
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 -
Method Summary
Modifier and TypeMethodDescription@NonNull org.springframework.http.client.ClientHttpResponseintercept(@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 ontorequestbefore delegating toexecution.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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 ontorequestbefore delegating toexecution.- Specified by:
interceptin interfaceorg.springframework.http.client.ClientHttpRequestInterceptor- Parameters:
request- the outgoing requestbody- the outgoing request body, passed through unchangedexecution- delegates the actual request execution- Returns:
- the response returned by
execution - Throws:
IOException
-