/** * @return A {@link Consumer} 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 <T> Consumer<T> consumerWithTracingAndMdc(Consumer<T> consumer, ChannelHandlerContext ctx) { return new ConsumerWithTracingAndMdcSupport<>(consumer, ctx); }
Deque<Span> spanStack = Tracer.getInstance().getCurrentSpanStackCopy(); Map<String, String> mdcInfo = MDC.getCopyOfContextMap(); ConsumerWithTracingAndMdcSupport instance = new ConsumerWithTracingAndMdcSupport( consumerMock, spanStack, mdcInfo ); Throwable ex = catchThrowable(() -> instance.accept(inObj));
/** * @return A {@link Consumer} 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 <T> Consumer<T> consumerWithTracingAndMdc(Consumer<T> consumer, ChannelHandlerContext ctx) { return new ConsumerWithTracingAndMdcSupport<>(consumer, ctx); }
/** * @return A {@link Consumer} 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 <T> Consumer<T> consumerWithTracingAndMdc(Consumer<T> consumer, Pair<Deque<Span>, Map<String, String>> threadInfoToLink) { return new ConsumerWithTracingAndMdcSupport<>(consumer, threadInfoToLink); }
/** * @return A {@link Consumer} 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 <T> Consumer<T> consumerWithTracingAndMdc(Consumer<T> consumer, Deque<Span> distributedTraceStackToLink, Map<String, String> mdcContextMapToLink) { return new ConsumerWithTracingAndMdcSupport<>(consumer, distributedTraceStackToLink, mdcContextMapToLink); }
/** * @return A {@link Consumer} 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 <T> Consumer<T> consumerWithTracingAndMdc(Consumer<T> consumer, Deque<Span> distributedTraceStackToLink, Map<String, String> mdcContextMapToLink) { return new ConsumerWithTracingAndMdcSupport<>(consumer, distributedTraceStackToLink, mdcContextMapToLink); }
/** * @return A {@link Consumer} 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 <T> Consumer<T> consumerWithTracingAndMdc(Consumer<T> consumer, Pair<Deque<Span>, Map<String, String>> threadInfoToLink) { return new ConsumerWithTracingAndMdcSupport<>(consumer, threadInfoToLink); }
@Test public void constructors_throw_exception_if_passed_null_consumer() { // given Deque<Span> spanStackMock = mock(Deque.class); Map<String, String> mdcInfoMock = mock(Map.class); // expect assertThat(catchThrowable(() -> new ConsumerWithTracingAndMdcSupport(null, ctxMock))) .isInstanceOf(IllegalArgumentException.class); // and expect assertThat(catchThrowable(() -> new ConsumerWithTracingAndMdcSupport(null, Pair.of(spanStackMock, mdcInfoMock)))) .isInstanceOf(IllegalArgumentException.class); // and expect assertThat(catchThrowable(() -> new ConsumerWithTracingAndMdcSupport(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 ConsumerWithTracingAndMdcSupport instance = new ConsumerWithTracingAndMdcSupport( consumerMock, spanStackMock, mdcInfoMock ); // then assertThat(instance.origConsumer).isSameAs(consumerMock); assertThat(instance.distributedTraceStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }
@Test public void pair_constructor_sets_fields_as_expected_when_pair_is_null() { // when ConsumerWithTracingAndMdcSupport instance = new ConsumerWithTracingAndMdcSupport(consumerMock, (Pair)null); // then assertThat(instance.origConsumer).isSameAs(consumerMock); 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 ConsumerWithTracingAndMdcSupport instance = new ConsumerWithTracingAndMdcSupport(consumerMock, ctxMock); // then assertThat(instance.origConsumer).isSameAs(consumerMock); 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 ConsumerWithTracingAndMdcSupport instance = new ConsumerWithTracingAndMdcSupport( consumerMock, Pair.of(spanStackMock, mdcInfoMock) ); // then assertThat(instance.origConsumer).isSameAs(consumerMock); assertThat(instance.distributedTraceStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }