FunctionWithTracing implements Function<T, U> { @Override public U apply(T t) { TracingState originalThreadInfo = null; try { originalThreadInfo = linkTracingToCurrentThread(spanStackForExecution, mdcContextMapForExecution); return origFunction.apply(t); } finally { unlinkTracingFromCurrentThread(originalThreadInfo); } } FunctionWithTracing(Function<T, U> origFunction);  FunctionWithTracing(Function<T, U> origFunction,
                               Pair<Deque<Span>, Map<String, String>> originalThreadInfo);  FunctionWithTracing(Function<T, U> origFunction,
                               Deque<Span> spanStackForExecution,
                               Map<String, String> mdcContextMapForExecution); static FunctionWithTracing<T, U> withTracing(Function<T, U> origFunction); static FunctionWithTracing<T, U> withTracing(
        Function<T, U> origFunction,
        Pair<Deque<Span>, Map<String, String>> originalThreadInfo
    ); static FunctionWithTracing<T, U> withTracing(Function<T, U> origFunction,
                                                               Deque<Span> spanStackForExecution,
                                                               Map<String, String> mdcContextMapForExecution); @Override U apply(T t);  }
@DataProvider(value = { "true", "false" }) @Test public void apply_handles_tracing_and_mdc_info_as_expected(boolean throwException) { throwExceptionDuringCall = throwException; Tracer.getInstance().startRequestWithRootSpan("foo"); Deque<Span> spanStack = Tracer.getInstance().getCurrentSpanStackCopy(); Map<String, String> mdcInfo = MDC.getCopyOfContextMap(); FunctionWithTracing instance = new FunctionWithTracing( functionMock, spanStack, mdcInfo ); resetTracing(); assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull(); assertThat(MDC.getCopyOfContextMap()).isNullOrEmpty(); Throwable ex = null; Object result = null; try { result = instance.apply(inObj); } catch(Throwable t) { ex = t; } verify(functionMock).apply(inObj); if (throwException) { assertThat(ex).isNotNull(); assertThat(result).isNull(); } else { assertThat(ex).isNull(); assertThat(result).isSameAs(outObj); } assertThat(currentSpanStackWhenFunctionWasCalled.get(0)).isEqualTo(spanStack); assertThat(currentMdcInfoWhenFunctionWasCalled.get(0)).isEqualTo(mdcInfo); assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull(); assertThat(MDC.getCopyOfContextMap()).isNullOrEmpty(); }