@Test public void testDoFilterWithTraceHeaderClearMocked() throws Exception { final String requestHeadTraceId = RandomStringUtils.randomAlphanumeric(TRACE_ID_SIZE); HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); doReturn(requestHeadTraceId).when(request).getHeader(TRACE_ID); FilterChain filterChain = mock(FilterChain.class); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocationOnMock) throws Throwable { assertTrue(MDC.get(TRACE_ID).startsWith(requestHeadTraceId)); return null; } }).when(filterChain).doFilter(request, response); webHookTracingFilter.doFilter(request, mock(HttpServletResponse.class), mock(FilterChain.class)); }
@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; String xTraceInHeader = request.getHeader(TRACE_ID); if (isBlank(xTraceInHeader)) { DistributedTracingUtils.setMDC(); xTraceInHeader = DistributedTracingUtils.getMDC(); LOG.info("Starting trace for request {}", request.getRequestURL()); } else { DistributedTracingUtils.setMDC(xTraceInHeader); LOG.info("Continuing trace for request {}", request.getRequestURL()); } HttpServletResponse response = (HttpServletResponse) servletResponse; response.setHeader(TRACE_ID, xTraceInHeader); LOG.info("Trace {} added to response.", xTraceInHeader); filterChain.doFilter(servletRequest, servletResponse); DistributedTracingUtils.clearMDC(); }
WebHookTracingFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; String xTraceInHeader = request.getHeader(TRACE_ID); if (isBlank(xTraceInHeader)) { DistributedTracingUtils.setMDC(); xTraceInHeader = DistributedTracingUtils.getMDC(); LOG.info("Starting trace for request {}", request.getRequestURL()); } else { DistributedTracingUtils.setMDC(xTraceInHeader); LOG.info("Continuing trace for request {}", request.getRequestURL()); } HttpServletResponse response = (HttpServletResponse) servletResponse; response.setHeader(TRACE_ID, xTraceInHeader); LOG.info("Trace {} added to response.", xTraceInHeader); filterChain.doFilter(servletRequest, servletResponse); DistributedTracingUtils.clearMDC(); } }
WebHookTracingFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; String xTraceInHeader = request.getHeader(TRACE_ID); if (isBlank(xTraceInHeader)) { DistributedTracingUtils.setMDC(); xTraceInHeader = DistributedTracingUtils.getMDC(); LOG.info("Starting trace for request {}", request.getRequestURL()); } else { DistributedTracingUtils.setMDC(xTraceInHeader); LOG.info("Continuing trace for request {}", request.getRequestURL()); } HttpServletResponse response = (HttpServletResponse) servletResponse; response.setHeader(TRACE_ID, xTraceInHeader); LOG.info("Trace {} added to response.", xTraceInHeader); filterChain.doFilter(servletRequest, servletResponse); DistributedTracingUtils.clearMDC(); }  }
WebHookTracingFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; String xTraceInHeader = request.getHeader(TRACE_ID); if (isBlank(xTraceInHeader)) { DistributedTracingUtils.setMDC(); xTraceInHeader = DistributedTracingUtils.getMDC(); LOG.info("Starting trace for request {}", request.getRequestURL()); } else { DistributedTracingUtils.setMDC(xTraceInHeader); LOG.info("Continuing trace for request {}", request.getRequestURL()); } HttpServletResponse response = (HttpServletResponse) servletResponse; response.setHeader(TRACE_ID, xTraceInHeader); LOG.info("Trace {} added to response.", xTraceInHeader); filterChain.doFilter(servletRequest, servletResponse); DistributedTracingUtils.clearMDC(); }  @Override void init(FilterConfig config); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain); @Override void destroy(); }
WebHookTracingFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; String xTraceInHeader = request.getHeader(TRACE_ID); if (isBlank(xTraceInHeader)) { DistributedTracingUtils.setMDC(); xTraceInHeader = DistributedTracingUtils.getMDC(); LOG.info("Starting trace for request {}", request.getRequestURL()); } else { DistributedTracingUtils.setMDC(xTraceInHeader); LOG.info("Continuing trace for request {}", request.getRequestURL()); } HttpServletResponse response = (HttpServletResponse) servletResponse; response.setHeader(TRACE_ID, xTraceInHeader); LOG.info("Trace {} added to response.", xTraceInHeader); filterChain.doFilter(servletRequest, servletResponse); DistributedTracingUtils.clearMDC(); }  @Override void init(FilterConfig config); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain); @Override void destroy();  }