PredicateWithTracing implements Predicate<T> { @Override public boolean test(T o) { TracingState originalThreadInfo = null; try { originalThreadInfo = linkTracingToCurrentThread(spanStackForExecution, mdcContextMapForExecution); return origPredicate.test(o); } finally { unlinkTracingFromCurrentThread(originalThreadInfo); } } PredicateWithTracing(Predicate<T> origPredicate);  PredicateWithTracing(Predicate<T> origPredicate,
                                Pair<Deque<Span>, Map<String, String>> originalThreadInfo);  PredicateWithTracing(Predicate<T> origPredicate,
                                Deque<Span> spanStackForExecution,
                                Map<String, String> mdcContextMapForExecution); static PredicateWithTracing<T> withTracing(Predicate<T> origPredicate); static PredicateWithTracing<T> withTracing(Predicate<T> origPredicate,
                                                          Pair<Deque<Span>, Map<String, String>> originalThreadInfo); static PredicateWithTracing<T> withTracing(Predicate<T> origPredicate,
                                                          Deque<Span> spanStackForExecution,
                                                          Map<String, String> mdcContextMapForExecution); @Override boolean test(T o);  }
@DataProvider(value = { "true | true", "true | false", "false | true", "false | false" }, splitBy = "\\|") @Test public void test_handles_tracing_and_mdc_info_as_expected(boolean throwException, boolean predicateReturnVal) { throwExceptionDuringCall = throwException; returnValIfNoException = predicateReturnVal; Tracer.getInstance().startRequestWithRootSpan("foo"); Deque<Span> spanStack = Tracer.getInstance().getCurrentSpanStackCopy(); Map<String, String> mdcInfo = MDC.getCopyOfContextMap(); PredicateWithTracing instance = new PredicateWithTracing( predicateMock, spanStack, mdcInfo ); resetTracing(); assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull(); assertThat(MDC.getCopyOfContextMap()).isNullOrEmpty(); Throwable ex = null; Boolean result = null; try { result = instance.test(inObj); } catch(Throwable t) { ex = t; } verify(predicateMock).test(inObj); if (throwException) { assertThat(ex).isNotNull(); assertThat(result).isNull(); } else { assertThat(ex).isNull(); assertThat(result).isEqualTo(predicateReturnVal); } assertThat(currentSpanStackWhenPredicateWasCalled.get(0)).isEqualTo(spanStack); assertThat(currentMdcInfoWhenPredicateWasCalled.get(0)).isEqualTo(mdcInfo); assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull(); assertThat(MDC.getCopyOfContextMap()).isNullOrEmpty(); }