Class AbstractSpan<T extends AbstractSpan<T>>
- java.lang.Object
-
- co.elastic.apm.agent.impl.transaction.AbstractSpan<T>
-
- All Implemented Interfaces:
Recyclable
- Direct Known Subclasses:
Span,Transaction
public abstract class AbstractSpan<T extends AbstractSpan<T>> extends java.lang.Object implements Recyclable
-
-
Field Summary
Fields Modifier and Type Field Description protected booleancollectBreakdownMetricsprotected longdurationprotected booleanfinishedprotected static doubleMS_IN_MICROSprotected java.lang.StringBuildernameGeneric designation of a transaction in the scope of a single service (eg: 'GET /users/:id')static intPRIO_DEFAULTstatic intPRIO_HIGH_LEVEL_FRAMEWORKstatic intPRIO_LOW_LEVEL_FRAMEWORKstatic intPRIO_METHOD_SIGNATUREstatic intPRIO_USER_SUPPLIEDprotected java.util.concurrent.atomic.AtomicIntegerreferencesprotected TraceContexttraceContextprotected ElasticApmTracertracer
-
Constructor Summary
Constructors Constructor Description AbstractSpan(ElasticApmTracer tracer)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Tactivate()ScopeactivateInScope()voidaddLabel(java.lang.String key, java.lang.Boolean value)voidaddLabel(java.lang.String key, java.lang.Number value)voidaddLabel(java.lang.String key, java.lang.String value)protected abstract voidafterEnd()TappendToName(java.lang.CharSequence cs)Appends a string to the name.TappendToName(java.lang.CharSequence cs, int priority)TasExit()protected abstract voidbeforeEnd(long epochMicros)voidcaptureException(long epochMicros, java.lang.Throwable t)TcaptureException(java.lang.Throwable t)java.lang.StringcaptureExceptionAndGetErrorId(java.lang.Throwable t)SpancreateExitSpan()Creates a child Span representing a remote call event, unless this TraceContextHolder already represents an exit event.SpancreateSpan()SpancreateSpan(long epochMicros)Tdeactivate()voiddecrementReferences()voidend()voidend(long epochMicros)voidendExceptionally(java.lang.Throwable t)java.lang.StringBuildergetAndOverrideName(int namePriority)java.lang.StringBuildergetAndOverrideName(int namePriority, boolean overrideIfSamePriority)Resets and returns the nameStringBuilderif one of the following applies: the provided priority is>namePrioritythe provided priority is==namePriorityANDoverrideIfSamePriorityistrueOtherwise, returnsnullLongListgetChildIds()abstract AbstractContextgetContext()longgetDuration()How long the transaction took to complete, in µsdoublegetDurationMs()java.lang.StringgetNameAsString()Only intended for testing purposes as this allocates aStringjava.lang.StringBuildergetNameForSerialization()Only intended to be used byDslJsonSerializerOutcomegetOutcome()intgetReferenceCount()longgetSelfDuration()longgetTimestamp()Recorded time of the span or transaction in microseconds since epochTraceContextgetTraceContext()abstract TransactiongetTransaction()protected booleanhasCapturedExceptions()voidincrementReferences()booleanisChildOf(AbstractSpan<?> parent)booleanisDiscardable()Returns whether it's possible to discard this span.booleanisDiscarded()Determines whether to discard the span.booleanisExit()booleanisFinished()booleanisReferenced()booleanisSampled()protected voidonAfterStart()Called after the span has been started and its parent references are setprotected booleanoutcomeNotSet()<C> booleanpropagateTraceContext(C carrier, BinaryHeaderSetter<C> headerSetter)Sets Trace context binary headers, using this context as parent, on the provided carrier using the provided setter<C> voidpropagateTraceContext(C carrier, TextHeaderSetter<C> headerSetter)Sets Trace context text headers, using this context as parent, on the provided carrier using the provided setterprotected abstract voidrecycle()TrequestDiscarding()Requests this span to be discarded, even if it's sampled.voidresetState()resets pooled object state so it can be reusedvoidsetNonDiscardable()Sets this context as non-discardable, meaning thatisDiscarded()will returnfalse, even ifrequestDiscarding()has been called.voidsetStartTimestamp(long epochMicros)Set start timestampvoidsetStartTimestampNow()Set start timestamp from context current clockprotected abstract Tthiz()TwithChildIds(LongList childIds)TwithName(java.lang.String name)TwithName(java.lang.String name, int priority)TwithName(java.lang.String name, int priority, boolean overrideIfSamePriority)TwithOutcome(Outcome outcome)Sets outcomeTwithUserOutcome(Outcome outcome)Sets user outcome, which has priority over outcome set throughwithOutcome(Outcome)
-
-
-
Field Detail
-
PRIO_USER_SUPPLIED
public static final int PRIO_USER_SUPPLIED
- See Also:
- Constant Field Values
-
PRIO_HIGH_LEVEL_FRAMEWORK
public static final int PRIO_HIGH_LEVEL_FRAMEWORK
- See Also:
- Constant Field Values
-
PRIO_METHOD_SIGNATURE
public static final int PRIO_METHOD_SIGNATURE
- See Also:
- Constant Field Values
-
PRIO_LOW_LEVEL_FRAMEWORK
public static final int PRIO_LOW_LEVEL_FRAMEWORK
- See Also:
- Constant Field Values
-
PRIO_DEFAULT
public static final int PRIO_DEFAULT
- See Also:
- Constant Field Values
-
MS_IN_MICROS
protected static final double MS_IN_MICROS
-
traceContext
protected final TraceContext traceContext
-
name
protected final java.lang.StringBuilder name
Generic designation of a transaction in the scope of a single service (eg: 'GET /users/:id')
-
collectBreakdownMetrics
protected final boolean collectBreakdownMetrics
-
tracer
protected final ElasticApmTracer tracer
-
duration
protected long duration
-
references
protected java.util.concurrent.atomic.AtomicInteger references
-
finished
protected volatile boolean finished
-
-
Constructor Detail
-
AbstractSpan
public AbstractSpan(ElasticApmTracer tracer)
-
-
Method Detail
-
getReferenceCount
public int getReferenceCount()
-
requestDiscarding
public T requestDiscarding()
Requests this span to be discarded, even if it's sampled.Whether the span can actually be discarded is determined by
isDiscarded()
-
isDiscarded
public boolean isDiscarded()
Determines whether to discard the span. Only spans that returnfalseare reported.A span is discarded if it is discardable and requested to be discarded.
- Returns:
true, if the span should be discarded,falseotherwise.
-
getTransaction
@Nullable public abstract Transaction getTransaction()
-
isReferenced
public boolean isReferenced()
-
isFinished
public boolean isFinished()
-
getDuration
public long getDuration()
How long the transaction took to complete, in µs
-
getSelfDuration
public long getSelfDuration()
-
getDurationMs
public double getDurationMs()
-
getNameForSerialization
public java.lang.StringBuilder getNameForSerialization()
Only intended to be used byDslJsonSerializer
-
getAndOverrideName
@Nullable public java.lang.StringBuilder getAndOverrideName(int namePriority)
Resets and returns the nameStringBuilderif the provided priority is>=namePriorityone. Otherwise, returnsnull- Parameters:
namePriority- the priority for the name. See also theAbstractSpan#PRIO_*constants.- Returns:
- the name
StringBuilderif the provided priority is>=namePriority,nullotherwise.
-
getAndOverrideName
@Nullable public java.lang.StringBuilder getAndOverrideName(int namePriority, boolean overrideIfSamePriority)Resets and returns the nameStringBuilderif one of the following applies:- the provided priority is
>namePriority - the provided priority is
==namePriorityANDoverrideIfSamePriorityistrue
null- Parameters:
namePriority- the priority for the name. See also theAbstractSpan#PRIO_*constants.overrideIfSamePriority- specifies whether the existing name should be overridden ifnamePriorityequals the priority used to set the current name- Returns:
- the name
StringBuilderif the provided priority is sufficient for overriding,nullotherwise.
- the provided priority is
-
getNameAsString
public java.lang.String getNameAsString()
Only intended for testing purposes as this allocates aString- Returns:
- name
-
appendToName
public T appendToName(java.lang.CharSequence cs)
Appends a string to the name.This method helps to avoid the memory allocations of string concatenations as the underlying
StringBuilderinstance will be reused.- Parameters:
cs- the char sequence to append to the name- Returns:
this, for chaining
-
appendToName
public T appendToName(java.lang.CharSequence cs, int priority)
-
withName
public T withName(@Nullable java.lang.String name)
-
withName
public T withName(@Nullable java.lang.String name, int priority)
-
withName
public T withName(@Nullable java.lang.String name, int priority, boolean overrideIfSamePriority)
-
getTimestamp
public long getTimestamp()
Recorded time of the span or transaction in microseconds since epoch
-
getTraceContext
public TraceContext getTraceContext()
-
resetState
public void resetState()
Description copied from interface:Recyclableresets pooled object state so it can be reused- Specified by:
resetStatein interfaceRecyclable
-
createSpan
public Span createSpan()
-
createSpan
public Span createSpan(long epochMicros)
-
createExitSpan
@Nullable public Span createExitSpan()
Creates a child Span representing a remote call event, unless this TraceContextHolder already represents an exit event. If current TraceContextHolder is representing an Exit- returns null- Returns:
- an Exit span if this TraceContextHolder is not an exit span, null otherwise
-
asExit
public T asExit()
-
isExit
public boolean isExit()
-
captureException
public void captureException(long epochMicros, java.lang.Throwable t)
-
captureException
public T captureException(@Nullable java.lang.Throwable t)
-
endExceptionally
public void endExceptionally(@Nullable java.lang.Throwable t)
-
captureExceptionAndGetErrorId
@Nullable public java.lang.String captureExceptionAndGetErrorId(@Nullable java.lang.Throwable t)
-
addLabel
public void addLabel(java.lang.String key, java.lang.String value)
-
addLabel
public void addLabel(java.lang.String key, java.lang.Number value)
-
addLabel
public void addLabel(java.lang.String key, java.lang.Boolean value)
-
getContext
public abstract AbstractContext getContext()
-
onAfterStart
protected void onAfterStart()
Called after the span has been started and its parent references are set
-
end
public void end()
-
end
public final void end(long epochMicros)
-
outcomeNotSet
protected boolean outcomeNotSet()
- Returns:
- true if outcome has NOT been set, either by user or through instrumentation
-
hasCapturedExceptions
protected boolean hasCapturedExceptions()
- Returns:
- true if an exception has been captured
-
beforeEnd
protected abstract void beforeEnd(long epochMicros)
-
afterEnd
protected abstract void afterEnd()
-
isChildOf
public boolean isChildOf(AbstractSpan<?> parent)
-
activate
public T activate()
-
deactivate
public T deactivate()
-
activateInScope
public Scope activateInScope()
-
setStartTimestamp
public void setStartTimestamp(long epochMicros)
Set start timestamp- Parameters:
epochMicros- start timestamp in micro-seconds since epoch
-
setStartTimestampNow
public void setStartTimestampNow()
Set start timestamp from context current clock
-
incrementReferences
public void incrementReferences()
-
decrementReferences
public void decrementReferences()
-
recycle
protected abstract void recycle()
-
propagateTraceContext
public <C> void propagateTraceContext(C carrier, TextHeaderSetter<C> headerSetter)Sets Trace context text headers, using this context as parent, on the provided carrier using the provided setter- Type Parameters:
C- the header carrier type, for example - an HTTP request- Parameters:
carrier- the text headers carrierheaderSetter- a setter implementing the actual addition of headers to the headers carrier
-
propagateTraceContext
public <C> boolean propagateTraceContext(C carrier, BinaryHeaderSetter<C> headerSetter)Sets Trace context binary headers, using this context as parent, on the provided carrier using the provided setter- Type Parameters:
C- the header carrier type, for example - a Kafka record- Parameters:
carrier- the binary headers carrierheaderSetter- a setter implementing the actual addition of headers to the headers carrier- Returns:
- true if Trace Context headers were set; false otherwise
-
setNonDiscardable
public void setNonDiscardable()
Sets this context as non-discardable, meaning thatisDiscarded()will returnfalse, even ifrequestDiscarding()has been called.
-
isDiscardable
public boolean isDiscardable()
Returns whether it's possible to discard this span.- Returns:
true, if it's safe to discard the span,falseotherwise.
-
isSampled
public boolean isSampled()
-
getChildIds
@Nullable public LongList getChildIds()
-
thiz
protected abstract T thiz()
-
getOutcome
public Outcome getOutcome()
- Returns:
- user outcome if set, otherwise outcome value
-
withOutcome
public T withOutcome(Outcome outcome)
Sets outcome- Parameters:
outcome- outcome- Returns:
- this
-
withUserOutcome
public T withUserOutcome(Outcome outcome)
Sets user outcome, which has priority over outcome set throughwithOutcome(Outcome)- Parameters:
outcome- user outcome- Returns:
- this
-
-