/** * @return A {@link Runnable} that wraps the given original so that the given distributed tracing and MDC * information is registered with the thread and therefore available during execution and unregistered after * execution. */ public static Runnable runnableWithTracingAndMdc(Runnable runnable, Deque<Span> distributedTraceStackToLink, Map<String, String> mdcContextMapToLink) { return new RunnableWithTracingAndMdcSupport(runnable, distributedTraceStackToLink, mdcContextMapToLink); }
@DataProvider(value = { "true", "false" }) @Test public void run_handles_tracing_and_mdc_info_as_expected(boolean throwException) { // given throwExceptionDuringCall = throwException; Tracer.getInstance().startRequestWithRootSpan("foo"); Deque<Span> spanStack = Tracer.getInstance().getCurrentSpanStackCopy(); Map<String, String> mdcInfo = MDC.getCopyOfContextMap(); RunnableWithTracingAndMdcSupport instance = new RunnableWithTracingAndMdcSupport( runnableMock, spanStack, mdcInfo ); resetTracingAndMdc(); assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull(); assertThat(MDC.getCopyOfContextMap()).isEmpty(); // when Throwable ex = catchThrowable(() -> instance.run()); // then verify(runnableMock).run(); if (throwException) assertThat(ex).isNotNull(); else assertThat(ex).isNull(); assertThat(currentSpanStackWhenRunnableWasCalled.get(0)).isEqualTo(spanStack); assertThat(currentMdcInfoWhenRunnableWasCalled.get(0)).isEqualTo(mdcInfo); assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull(); assertThat(MDC.getCopyOfContextMap()).isEmpty(); }
/** * @return A {@link Runnable} that wraps the given original so that the given {@link ChannelHandlerContext}'s * distributed tracing and MDC information is registered with the thread and therefore available during execution * and unregistered after execution. */ public static Runnable runnableWithTracingAndMdc(Runnable runnable, ChannelHandlerContext ctx) { return new RunnableWithTracingAndMdcSupport(runnable, ctx); }
/** * @return A {@link Runnable} that wraps the given original so that the given {@link ChannelHandlerContext}'s * distributed tracing and MDC information is registered with the thread and therefore available during execution * and unregistered after execution. */ public static Runnable runnableWithTracingAndMdc(Runnable runnable, ChannelHandlerContext ctx) { return new RunnableWithTracingAndMdcSupport(runnable, ctx); }
/** * @return A {@link Runnable} that wraps the given original so that the given distributed tracing and MDC * information is registered with the thread and therefore available during execution and unregistered after * execution. */ public static Runnable runnableWithTracingAndMdc(Runnable runnable, Pair<Deque<Span>, Map<String, String>> threadInfoToLink) { return new RunnableWithTracingAndMdcSupport(runnable, threadInfoToLink); }
/** * @return A {@link Runnable} that wraps the given original so that the given distributed tracing and MDC * information is registered with the thread and therefore available during execution and unregistered after * execution. */ public static Runnable runnableWithTracingAndMdc(Runnable runnable, Deque<Span> distributedTraceStackToLink, Map<String, String> mdcContextMapToLink) { return new RunnableWithTracingAndMdcSupport(runnable, distributedTraceStackToLink, mdcContextMapToLink); }
/** * @return A {@link Runnable} that wraps the given original so that the given distributed tracing and MDC * information is registered with the thread and therefore available during execution and unregistered after * execution. */ public static Runnable runnableWithTracingAndMdc(Runnable runnable, Pair<Deque<Span>, Map<String, String>> threadInfoToLink) { return new RunnableWithTracingAndMdcSupport(runnable, threadInfoToLink); }
@Test public void constructors_throw_exception_if_passed_null_runnable() { // given Deque<Span> spanStackMock = mock(Deque.class); Map<String, String> mdcInfoMock = mock(Map.class); // expect assertThat(catchThrowable(() -> new RunnableWithTracingAndMdcSupport(null, ctxMock))) .isInstanceOf(IllegalArgumentException.class); // and expect assertThat(catchThrowable(() -> new RunnableWithTracingAndMdcSupport(null, Pair.of(spanStackMock, mdcInfoMock)))) .isInstanceOf(IllegalArgumentException.class); // and expect assertThat(catchThrowable(() -> new RunnableWithTracingAndMdcSupport(null, spanStackMock, mdcInfoMock))) .isInstanceOf(IllegalArgumentException.class); }
@Test public void kitchen_sink_constructor_sets_fields_as_expected() { // given Deque<Span> spanStackMock = mock(Deque.class); Map<String, String> mdcInfoMock = mock(Map.class); // when RunnableWithTracingAndMdcSupport instance = new RunnableWithTracingAndMdcSupport( runnableMock, spanStackMock, mdcInfoMock ); // then assertThat(instance.origRunnable).isSameAs(runnableMock); assertThat(instance.distributedTraceStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }
@Test public void pair_constructor_sets_fields_as_expected_when_pair_is_null() { // when RunnableWithTracingAndMdcSupport instance = new RunnableWithTracingAndMdcSupport(runnableMock, (Pair)null); // then assertThat(instance.origRunnable).isSameAs(runnableMock); assertThat(instance.distributedTraceStackForExecution).isNull(); assertThat(instance.mdcContextMapForExecution).isNull(); }
@Test public void ctx_constructor_sets_fields_as_expected() { // given Deque<Span> spanStackMock = mock(Deque.class); Map<String, String> mdcInfoMock = mock(Map.class); state.setDistributedTraceStack(spanStackMock); state.setLoggerMdcContextMap(mdcInfoMock); // when RunnableWithTracingAndMdcSupport instance = new RunnableWithTracingAndMdcSupport(runnableMock, ctxMock); // then assertThat(instance.origRunnable).isSameAs(runnableMock); assertThat(instance.distributedTraceStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }
@DataProvider(value = { "true | true", "true | false", "false | true", "false | false", }, splitBy = "\\|") @Test public void pair_constructor_sets_fields_as_expected(boolean nullSpanStack, boolean nullMdcInfo) { // given Deque<Span> spanStackMock = (nullSpanStack) ? null : mock(Deque.class); Map<String, String> mdcInfoMock = (nullMdcInfo) ? null : mock(Map.class); // when RunnableWithTracingAndMdcSupport instance = new RunnableWithTracingAndMdcSupport( runnableMock, Pair.of(spanStackMock, mdcInfoMock) ); // then assertThat(instance.origRunnable).isSameAs(runnableMock); assertThat(instance.distributedTraceStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }