Class HttpTraceServletFilter

java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
com.fayupable.logged.spring.http.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

public final class HttpTraceServletFilter extends org.springframework.web.filter.OncePerRequestFilter
Servlet 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_SUFFIX

    Fields inherited from class org.springframework.web.filter.GenericFilterBean

    logger
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    doFilterInternal(@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, shouldNotFilterErrorDispatch

    Methods inherited from class org.springframework.web.filter.GenericFilterBean

    addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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:
      doFilterInternal in class org.springframework.web.filter.OncePerRequestFilter
      Parameters:
      request - the incoming HTTP request
      response - the outgoing HTTP response, passed through unchanged
      filterChain - the rest of the filter chain
      Throws:
      jakarta.servlet.ServletException
      IOException