@DataProvider(value = { "true | true", "true | false", "false | true", "false | false", }, splitBy = "\\|") @Test public void doFilterInternal_should_reset_tracing_info_to_whatever_was_on_the_thread_originally( boolean isAsync, boolean throwExceptionInInnerFinallyBlock ) { final RequestTracingFilter filter = getBasicFilter(); if (isAsync) { setupAsyncContextWorkflow(); } RuntimeException exToThrowInInnerFinallyBlock = null; if (throwExceptionInInnerFinallyBlock) { exToThrowInInnerFinallyBlock = new RuntimeException("kaboom"); doThrow(exToThrowInInnerFinallyBlock).when(requestMock).isAsyncStarted(); } Tracer.getInstance().startRequestWithRootSpan("someOutsideSpan"); TracingState originalTracingState = TracingState.getCurrentThreadTracingState(); Throwable ex = catchThrowable( () -> filter.doFilterInternal(requestMock, responseMock, spanCapturingFilterChain) ); if (throwExceptionInInnerFinallyBlock) { assertThat(ex).isSameAs(exToThrowInInnerFinallyBlock); } assertThat(TracingState.getCurrentThreadTracingState()).isEqualTo(originalTracingState); assertThat(spanCapturingFilterChain.capturedSpan).isNotNull(); assertThat(spanCapturingFilterChain.capturedSpan.getTraceId()) .isNotEqualTo(originalTracingState.spanStack.peek().getTraceId()); }
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { TracingState originalThreadInfo = TracingState.getCurrentThreadTracingState(); try { Span overallRequestSpan = createNewSpanForRequest(request); addTracingInfoToRequestAttributes(overallRequestSpan, request); response.setHeader(TraceHeaders.TRACE_ID, overallRequestSpan.getTraceId()); TracingState originalRequestTracingState = TracingState.getCurrentThreadTracingState(); Throwable errorForTagging = null; try { tagAndNamingStrategy.handleRequestTagging(overallRequestSpan, request, tagAndNamingAdapter); filterChain.doFilter(request, response); } catch(Throwable t) { errorForTagging = t; throw t; } finally { if (isAsyncRequest(request)) { setupTracingCompletionWhenAsyncRequestCompletes( request, response, originalRequestTracingState, tagAndNamingStrategy, tagAndNamingAdapter ); } else { try { tagAndNamingStrategy.handleResponseTaggingAndFinalSpanName( overallRequestSpan, request, response, errorForTagging, tagAndNamingAdapter ); } finally { Tracer.getInstance().completeRequestSpan(); } } } } finally { unlinkTracingFromCurrentThread(originalThreadInfo); } }
RequestTracingFilter implements Filter { protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { TracingState originalThreadInfo = TracingState.getCurrentThreadTracingState(); try { Span overallRequestSpan = createNewSpanForRequest(request); addTracingInfoToRequestAttributes(overallRequestSpan, request); response.setHeader(TraceHeaders.TRACE_ID, overallRequestSpan.getTraceId()); TracingState originalRequestTracingState = TracingState.getCurrentThreadTracingState(); Throwable errorForTagging = null; try { tagAndNamingStrategy.handleRequestTagging(overallRequestSpan, request, tagAndNamingAdapter); filterChain.doFilter(request, response); } catch(Throwable t) { errorForTagging = t; throw t; } finally { if (isAsyncRequest(request)) { setupTracingCompletionWhenAsyncRequestCompletes( request, response, originalRequestTracingState, tagAndNamingStrategy, tagAndNamingAdapter ); } else { try { tagAndNamingStrategy.handleResponseTaggingAndFinalSpanName( overallRequestSpan, request, response, errorForTagging, tagAndNamingAdapter ); } finally { Tracer.getInstance().completeRequestSpan(); } } } } finally { unlinkTracingFromCurrentThread(originalThreadInfo); } } }
RequestTracingFilter implements Filter { protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { TracingState originalThreadInfo = TracingState.getCurrentThreadTracingState(); try { Span overallRequestSpan = createNewSpanForRequest(request); addTracingInfoToRequestAttributes(overallRequestSpan, request); response.setHeader(TraceHeaders.TRACE_ID, overallRequestSpan.getTraceId()); TracingState originalRequestTracingState = TracingState.getCurrentThreadTracingState(); Throwable errorForTagging = null; try { tagAndNamingStrategy.handleRequestTagging(overallRequestSpan, request, tagAndNamingAdapter); filterChain.doFilter(request, response); } catch(Throwable t) { errorForTagging = t; throw t; } finally { if (isAsyncRequest(request)) { setupTracingCompletionWhenAsyncRequestCompletes( request, response, originalRequestTracingState, tagAndNamingStrategy, tagAndNamingAdapter ); } else { try { tagAndNamingStrategy.handleResponseTaggingAndFinalSpanName( overallRequestSpan, request, response, errorForTagging, tagAndNamingAdapter ); } finally { Tracer.getInstance().completeRequestSpan(); } } } } finally { unlinkTracingFromCurrentThread(originalThreadInfo); } }  }
RequestTracingFilter implements Filter { protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { TracingState originalThreadInfo = TracingState.getCurrentThreadTracingState(); try { Span overallRequestSpan = createNewSpanForRequest(request); addTracingInfoToRequestAttributes(overallRequestSpan, request); response.setHeader(TraceHeaders.TRACE_ID, overallRequestSpan.getTraceId()); TracingState originalRequestTracingState = TracingState.getCurrentThreadTracingState(); Throwable errorForTagging = null; try { tagAndNamingStrategy.handleRequestTagging(overallRequestSpan, request, tagAndNamingAdapter); filterChain.doFilter(request, response); } catch(Throwable t) { errorForTagging = t; throw t; } finally { if (isAsyncRequest(request)) { setupTracingCompletionWhenAsyncRequestCompletes( request, response, originalRequestTracingState, tagAndNamingStrategy, tagAndNamingAdapter ); } else { try { tagAndNamingStrategy.handleResponseTaggingAndFinalSpanName( overallRequestSpan, request, response, errorForTagging, tagAndNamingAdapter ); } finally { Tracer.getInstance().completeRequestSpan(); } } } } finally { unlinkTracingFromCurrentThread(originalThreadInfo); } }  @Override @SuppressWarnings("RedundantThrows") void init(FilterConfig filterConfig); @Override void destroy(); @Override void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain filterChain); }
RequestTracingFilter implements Filter { protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { TracingState originalThreadInfo = TracingState.getCurrentThreadTracingState(); try { Span overallRequestSpan = createNewSpanForRequest(request); addTracingInfoToRequestAttributes(overallRequestSpan, request); response.setHeader(TraceHeaders.TRACE_ID, overallRequestSpan.getTraceId()); TracingState originalRequestTracingState = TracingState.getCurrentThreadTracingState(); Throwable errorForTagging = null; try { tagAndNamingStrategy.handleRequestTagging(overallRequestSpan, request, tagAndNamingAdapter); filterChain.doFilter(request, response); } catch(Throwable t) { errorForTagging = t; throw t; } finally { if (isAsyncRequest(request)) { setupTracingCompletionWhenAsyncRequestCompletes( request, response, originalRequestTracingState, tagAndNamingStrategy, tagAndNamingAdapter ); } else { try { tagAndNamingStrategy.handleResponseTaggingAndFinalSpanName( overallRequestSpan, request, response, errorForTagging, tagAndNamingAdapter ); } finally { Tracer.getInstance().completeRequestSpan(); } } } } finally { unlinkTracingFromCurrentThread(originalThreadInfo); } }  @Override @SuppressWarnings("RedundantThrows") void init(FilterConfig filterConfig); @Override void destroy(); @Override void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain filterChain); static final String FILTER_HAS_ALREADY_EXECUTED_ATTRIBUTE; @Deprecated
    static final String ERROR_REQUEST_URI_ATTRIBUTE; static final String USER_ID_HEADER_KEYS_LIST_INIT_PARAM_NAME; static final String TAG_AND_SPAN_NAMING_STRATEGY_INIT_PARAM_NAME; static final String TAG_AND_SPAN_NAMING_ADAPTER_INIT_PARAM_NAME; }