Class ThreadLocalCurrentTraceContext
- java.lang.Object
-
- brave.propagation.CurrentTraceContext
-
- brave.propagation.ThreadLocalCurrentTraceContext
-
- Direct Known Subclasses:
CurrentTraceContext.Default
public class ThreadLocalCurrentTraceContext extends CurrentTraceContext
In-process trace context propagation backed by a static thread local.Design notes
A static thread local ensures we have one context per thread, as opposed to one per thread- tracer. This means all tracer instances will be able to see any tracer's contexts.
The trade-off of this (instance-based reference) vs the reverse: trace contexts are not separated by tracer by default. For example, to make a trace invisible to another tracer, you have to use a non-default implementation.
Sometimes people make different instances of the tracer just to change configuration like the local service name. If we used a thread-instance approach, none of these would be able to see eachother's scopes. This would break
Tracing.currentTracer()scope visibility in a way few would want to debug. It might be phrased as "MySQL always starts a new trace and I don't know why."If you want a different behavior, use a different subtype of
CurrentTraceContext, possibly your own, or raise an issue and explain what your use case is.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classThreadLocalCurrentTraceContext.Builder-
Nested classes/interfaces inherited from class brave.propagation.CurrentTraceContext
CurrentTraceContext.Default, CurrentTraceContext.Scope, CurrentTraceContext.ScopeDecorator
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()This component is backed by a possibly static shared thread local.static CurrentTraceContextcreate()TraceContextget()Returns the current span in scope or null if there isn't one.static ThreadLocalCurrentTraceContext.BuildernewBuilder()CurrentTraceContext.ScopenewScope(TraceContext currentSpan)Sets the current span in scope until the returned object is closed.-
Methods inherited from class brave.propagation.CurrentTraceContext
decorateScope, executor, executorService, maybeScope, wrap, wrap
-
-
-
-
Method Detail
-
create
public static CurrentTraceContext create()
-
newBuilder
public static ThreadLocalCurrentTraceContext.Builder newBuilder()
-
clear
public void clear()
This component is backed by a possibly static shared thread local. Call this to clear the reference when you are sure any residual state is due to a leak. This is generally only useful in tests.- Since:
- 5.11
-
get
public TraceContext get()
Description copied from class:CurrentTraceContextReturns the current span in scope or null if there isn't one.- Specified by:
getin classCurrentTraceContext
-
newScope
public CurrentTraceContext.Scope newScope(@Nullable TraceContext currentSpan)
Description copied from class:CurrentTraceContextSets 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.- Specified by:
newScopein classCurrentTraceContext- Parameters:
currentSpan- span to place into scope or null to clear the scope
-
-