/** * @return A *copy* of the current thread's tracing information. Since this creates copies of the span stack and MDC * info it can have a noticeable performance impact if used too many times (i.e. tens or hundreds of times per * request for high throughput services). NOTE: This is usually not needed unless you're doing asynchronous * processing and need to pass tracing state across thread boundaries. */ public TracingState getCurrentTracingStateCopy() { return new TracingState(getCurrentSpanStackCopy(), MDC.getCopyOfContextMap()); }
/** * Constructor that extracts the current tracing and MDC information from the current thread using {@link * Tracer#getCurrentSpanStackCopy()} and {@link MDC#getCopyOfContextMap()}, and forwards the information to * the {@link BiFunctionWithTracing#BiFunctionWithTracing(BiFunction, Deque, Map)} * constructor. 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). */ public BiFunctionWithTracing(BiFunction<T, U, R> origBiFunction) { this(origBiFunction, Tracer.getInstance().getCurrentSpanStackCopy(), MDC.getCopyOfContextMap()); }
/** * Constructor that extracts the current tracing and MDC information from the current thread using {@link * Tracer#getCurrentSpanStackCopy()} and {@link MDC#getCopyOfContextMap()}, and forwards the information to * the {@link SupplierWithTracing#SupplierWithTracing(Supplier, Deque, Map)} * constructor. 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). */ public SupplierWithTracing(Supplier<U> origSupplier) { this(origSupplier, Tracer.getInstance().getCurrentSpanStackCopy(), MDC.getCopyOfContextMap()); }
/** * Constructor that extracts the current tracing and MDC information from the current thread using {@link * Tracer#getCurrentSpanStackCopy()} and {@link MDC#getCopyOfContextMap()}, and forwards the information to * the {@link SuccessCallbackWithTracing#SuccessCallbackWithTracing(SuccessCallback, Deque, Map)} * constructor. 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). */ public SuccessCallbackWithTracing(SuccessCallback<T> origSuccessCallback) { this(origSuccessCallback, Tracer.getInstance().getCurrentSpanStackCopy(), MDC.getCopyOfContextMap()); }
/** * Constructor that extracts the current tracing and MDC information from the current thread using {@link * Tracer#getCurrentSpanStackCopy()} and {@link MDC#getCopyOfContextMap()}, and forwards the information to * the {@link FailureCallbackWithTracing#FailureCallbackWithTracing(FailureCallback, Deque, Map)} * constructor. 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). */ public FailureCallbackWithTracing(FailureCallback origFailureCallback) { this(origFailureCallback, Tracer.getInstance().getCurrentSpanStackCopy(), MDC.getCopyOfContextMap()); }
/** * Constructor that extracts the current tracing and MDC information from the current thread using {@link * Tracer#getCurrentSpanStackCopy()} and {@link MDC#getCopyOfContextMap()}, and forwards the information to * the {@link ConsumerWithTracing#ConsumerWithTracing(Consumer, Deque, Map)} * constructor. 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). */ public ConsumerWithTracing(Consumer<T> origConsumer) { this(origConsumer, Tracer.getInstance().getCurrentSpanStackCopy(), MDC.getCopyOfContextMap()); }
/** * Constructor that extracts the current tracing and MDC information from the current thread using {@link * Tracer#getCurrentSpanStackCopy()} and {@link MDC#getCopyOfContextMap()}, and forwards the information to * the {@link BiPredicateWithTracing#BiPredicateWithTracing(BiPredicate, Deque, Map)} * constructor. 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). */ public BiPredicateWithTracing(BiPredicate<T, U> origBiPredicate) { this(origBiPredicate, Tracer.getInstance().getCurrentSpanStackCopy(), MDC.getCopyOfContextMap()); }
/** * Constructor that extracts the current tracing and MDC information from the current thread using {@link * Tracer#getCurrentSpanStackCopy()} and {@link MDC#getCopyOfContextMap()}, and forwards the information to * the {@link RunnableWithTracing#RunnableWithTracing(Runnable, Deque, Map)} * constructor. 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). */ public RunnableWithTracing(Runnable origRunnable) { this(origRunnable, Tracer.getInstance().getCurrentSpanStackCopy(), MDC.getCopyOfContextMap()); }
@Override public Object answer(InvocationOnMock invocation) throws Throwable { currentSpanStackWhenCallableWasCalled.add(Tracer.getInstance().getCurrentSpanStackCopy()); currentMdcInfoWhenCallableWasCalled.add(MDC.getCopyOfContextMap()); if (throwExceptionDuringCall) throw new RuntimeException("kaboom"); return null; } }).when(callableMock).call();
@Override public Object answer(InvocationOnMock invocation) throws Throwable { currentSpanStackWhenRunnableWasCalled.add(Tracer.getInstance().getCurrentSpanStackCopy()); currentMdcInfoWhenRunnableWasCalled.add(MDC.getCopyOfContextMap()); if (throwExceptionDuringCall) throw new RuntimeException("kaboom"); return null; } }).when(runnableMock).run();
@Test public void getCurrentSpanStackCopy_returns_null_if_original_is_null() { // given Tracer tracer = Tracer.getInstance(); assertThat(getSpanStackSize()).isEqualTo(0); // expect assertThat(tracer.getCurrentSpanStackCopy()).isNull(); }
@Test public void linkTracingAndMdcToCurrentThread_should_clear_mdc_if_state_is_available_but_state_mdc_info_is_null() { // given MDC.put("foo", "bar"); Tracer.getInstance().startRequestWithRootSpan("blahtrace"); assertThat(MDC.getCopyOfContextMap().isEmpty(), is(false)); assertThat(Tracer.getInstance().getCurrentSpan(), notNullValue()); // when handler.linkTracingAndMdcToCurrentThread(ctxMock); // then assertThat(MDC.getCopyOfContextMap().isEmpty(), is(true)); assertThat(Tracer.getInstance().getCurrentSpanStackCopy(), nullValue()); }
private Pair<Deque<Span>, Map<String, String>> setupCurrentThreadWithTracingInfo() { resetTracing(); Tracer.getInstance().startRequestWithRootSpan("request-" + UUID.randomUUID().toString()); return Pair.of(Tracer.getInstance().getCurrentSpanStackCopy(), MDC.getCopyOfContextMap()); }
private Pair<Deque<Span>, Map<String, String>> setupCurrentThreadWithTracingInfo() { resetTracing(); Tracer.getInstance().startRequestWithRootSpan("request-" + UUID.randomUUID().toString()); return Pair.of(Tracer.getInstance().getCurrentSpanStackCopy(), MDC.getCopyOfContextMap()); }
private Pair<Deque<Span>, Map<String, String>> setupCurrentThreadWithTracingInfo() { resetTracing(); Tracer.getInstance().startRequestWithRootSpan("request-" + UUID.randomUUID().toString()); return Pair.of(Tracer.getInstance().getCurrentSpanStackCopy(), MDC.getCopyOfContextMap()); }
public static ChannelHandlerContextMocks mockChannelHandlerContextWithTraceInfo(String userId) { if (Tracer.getInstance().getCurrentSpan() == null) { Tracer.getInstance().startRequestWithRootSpan("mockChannelHandlerContext", userId); } ChannelHandlerContextMocks channelHandlerMocks = mockChannelHandlerContext(); when(channelHandlerMocks.mockHttpProcessingState.getLoggerMdcContextMap()).thenReturn(MDC.getCopyOfContextMap()); when(channelHandlerMocks.mockHttpProcessingState.getDistributedTraceStack()).thenReturn(Tracer.getInstance().getCurrentSpanStackCopy()); return channelHandlerMocks; }
private Pair<Deque<Span>, Map<String, String>> generateTracingInfo() { resetTracing(); Tracer.getInstance().startRequestWithRootSpan("someSpan"); Pair<Deque<Span>, Map<String, String>> result = Pair.of( Tracer.getInstance().getCurrentSpanStackCopy(), new HashMap<>(MDC.getCopyOfContextMap()) ); resetTracing(); return result; }
private Pair<Deque<Span>, Map<String, String>> generateTracingInfo() { resetTracing(); Tracer.getInstance().startRequestWithRootSpan("someSpan"); Pair<Deque<Span>, Map<String, String>> result = Pair.of( Tracer.getInstance().getCurrentSpanStackCopy(), new HashMap<>(MDC.getCopyOfContextMap()) ); resetTracing(); return result; }
private Pair<Deque<Span>, Map<String, String>> generateTracingAndMdcInfo() { resetTracingAndMdc(); Tracer.getInstance().startRequestWithRootSpan("someSpan"); Pair<Deque<Span>, Map<String, String>> result = Pair.of( Tracer.getInstance().getCurrentSpanStackCopy(), new HashMap<>(MDC.getCopyOfContextMap()) ); resetTracingAndMdc(); return result; }
private Pair<Deque<Span>, Map<String, String>> generateTracingInfo() { resetTracing(); Tracer.getInstance().startRequestWithRootSpan("someSpan"); Pair<Deque<Span>, Map<String, String>> result = Pair.of( Tracer.getInstance().getCurrentSpanStackCopy(), (Map<String, String>)new HashMap<>(MDC.getCopyOfContextMap()) ); resetTracing(); return result; }