ConsumerWithTracing implements Consumer<T> { @Override public void accept(T t) { TracingState originalThreadInfo = null; try { originalThreadInfo = linkTracingToCurrentThread(spanStackForExecution, mdcContextMapForExecution); origConsumer.accept(t); } finally { unlinkTracingFromCurrentThread(originalThreadInfo); } } ConsumerWithTracing(Consumer<T> origConsumer);  ConsumerWithTracing(Consumer<T> origConsumer,
                               Pair<Deque<Span>, Map<String, String>> originalThreadInfo);  ConsumerWithTracing(Consumer<T> origConsumer,
                               Deque<Span> spanStackForExecution,
                               Map<String, String> mdcContextMapForExecution); static ConsumerWithTracing<T> withTracing(Consumer<T> origConsumer); static ConsumerWithTracing<T> withTracing(Consumer<T> origConsumer,
                                                         Pair<Deque<Span>, Map<String, String>> originalThreadInfo); static ConsumerWithTracing<T> withTracing(Consumer<T> origConsumer,
                                                         Deque<Span> spanStackForExecution,
                                                         Map<String, String> mdcContextMapForExecution); @Override void accept(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(); ConsumerWithTracing instance = new ConsumerWithTracing( consumerMock, spanStack, mdcInfo ); resetTracing(); assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull(); assertThat(MDC.getCopyOfContextMap()).isNullOrEmpty(); Throwable ex = catchThrowable(() -> instance.accept(inObj)); verify(consumerMock).accept(inObj); if (throwException) { assertThat(ex).isNotNull(); } else { assertThat(ex).isNull(); } assertThat(currentSpanStackWhenConsumerWasCalled.get(0)).isEqualTo(spanStack); assertThat(currentMdcInfoWhenConsumerWasCalled.get(0)).isEqualTo(mdcInfo); assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull(); assertThat(MDC.getCopyOfContextMap()).isNullOrEmpty(); }