Class SpanHandler
- java.lang.Object
-
- brave.handler.SpanHandler
-
- Direct Known Subclasses:
FinishedSpanHandler
public abstract class SpanHandler extends Object
This tracks one recording of aTraceContext. Common implementations include span reporting (ex to Zipkin) and data manipulation, such as redaction for security purposes.Relationship to Span lifecycle
The pair ofbegin(brave.propagation.TraceContext, brave.handler.MutableSpan, brave.propagation.TraceContext)andend(brave.propagation.TraceContext, brave.handler.MutableSpan, brave.handler.SpanHandler.Cause)seems the same as the span lifecycle. In most cases it will be the same, but you cannot assume this.A
TraceContextcould be recorded twice, for example, if a long operation began, calledSpan.flush()(recording 1) and later calledSpan.finish()(recording 2). ATraceContextcould be abrupted by garbage collection resulting in aSpanHandler.Cause.ABANDONED. A user could even abandon a span without recording anything!Collectors that process finished spans will need to look at the {link Cause} and
MutableSpancollected. For example,SpanHandler.Cause.FINISHEDis usually a good enough heuristic to find complete spans.Advanced Notes
It is important to do work quickly as callbacks are run on the same thread as application code. However, do not mutate
MutableSpanbetween callbacks, as it is not thread safe.The
TraceContextandMutableSpanparameter frombegin(brave.propagation.TraceContext, brave.handler.MutableSpan, brave.propagation.TraceContext)will be the same reference forend(brave.propagation.TraceContext, brave.handler.MutableSpan, brave.handler.SpanHandler.Cause).If caching the context or span parameters between callbacks, consider a
WeakReferenceto avoid holding up garbage collection.The
begin(brave.propagation.TraceContext, brave.handler.MutableSpan, brave.propagation.TraceContext)callback primarily supports tracking of children, or partitioning of data for backend that needs to see an entire local root.- Since:
- 5.12
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSpanHandler.CauseWhat ended the data collection?
-
Field Summary
Fields Modifier and Type Field Description static SpanHandlerNOOPUse to avoid comparing againstnullreferences.
-
Constructor Summary
Constructors Constructor Description SpanHandler()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanbegin(TraceContext context, MutableSpan span, TraceContext parent)This is called when a span is sampled, but before it is started.booleanend(TraceContext context, MutableSpan span, SpanHandler.Cause cause)Called when data collection complete.booleanhandlesAbandoned()Span.abandon()means the data is not intended to be recorded.
-
-
-
Field Detail
-
NOOP
public static final SpanHandler NOOP
Use to avoid comparing againstnullreferences.- Since:
- 5.12
-
-
Method Detail
-
begin
public boolean begin(TraceContext context, MutableSpan span, @Nullable TraceContext parent)
This is called when a span is sampled, but before it is started.- Parameters:
context- the trace context which isSamplingFlags.sampledLocal(). This includes identifiers and potentiallyextra propagated datasuch as baggage or extended sampling configuration.span- a mutable object that stores data recorded with span apis. Modifications are visible to later collectors.parent- can benullonly when the new context is a local root.- Returns:
trueretains the span, and should almost always be used.falsemakes it invisible to later handlers such as Zipkin.- Since:
- 5.12
- See Also:
Tracing.Builder.alwaysSampleLocal(),end(TraceContext, MutableSpan, Cause)
-
end
public boolean end(TraceContext context, MutableSpan span, SpanHandler.Cause cause)
Called when data collection complete.Advanced Note
By default, this only receives callbacks when data is intended to be recorded. If you are implementing tracking betweenbegin(brave.propagation.TraceContext, brave.handler.MutableSpan, brave.propagation.TraceContext)and here, you should consider overridinghandlesAbandoned()so that you have parity for all cases.- Parameters:
context- same instance as passed tobegin(brave.propagation.TraceContext, brave.handler.MutableSpan, brave.propagation.TraceContext)span- same instance as passed tobegin(brave.propagation.TraceContext, brave.handler.MutableSpan, brave.propagation.TraceContext)cause- why the data collection stopped.- Returns:
trueretains the span, and should almost always be used.falsedrops the span, making it invisible to later handlers such as Zipkin.- Since:
- 5.12
- See Also:
begin(TraceContext, MutableSpan, TraceContext),SpanHandler.Cause
-
handlesAbandoned
public boolean handlesAbandoned()
Span.abandon()means the data is not intended to be recorded. It results in an end callback withSpanHandler.Cause.ABANDONED.Note:
SpanHandler.Cause.ABANDONEDmeans the data is not intended to be recorded!
-
-