/** * @return A {@link BiConsumer} 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. You can pass in a {@link TracingState} for clearer less verbose code since it extends * {@code Pair<Deque<Span>, Map<String, String>>}. */ default <T, U> BiConsumer<T, U> biConsumerWithTracing( BiConsumer<T, U> biConsumer, Pair<Deque<Span>, Map<String, String>> threadInfoToLink ) { return new BiConsumerWithTracing<>(biConsumer, threadInfoToLink); }
@Test public void constructors_throw_exception_if_passed_null_operator() { // given final Deque<Span> spanStackMock = mock(Deque.class); final Map<String, String> mdcInfoMock = mock(Map.class); // expect assertThat(catchThrowable(() -> new BiConsumerWithTracing(null))) .isInstanceOf(IllegalArgumentException.class); assertThat(catchThrowable(() -> withTracing(null))) .isInstanceOf(IllegalArgumentException.class); // and expect assertThat(catchThrowable(() -> new BiConsumerWithTracing(null, Pair.of(spanStackMock, mdcInfoMock)))) .isInstanceOf(IllegalArgumentException.class); assertThat(catchThrowable(() -> withTracing(null, Pair.of(spanStackMock, mdcInfoMock)))) .isInstanceOf(IllegalArgumentException.class); // and expect assertThat(catchThrowable(() -> new BiConsumerWithTracing(null, spanStackMock, mdcInfoMock))) .isInstanceOf(IllegalArgumentException.class); assertThat(catchThrowable(() -> withTracing(null, spanStackMock, mdcInfoMock))) .isInstanceOf(IllegalArgumentException.class); }
Deque<Span> spanStack = Tracer.getInstance().getCurrentSpanStackCopy(); Map<String, String> mdcInfo = MDC.getCopyOfContextMap(); BiConsumerWithTracing instance = new BiConsumerWithTracing( biConsumerMock, spanStack, mdcInfo ); Throwable ex = catchThrowable(() -> instance.accept(inObj1, inObj2));
@DataProvider(value = { "true", "false" }) @Test public void kitchen_sink_constructor_sets_fields_as_expected(boolean useStaticFactory) { // given Deque<Span> spanStackMock = mock(Deque.class); Map<String, String> mdcInfoMock = mock(Map.class); // when BiConsumerWithTracing instance = (useStaticFactory) ? withTracing(biConsumerMock, spanStackMock, mdcInfoMock) : new BiConsumerWithTracing(biConsumerMock, spanStackMock, mdcInfoMock); // then assertThat(instance.origBiConsumer).isSameAs(biConsumerMock); assertThat(instance.spanStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }
@DataProvider(value = { "true", "false" }) @Test public void pair_constructor_sets_fields_as_expected_when_pair_is_null(boolean useStaticFactory) { // when BiConsumerWithTracing instance = (useStaticFactory) ? withTracing(biConsumerMock, (Pair)null) : new BiConsumerWithTracing(biConsumerMock, (Pair)null); // then assertThat(instance.origBiConsumer).isSameAs(biConsumerMock); assertThat(instance.spanStackForExecution).isNull(); assertThat(instance.mdcContextMapForExecution).isNull(); }
/** * @return A {@link BiConsumer} 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. */ default <T, U> BiConsumer<T, U> biConsumerWithTracing(BiConsumer<T, U> biConsumer, Deque<Span> spanStackToLink, Map<String, String> mdcContextMapToLink) { return new BiConsumerWithTracing<>(biConsumer, spanStackToLink, mdcContextMapToLink); }
@DataProvider(value = { "true | true | true", "true | false | true", "false | true | true", "false | false | true", "true | true | false", "true | false | false", "false | true | false", "false | false | false", }, splitBy = "\\|") @Test public void pair_constructor_sets_fields_as_expected( boolean nullSpanStack, boolean nullMdcInfo, boolean useStaticFactory ) { // given Deque<Span> spanStackMock = (nullSpanStack) ? null : mock(Deque.class); Map<String, String> mdcInfoMock = (nullMdcInfo) ? null : mock(Map.class); // when BiConsumerWithTracing instance = (useStaticFactory) ? withTracing(biConsumerMock, Pair.of(spanStackMock, mdcInfoMock)) : new BiConsumerWithTracing(biConsumerMock, Pair.of(spanStackMock, mdcInfoMock) ); // then assertThat(instance.origBiConsumer).isSameAs(biConsumerMock); assertThat(instance.spanStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }
/** * @return A {@link BiConsumer} that wraps the given original so that the <b>current thread's</b> tracing and MDC * information is registered with the thread and therefore available during execution and unregistered after * execution. * * <p>NOTE: The current thread's tracing and MDC info will be extracted using {@link * Tracer#getCurrentSpanStackCopy()} and {@link MDC#getCopyOfContextMap()}. */ default <T, U> BiConsumer<T, U> biConsumerWithTracing(BiConsumer<T, U> biConsumer) { return new BiConsumerWithTracing<>(biConsumer); }
@DataProvider(value = { "true", "false" }) @Test public void current_thread_info_constructor_sets_fields_as_expected(boolean useStaticFactory) { // given Tracer.getInstance().startRequestWithRootSpan("request-" + UUID.randomUUID().toString()); Deque<Span> spanStackMock = Tracer.getInstance().getCurrentSpanStackCopy(); Map<String, String> mdcInfoMock = MDC.getCopyOfContextMap(); // when BiConsumerWithTracing instance = (useStaticFactory) ? withTracing(biConsumerMock) : new BiConsumerWithTracing(biConsumerMock); // then assertThat(instance.origBiConsumer).isSameAs(biConsumerMock); assertThat(instance.spanStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }
/** * Equivalent to calling {@code new BiConsumerWithTracing(origBiConsumer)} - this allows you to do a static method * import for cleaner looking code in some cases. This method ultimately extracts the current tracing and MDC * information from the current thread using {@link Tracer#getCurrentSpanStackCopy()} and {@link * MDC#getCopyOfContextMap()}. That tracing and MDC information will be associated with the thread when the given * operation is executed. * * <p>The operation you pass in cannot be null (an {@link IllegalArgumentException} will be thrown if you pass in * null for the operation). * * @return {@code new BiConsumerWithTracing(origBiConsumer)}. * @see BiConsumerWithTracing#BiConsumerWithTracing(BiConsumer) * @see BiConsumerWithTracing */ public static <T, U> BiConsumerWithTracing<T, U> withTracing(BiConsumer<T, U> origBiConsumer) { return new BiConsumerWithTracing<>(origBiConsumer); }
/** * Equivalent to calling {@code * new BiConsumerWithTracing(origBiConsumer, spanStackForExecution, mdcContextMapForExecution)} - * this allows you to do a static method import for cleaner looking code in some cases. This method uses the given * trace and MDC information, which will be associated with the thread when the given operation is executed. * * <p>The operation you pass in cannot be null (an {@link IllegalArgumentException} will be thrown if you pass in * null for the operation). * * <p>The trace and/or MDC info can be null and no error will be thrown, however any trace or MDC info that is null * means the corresponding info will not be available to the thread when the operation is executed. * * @return {@code new BiConsumerWithTracing(origBiConsumer, spanStackForExecution, mdcContextMapForExecution)}. * @see BiConsumerWithTracing#BiConsumerWithTracing(BiConsumer, Deque, Map) * @see BiConsumerWithTracing */ public static <T, U> BiConsumerWithTracing<T, U> withTracing(BiConsumer<T, U> origBiConsumer, Deque<Span> spanStackForExecution, Map<String, String> mdcContextMapForExecution) { return new BiConsumerWithTracing<>(origBiConsumer, spanStackForExecution, mdcContextMapForExecution); }
/** * Equivalent to calling {@code new BiConsumerWithTracing(origBiConsumer, originalThreadInfo)} - this allows you * to do a static method import for cleaner looking code in some cases. This method uses the given trace and MDC * information, which will be associated with the thread when the given operation is executed. * * <p>The operation you pass in cannot be null (an {@link IllegalArgumentException} will be thrown if you pass in * null for the operation). * * <p>The {@link Pair} can be null, or you can pass null for the left and/or right side of the pair, and no error * will be thrown. Any trace or MDC info that is null means the corresponding info will not be available to the * thread when the operation is executed however. * * <p>You can pass in a {@link TracingState} for clearer less verbose code since it extends * {@code Pair<Deque<Span>, Map<String, String>>}. * * @return {@code new BiConsumerWithTracing(origBiConsumer, originalThreadInfo)}. * @see BiConsumerWithTracing#BiConsumerWithTracing(BiConsumer, Pair) * @see BiConsumerWithTracing */ public static <T, U> BiConsumerWithTracing<T, U> withTracing( BiConsumer<T, U> origBiConsumer, Pair<Deque<Span>, Map<String, String>> originalThreadInfo ) { return new BiConsumerWithTracing<>(origBiConsumer, originalThreadInfo); }