/** * Initializes the current thread's trace, erasing any previously accrued open spans. The new trace is {@link * Trace#isObservable observable} iff the given flag is true, or, iff {@code isObservable} is absent, if the {@link * #setSampler configured sampler} returns true. */ public static void initTrace(Optional<Boolean> isObservable, String traceId) { setTrace(createTrace(isObservable, traceId)); }
/** * Initializes the current thread's trace, erasing any previously accrued open spans. The new trace is {@link * Trace#isObservable observable} iff the given flag is true, or, iff {@code isObservable} is absent, if the {@link * #setSampler configured sampler} returns true. */ public static void initTrace(Optional<Boolean> isObservable, String traceId) { setTrace(createTrace(isObservable, traceId)); }
/** * Runs the given callable with the current trace at * the time of construction of this {@link DeferredTracer}. */ public <T, E extends Throwable> T withTrace(ThrowingCallable<T, E> inner) throws E { Trace originalTrace = Tracer.copyTrace(); Tracer.setTrace(trace); try { return inner.call(); } finally { Tracer.setTrace(originalTrace); } } }
/** * Runs the given callable with the current trace at * the time of construction of this {@link DeferredTracer}. */ public <T, E extends Throwable> T withTrace(ThrowingCallable<T, E> inner) throws E { Trace originalTrace = Tracer.copyTrace(); Tracer.setTrace(trace); try { return inner.call(); } finally { Tracer.setTrace(originalTrace); } } }
/** * Wraps the given {@link Runnable} such that it creates a fresh {@link Trace tracing state with the given traceId} * for its execution. That is, the trace during its {@link Runnable#run() execution} will use the traceId provided * instead of any trace already set on the thread used to execute the runnable. Each execution of the runnable * will use a new {@link Trace tracing state} with the same given traceId. */ public static Runnable wrapWithAlternateTraceId(String traceId, Runnable delegate) { return () -> { // clear the existing trace and keep it around for restoration when we're done Trace originalTrace = Tracer.getAndClearTrace(); try { Tracer.initTrace(Optional.empty(), traceId); delegate.run(); } finally { // restore the trace Tracer.setTrace(originalTrace); } }; }
/** * Wraps the given {@link Runnable} such that it creates a fresh {@link Trace tracing state with the given traceId} * for its execution. That is, the trace during its {@link Runnable#run() execution} will use the traceId provided * instead of any trace already set on the thread used to execute the runnable. Each execution of the runnable * will use a new {@link Trace tracing state} with the same given traceId. */ public static Runnable wrapWithAlternateTraceId(String traceId, Runnable delegate) { return () -> { // clear the existing trace and keep it around for restoration when we're done Trace originalTrace = Tracer.getAndClearTrace(); try { Tracer.initTrace(Optional.empty(), traceId); delegate.run(); } finally { // restore the trace Tracer.setTrace(originalTrace); } }; }
/** * Like {@link #wrapWithNewTrace(Callable)}, but for Runnables. */ public static Runnable wrapWithNewTrace(Runnable delegate) { return () -> { // clear the existing trace and keep it around for restoration when we're done Trace originalTrace = Tracer.getAndClearTrace(); try { Tracer.initTrace(Optional.empty(), Tracers.randomId()); delegate.run(); } finally { // restore the trace Tracer.setTrace(originalTrace); } }; }
/** * Like {@link #wrapWithNewTrace(Callable)}, but for Runnables. */ public static Runnable wrapWithNewTrace(Runnable delegate) { return () -> { // clear the existing trace and keep it around for restoration when we're done Trace originalTrace = Tracer.getAndClearTrace(); try { Tracer.initTrace(Optional.empty(), Tracers.randomId()); delegate.run(); } finally { // restore the trace Tracer.setTrace(originalTrace); } }; }
/** * Wraps the given {@link Callable} such that it creates a fresh {@link Trace tracing state} for its execution. * That is, the trace during its {@link Callable#call() execution} is entirely separate from the trace at * construction or any trace already set on the thread used to execute the callable. Each execution of the callable * will have a fresh trace. */ public static <V> Callable<V> wrapWithNewTrace(Callable<V> delegate) { return () -> { // clear the existing trace and keep it around for restoration when we're done Trace originalTrace = Tracer.getAndClearTrace(); try { Tracer.initTrace(Optional.empty(), Tracers.randomId()); return delegate.call(); } finally { // restore the trace Tracer.setTrace(originalTrace); } }; }
/** * Wraps the given {@link Callable} such that it creates a fresh {@link Trace tracing state} for its execution. * That is, the trace during its {@link Callable#call() execution} is entirely separate from the trace at * construction or any trace already set on the thread used to execute the callable. Each execution of the callable * will have a fresh trace. */ public static <V> Callable<V> wrapWithNewTrace(Callable<V> delegate) { return () -> { // clear the existing trace and keep it around for restoration when we're done Trace originalTrace = Tracer.getAndClearTrace(); try { Tracer.initTrace(Optional.empty(), Tracers.randomId()); return delegate.call(); } finally { // restore the trace Tracer.setTrace(originalTrace); } }; }