Class HttpTraceServletFilter
- All Implemented Interfaces:
jakarta.servlet.Filter,org.springframework.beans.factory.Aware,org.springframework.beans.factory.BeanNameAware,org.springframework.beans.factory.DisposableBean,org.springframework.beans.factory.InitializingBean,org.springframework.context.EnvironmentAware,org.springframework.core.env.EnvironmentCapable,org.springframework.web.context.ServletContextAware
Filter that reads the incoming HTTP
request's trace headers, if present, and adopts them as the active
@Logged call chain for the duration of the request, via
HttpTraceHeaderCarrier.
This is the inbound counterpart to
HttpTraceClientHttpRequestInterceptor/FeignTraceRequestInterceptor/HttpTraceExchangeFilterFunction: a
service that only sends the outbound trace headers but never reads them
back on the receiving side would never actually observe the propagated
chain continue — this filter is the other half of that exchange.
Register as a Spring bean, with high precedence so the adopted context
is active for as much of the request-handling chain as possible,
including any @Logged controller/service methods invoked further
down the chain:
@Bean
public FilterRegistrationBean<HttpTraceServletFilter> httpTraceServletFilter() {
FilterRegistrationBean<HttpTraceServletFilter> registration =
new FilterRegistrationBean<>(new HttpTraceServletFilter());
registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
return registration;
}
Not wired in automatically: a consuming application decides whether it
wants incoming requests to participate in cross-service tracing, the same
way it decides which methods get @Logged in the first place.
-
Field Summary
Fields inherited from class org.springframework.web.filter.OncePerRequestFilter
ALREADY_FILTERED_SUFFIXFields inherited from class org.springframework.web.filter.GenericFilterBean
logger -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voiddoFilterInternal(@NonNull jakarta.servlet.http.HttpServletRequest request, @NonNull jakarta.servlet.http.HttpServletResponse response, @NonNull jakarta.servlet.FilterChain filterChain) Reads the incoming request's trace headers, if present, and runs the rest of the filter chain with them adopted as the active call chain for this thread, restoring this thread's previous context again afterward, regardless of whether the chain completes normally or throws.Methods inherited from class org.springframework.web.filter.OncePerRequestFilter
doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterAsyncDispatch, shouldNotFilterErrorDispatchMethods inherited from class org.springframework.web.filter.GenericFilterBean
addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
-
Constructor Details
-
HttpTraceServletFilter
public HttpTraceServletFilter()
-
-
Method Details
-
doFilterInternal
protected void doFilterInternal(@NonNull jakarta.servlet.http.HttpServletRequest request, @NonNull jakarta.servlet.http.HttpServletResponse response, @NonNull jakarta.servlet.FilterChain filterChain) throws jakarta.servlet.ServletException, IOException Reads the incoming request's trace headers, if present, and runs the rest of the filter chain with them adopted as the active call chain for this thread, restoring this thread's previous context again afterward, regardless of whether the chain completes normally or throws.- Specified by:
doFilterInternalin classorg.springframework.web.filter.OncePerRequestFilter- Parameters:
request- the incoming HTTP requestresponse- the outgoing HTTP response, passed through unchangedfilterChain- the rest of the filter chain- Throws:
jakarta.servlet.ServletExceptionIOException
-