/** * 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); } }; }