Class CurrentTraceContext
- Direct Known Subclasses:
StrictCurrentTraceContext,ThreadLocalCurrentTraceContext
public abstract class CurrentTraceContext extends Object
This type is an SPI, and intended to be used by implementors looking to change thread-local storage, or integrate with other contexts such as logging (MDC).
Design
This design was inspired by com.google.instrumentation.trace.ContextUtils, com.google.inject.servlet.RequestScoper and com.github.kristofa.brave.CurrentSpan-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCurrentTraceContext.BuilderImplementations of this allow standardized configuration, for example scope decoration.static classCurrentTraceContext.DefaultDefault implementation which is backed by a static thread local.static interfaceCurrentTraceContext.ScopeA span remains in the scope it was bound to until close is called.static interfaceCurrentTraceContext.ScopeDecoratorUse this to add features such as thread checks or log correlation when a scope is created or closed. -
Constructor Summary
Constructors Modifier Constructor Description protectedCurrentTraceContext()protectedCurrentTraceContext(CurrentTraceContext.Builder builder) -
Method Summary
Modifier and Type Method Description protected CurrentTraceContext.ScopedecorateScope(TraceContext context, CurrentTraceContext.Scope scope)When implementing newScope(TraceContext), decorate the result before returning it.Executorexecutor(Executor delegate)Decorates the input such that thecurrent trace contextat the time a task is scheduled is made current when the task is executed.ExecutorServiceexecutorService(ExecutorService delegate)Decorates the input such that thecurrent trace contextat the time a task is scheduled is made current when the task is executed.abstract TraceContextget()Returns the current span in scope or null if there isn't one.CurrentTraceContext.ScopemaybeScope(TraceContext context)LikenewScope(TraceContext), except returnsCurrentTraceContext.Scope.NOOPif the given context is already in scope.abstract CurrentTraceContext.ScopenewScope(TraceContext context)Sets the current span in scope until the returned object is closed.Runnablewrap(Runnable task)Wraps the input so that it executes with the same context as now.<C> Callable<C>wrap(Callable<C> task)Wraps the input so that it executes with the same context as now.
-
Constructor Details
-
CurrentTraceContext
protected CurrentTraceContext() -
CurrentTraceContext
-
-
Method Details
-
get
Returns the current span in scope or null if there isn't one. -
newScope
Sets the current span in scope until the returned object is closed. It is a programming error to drop or never close the result. Using try-with-resources is preferred for this reason.- Parameters:
context- span to place into scope or null to clear the scope
-
decorateScope
protected CurrentTraceContext.Scope decorateScope(@Nullable TraceContext context, CurrentTraceContext.Scope scope)- Parameters:
scope-CurrentTraceContext.Scope.NOOPif the prior context was equal to thecontextparameter.
-
maybeScope
LikenewScope(TraceContext), except returnsCurrentTraceContext.Scope.NOOPif the given context is already in scope. This can reduce overhead when scoping callbacks. However, this will not apply any changes, notably inTraceContext.extra(). As such, it should be used carefully and only in conditions where redundancy is possible and the intent is primarily to facilitateTracer.currentSpan(). Most often, this is used to eliminate redundant scopes by wrappers.For example, RxJava includes hooks to wrap types that represent an asynchronous functional composition. For example,
flowable.parallel().flatMap(Y).sequential()Assembly hooks can ensure each stage of this operation can see the initial trace context. However, other tools can also instrument the stages, including vert.x or even agent instrumentation. When wrapping callbacks, it can reduce overhead to usemaybeScopeas opposed tonewScope.Generally speaking, this is best used for wrappers, such as executor services or lifecycle hooks, which usually have no current trace context when invoked.
Implementors note
For those overriding this method, you must compare
TraceContext.traceIdHigh(),TraceContext.traceId()andTraceContext.spanId()to decide if the contexts are equivalent. Due to details of propagation, other data like parent ID are not considered in equivalence checks.- Parameters:
context- span to place into scope or null to clear the scope- Returns:
- a new scope object or
CurrentTraceContext.Scope.NOOPif the input is already the case
-
wrap
Wraps the input so that it executes with the same context as now. -
wrap
Wraps the input so that it executes with the same context as now. -
executor
Decorates the input such that thecurrent trace contextat the time a task is scheduled is made current when the task is executed. -
executorService
Decorates the input such that thecurrent trace contextat the time a task is scheduled is made current when the task is executed.
-