/** * @return A {@link Callable} 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 <U> Callable<U> callableWithTracingAndMdc(Callable<U> callable, ChannelHandlerContext ctx) { return new CallableWithTracingAndMdcSupport<>(callable, ctx); }
@DataProvider(value = { "true", "false" }) @Test public void call_handles_tracing_and_mdc_info_as_expected(boolean throwException) throws Exception { // given throwExceptionDuringCall = throwException; Tracer.getInstance().startRequestWithRootSpan("foo"); Deque<Span> spanStack = Tracer.getInstance().getCurrentSpanStackCopy(); Map<String, String> mdcInfo = MDC.getCopyOfContextMap(); CallableWithTracingAndMdcSupport instance = new CallableWithTracingAndMdcSupport( callableMock, spanStack, mdcInfo ); resetTracingAndMdc(); assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull(); assertThat(MDC.getCopyOfContextMap()).isEmpty(); // when Throwable ex = catchThrowable(() -> instance.call()); // then verify(callableMock).call(); if (throwException) assertThat(ex).isNotNull(); else assertThat(ex).isNull(); assertThat(currentSpanStackWhenCallableWasCalled.get(0)).isEqualTo(spanStack); assertThat(currentMdcInfoWhenCallableWasCalled.get(0)).isEqualTo(mdcInfo); assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull(); assertThat(MDC.getCopyOfContextMap()).isEmpty(); }
/** * @return A {@link Callable} 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 <U> Callable<U> callableWithTracingAndMdc(Callable<U> callable, ChannelHandlerContext ctx) { return new CallableWithTracingAndMdcSupport<>(callable, ctx); }
/** * @return A {@link Callable} 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 <U> Callable<U> callableWithTracingAndMdc(Callable<U> callable, Pair<Deque<Span>, Map<String, String>> threadInfoToLink) { return new CallableWithTracingAndMdcSupport<>(callable, threadInfoToLink); }
/** * @return A {@link Callable} 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 <U> Callable<U> callableWithTracingAndMdc(Callable<U> callable, Deque<Span> distributedTraceStackToLink, Map<String, String> mdcContextMapToLink) { return new CallableWithTracingAndMdcSupport<>(callable, distributedTraceStackToLink, mdcContextMapToLink); }
/** * @return A {@link Callable} 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 <U> Callable<U> callableWithTracingAndMdc(Callable<U> callable, Pair<Deque<Span>, Map<String, String>> threadInfoToLink) { return new CallableWithTracingAndMdcSupport<>(callable, threadInfoToLink); }
/** * @return A {@link Callable} 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 <U> Callable<U> callableWithTracingAndMdc(Callable<U> callable, Deque<Span> distributedTraceStackToLink, Map<String, String> mdcContextMapToLink) { return new CallableWithTracingAndMdcSupport<>(callable, distributedTraceStackToLink, mdcContextMapToLink); }
@Test public void constructors_throw_exception_if_passed_null_callable() { // given Deque<Span> spanStackMock = mock(Deque.class); Map<String, String> mdcInfoMock = mock(Map.class); // expect assertThat(catchThrowable(() -> new CallableWithTracingAndMdcSupport(null, ctxMock))) .isInstanceOf(IllegalArgumentException.class); // and expect assertThat(catchThrowable(() -> new CallableWithTracingAndMdcSupport(null, Pair.of(spanStackMock, mdcInfoMock)))) .isInstanceOf(IllegalArgumentException.class); // and expect assertThat(catchThrowable(() -> new CallableWithTracingAndMdcSupport(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 CallableWithTracingAndMdcSupport instance = new CallableWithTracingAndMdcSupport( callableMock, spanStackMock, mdcInfoMock ); // then assertThat(instance.origCallable).isSameAs(callableMock); assertThat(instance.distributedTraceStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }
@Test public void pair_constructor_sets_fields_as_expected_when_pair_is_null() { // when CallableWithTracingAndMdcSupport instance = new CallableWithTracingAndMdcSupport(callableMock, (Pair)null); // then assertThat(instance.origCallable).isSameAs(callableMock); 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 CallableWithTracingAndMdcSupport instance = new CallableWithTracingAndMdcSupport(callableMock, ctxMock); // then assertThat(instance.origCallable).isSameAs(callableMock); 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 CallableWithTracingAndMdcSupport instance = new CallableWithTracingAndMdcSupport( callableMock, Pair.of(spanStackMock, mdcInfoMock) ); // then assertThat(instance.origCallable).isSameAs(callableMock); assertThat(instance.distributedTraceStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }