/** * Equivalent to calling {@code new CallableWithTracing(origCallable)} - 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 CallableWithTracing(origCallable)}. * @see CallableWithTracing#CallableWithTracing(Callable) * @see CallableWithTracing */ public static <U> CallableWithTracing<U> withTracing(Callable<U> origCallable) { return new CallableWithTracing<>(origCallable); }
@Override public void call() throws Throwable { instance.call(); } });
@Override public void call() throws Throwable { withTracing(null); } })).isInstanceOf(IllegalArgumentException.class);
@DataProvider(value = { "true", "false" }) @Test public void pair_constructor_sets_fields_as_expected_when_pair_is_null(boolean useStaticFactory) { // when CallableWithTracing instance = (useStaticFactory) ? withTracing(callableMock, (Pair)null) : new CallableWithTracing(callableMock, (Pair)null); // then assertThat(instance.origCallable).isSameAs(callableMock); assertThat(instance.spanStackForExecution).isNull(); assertThat(instance.mdcContextMapForExecution).isNull(); }
@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 CallableWithTracing instance = (useStaticFactory) ? withTracing(callableMock, spanStackMock, mdcInfoMock) : new CallableWithTracing(callableMock, spanStackMock, mdcInfoMock); // then assertThat(instance.origCallable).isSameAs(callableMock); assertThat(instance.spanStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }
/** * Equivalent to calling {@code * new CallableWithTracing(origCallable, 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 CallableWithTracing(origCallable, spanStackForExecution, mdcContextMapForExecution)}. * @see CallableWithTracing#CallableWithTracing(Callable, Deque, Map) * @see CallableWithTracing */ public static <U> CallableWithTracing<U> withTracing(Callable<U> origCallable, Deque<Span> spanStackForExecution, Map<String, String> mdcContextMapForExecution) { return new CallableWithTracing<>(origCallable, spanStackForExecution, mdcContextMapForExecution); }
@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 CallableWithTracing instance = (useStaticFactory) ? withTracing(callableMock, Pair.of(spanStackMock, mdcInfoMock)) : new CallableWithTracing(callableMock, Pair.of(spanStackMock, mdcInfoMock) ); // then assertThat(instance.origCallable).isSameAs(callableMock); assertThat(instance.spanStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }
@Override public void call() throws Throwable { withTracing(null, spanStackMock, mdcInfoMock); } })).isInstanceOf(IllegalArgumentException.class);
/** * Equivalent to calling {@code new CallableWithTracing(origCallable, 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 CallableWithTracing(origCallable, originalThreadInfo)}. * @see CallableWithTracing#CallableWithTracing(Callable, Pair) * @see CallableWithTracing */ public static <U> CallableWithTracing<U> withTracing(Callable<U> origCallable, Pair<Deque<Span>, Map<String, String>> originalThreadInfo) { return new CallableWithTracing<>(origCallable, originalThreadInfo); }
@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 CallableWithTracing instance = (useStaticFactory) ? withTracing(callableMock) : new CallableWithTracing(callableMock); // then assertThat(instance.origCallable).isSameAs(callableMock); assertThat(instance.spanStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }
@Override public void call() throws Throwable { withTracing(null, Pair.of(spanStackMock, mdcInfoMock)); } })).isInstanceOf(IllegalArgumentException.class);
@Override public <T> Future<T> submit(Callable<T> task) { return delegate.submit(new CallableWithTracing<>(task)); }
/** * @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. * * @deprecated Please move to the Java 8 version of this class and method ({@code AsyncWingtipsHelper} or the static * {@code AsyncWingtipsHelperStatic}) whenever possible. */ @Deprecated public static <U> Callable<U> callableWithTracing(Callable<U> callable, Deque<Span> spanStackToLink, Map<String, String> mdcContextMapToLink) { return new CallableWithTracing<>(callable, spanStackToLink, 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. You can pass in a {@link TracingState} for clearer less verbose code since it extends * {@code Pair<Deque<Span>, Map<String, String>>}. * * @deprecated Please move to the Java 8 version of this class and method ({@code AsyncWingtipsHelper} or the static * {@code AsyncWingtipsHelperStatic}) whenever possible. */ @Deprecated public static <U> Callable<U> callableWithTracing(Callable<U> callable, Pair<Deque<Span>, Map<String, String>> threadInfoToLink) { return new CallableWithTracing<>(callable, threadInfoToLink); }
/** * @return A {@link Callable} 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()}. * * @deprecated Please move to the Java 8 version of this class and method ({@code AsyncWingtipsHelper} or the static * {@code AsyncWingtipsHelperStatic}) whenever possible. */ @Deprecated public static <U> Callable<U> callableWithTracing(Callable<U> callable) { return new CallableWithTracing<>(callable); }
protected <T> List<Callable<T>> convertToCallableWithTracingList(Collection<? extends Callable<T>> tasks) { if (tasks == null) { return null; } List<Callable<T>> tasksWithTracing = new ArrayList<>(tasks.size()); for (Callable<T> task : tasks) { Callable<T> taskWithTracing = (task == null) ? null : new CallableWithTracing<>(task); tasksWithTracing.add(taskWithTracing); } return tasksWithTracing; }
@Override public void call() throws Throwable { new CallableWithTracing(null); } })).isInstanceOf(IllegalArgumentException.class);
@Override public void call() throws Throwable { new CallableWithTracing(null, spanStackMock, mdcInfoMock); } })).isInstanceOf(IllegalArgumentException.class);
@Override public void call() throws Throwable { new CallableWithTracing(null, Pair.of(spanStackMock, mdcInfoMock)); } })).isInstanceOf(IllegalArgumentException.class);
Deque<Span> spanStack = Tracer.getInstance().getCurrentSpanStackCopy(); Map<String, String> mdcInfo = MDC.getCopyOfContextMap(); final CallableWithTracing instance = new CallableWithTracing( callableMock, spanStack, mdcInfo );