Class SdkSpan
- All Implemented Interfaces:
Span, ImplicitContextKeyed, ReadableSpan, ReadWriteSpan
Span class that records trace events.-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate AttributesMapprivate final AnchoredClockprivate final SpanContextprivate longprivate static final AttributeKey<String> private static final AttributeKey<String> private static final AttributeKey<String> private SdkSpan.EndStateprivate final InstrumentationScopeInfoprivate final SpanKindprivate final Objectprivate static final Loggerprivate Stringprivate final SpanContextprivate final Resourceprivate ThreadThe thread on whichend()is called and which will be invoking theSpanProcessors.private final SpanLimitsprivate final SpanProcessorprivate final longprivate StatusDataprivate intprivate int -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateSdkSpan(SpanContext context, String name, InstrumentationScopeInfo instrumentationScopeInfo, SpanKind kind, SpanContext parentSpanContext, SpanLimits spanLimits, SpanProcessor spanProcessor, AnchoredClock clock, Resource resource, AttributesMap attributes, List<LinkData> links, int totalRecordedLinks, long startEpochNanos) -
Method Summary
Modifier and TypeMethodDescriptionAdds an event to theSpan.Adds an event to theSpanwith the giventimestamp, as nanos since epoch.addEvent(String name, Attributes attributes) Adds an event to theSpanwith the givenAttributes.addEvent(String name, Attributes attributes, long timestamp, TimeUnit unit) addLink(SpanContext spanContext, Attributes attributes) Adds a link to thisSpan.private voidaddTimedEvent(EventData timedEvent) voidend()Marks the end ofSpanexecution.voidMarks the end ofSpanexecution with the specified timestamp.private voidendInternal(long endEpochNanos) <T> TgetAttribute(AttributeKey<T> key) Returns the value for the givenAttributeKey, ornullif not found.Returns the Span attributes.(package private) AnchoredClockgetClock()Returns theAnchoredClockused by thisSpan.private AttributesDeprecated.Returns the instrumentation scope specified when creating the tracer which produced this span.getKind()Returns the kind of the span.longReturns the latency of theSpanin nanos.getName()Returns the name of theSpan.(package private) ResourceReturns theSpanContextassociated with thisSpan.(package private) longbooleanhasEnded()Returns whether this Span has already been ended.private booleanbooleanReturnstrueif thisSpanrecords tracing events (e.g.recordException(Throwable exception) recordException(Throwable exception, Attributes additionalAttributes) <T> ReadWriteSpansetAttribute(AttributeKey<T> key, T value) Sets an attribute to theSpan.setStatus(StatusCode statusCode, String description) Sets the status to theSpan.(package private) static SdkSpanstartSpan(SpanContext context, String name, InstrumentationScopeInfo instrumentationScopeInfo, SpanKind kind, Span parentSpan, Context parentContext, SpanLimits spanLimits, SpanProcessor spanProcessor, Clock tracerClock, Resource resource, AttributesMap attributes, List<LinkData> links, int totalRecordedLinks, long userStartEpochNanos) Creates and starts a span with the given configuration.This converts this instance into an immutable SpanData instance, for use in export.toString()updateName(String name) Updates theSpanname.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface ImplicitContextKeyed
makeCurrentMethods inherited from interface Span
addEvent, addEvent, addLink, end, setAllAttributes, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setStatus, storeInContext
-
Field Details
-
logger
-
spanLimits
-
context
-
parentSpanContext
-
spanProcessor
-
kind
-
clock
-
resource
-
instrumentationScopeInfo
-
startEpochNanos
private final long startEpochNanos -
lock
-
name
-
attributes
-
events
-
totalRecordedEvents
private int totalRecordedEvents -
links
-
totalRecordedLinks
private int totalRecordedLinks -
status
-
endEpochNanos
private long endEpochNanos -
hasEnded
-
spanEndingThread
The thread on whichend()is called and which will be invoking theSpanProcessors. This field is used to ensure that only this thread may modify the span while it is in stateSdkSpan.EndState.ENDINGto prevent concurrent updates outside ofExtendedSpanProcessor.onEnding(ReadWriteSpan). -
EXCEPTION_TYPE
-
EXCEPTION_MESSAGE
-
EXCEPTION_STACKTRACE
-
-
Constructor Details
-
SdkSpan
private SdkSpan(SpanContext context, String name, InstrumentationScopeInfo instrumentationScopeInfo, SpanKind kind, SpanContext parentSpanContext, SpanLimits spanLimits, SpanProcessor spanProcessor, AnchoredClock clock, Resource resource, @Nullable AttributesMap attributes, @Nullable List<LinkData> links, int totalRecordedLinks, long startEpochNanos)
-
-
Method Details
-
startSpan
static SdkSpan startSpan(SpanContext context, String name, InstrumentationScopeInfo instrumentationScopeInfo, SpanKind kind, Span parentSpan, Context parentContext, SpanLimits spanLimits, SpanProcessor spanProcessor, Clock tracerClock, Resource resource, @Nullable AttributesMap attributes, @Nullable List<LinkData> links, int totalRecordedLinks, long userStartEpochNanos) Creates and starts a span with the given configuration.- Parameters:
context- supplies the trace_id and span_id for the newly started span.name- the displayed name for the new span.kind- the span kind.parentSpan- the parent span, orSpan.getInvalid()if this span is a root span.spanLimits- limits applied to this span.spanProcessor- handler called when the span starts and ends.tracerClock- the tracer's clockresource- the resource associated with this span.attributes- the attributes set during span creation.links- the links set during span creation, may be truncated. The list MUST be immutable.- Returns:
- a new and started span.
-
toSpanData
Description copied from interface:ReadableSpanThis converts this instance into an immutable SpanData instance, for use in export.- Specified by:
toSpanDatain interfaceReadableSpan- Returns:
- an immutable
SpanDatainstance.
-
getAttribute
Description copied from interface:ReadableSpanReturns the value for the givenAttributeKey, ornullif not found.The attribute values can be changed during the lifetime of the Span by using
Span.setAttribute(String, String)} so this value cannot be cached.Note: the implementation of this method performs locking to ensure thread-safe behavior.
- Specified by:
getAttributein interfaceReadableSpan- Returns:
- the value for the given
AttributeKey, ornullif not found.
-
getAttributes
Description copied from interface:ReadableSpanReturns the Span attributes.Attributes can be changed during the lifetime of the Span by using
Span.setAttribute(String, String)} so this value cannot be cached.Note: the implementation of this method performs locking and returns an immutable copy to ensure thread-safe behavior. If you only need a single attribute it is better to call
ReadableSpan.getAttribute(AttributeKey).- Specified by:
getAttributesin interfaceReadableSpan- Returns:
- the Span attributes, or
Attributes.empty()if the span has no attributes.
-
hasEnded
public boolean hasEnded()Description copied from interface:ReadableSpanReturns whether this Span has already been ended.Note: the implementation of this method performs locking to ensure thread-safe behavior.
- Specified by:
hasEndedin interfaceReadableSpan- Returns:
trueif the span has already been ended,falseif not.
-
getSpanContext
Description copied from interface:SpanReturns theSpanContextassociated with thisSpan.- Specified by:
getSpanContextin interfaceReadableSpan- Specified by:
getSpanContextin interfaceSpan- Returns:
- the
SpanContextassociated with thisSpan.
-
getParentSpanContext
Description copied from interface:ReadableSpan- Specified by:
getParentSpanContextin interfaceReadableSpan- Returns:
- the parent
SpanContextof theSpan
-
getName
Returns the name of theSpan.- Specified by:
getNamein interfaceReadableSpan- Returns:
- the name of the
Span.
-
getInstrumentationLibraryInfo
Deprecated.Description copied from interface:ReadableSpanReturns the instrumentation library specified when creating the tracer which produced this span.- Specified by:
getInstrumentationLibraryInfoin interfaceReadableSpan- Returns:
- an instance of
InstrumentationLibraryInfodescribing the instrumentation library
-
getInstrumentationScopeInfo
Description copied from interface:ReadableSpanReturns the instrumentation scope specified when creating the tracer which produced this span.- Specified by:
getInstrumentationScopeInfoin interfaceReadableSpan- Returns:
- an instance of
InstrumentationScopeInfodescribing the instrumentation scope
-
getLatencyNanos
public long getLatencyNanos()Returns the latency of theSpanin nanos. If still active then returns now() - start time.- Specified by:
getLatencyNanosin interfaceReadableSpan- Returns:
- the latency of the
Spanin nanos.
-
getClock
AnchoredClock getClock()Returns theAnchoredClockused by thisSpan. -
setAttribute
Description copied from interface:SpanSets an attribute to theSpan. If theSpanpreviously contained a mapping for the key, the old value is replaced by the specified value.Note: the behavior of null values is undefined, and hence strongly discouraged.
- Specified by:
setAttributein interfaceSpan- Parameters:
key- the key for this attribute.value- the value for this attribute.- Returns:
- this.
-
isModifiableByCurrentThread
private boolean isModifiableByCurrentThread() -
addEvent
-
addEvent
Description copied from interface:SpanAdds an event to theSpanwith the giventimestamp, as nanos since epoch. Note, thistimestampis not the same asSystem.nanoTime()but may be computed using it, for example, by taking a difference of readings fromSystem.nanoTime()and adding to the span start time.When possible, it is preferred to use
Span.addEvent(String)at the time the event occurred. -
addEvent
Description copied from interface:SpanAdds an event to theSpanwith the givenAttributes. The timestamp of the event will be the current time. -
addEvent
Description copied from interface:SpanAdds an event to theSpanwith the givenAttributesandtimestamp. Note, thistimestampis not the same asSystem.nanoTime()but may be computed using it, for example, by taking a difference of readings fromSystem.nanoTime()and adding to the span start time.When possible, it is preferred to use
Span.addEvent(String)at the time the event occurred.- Specified by:
addEventin interfaceSpan- Parameters:
name- the name of the event.attributes- the attributes that will be added; these are associated with this event, not theSpanas forsetAttribute().timestamp- the explicit event timestamp since epoch.unit- the unit of the timestamp- Returns:
- this.
-
addTimedEvent
-
setStatus
Description copied from interface:SpanSets the status to theSpan.If used, this will override the default
Spanstatus. Default status code isStatusCode.UNSET.Only the value of the last call will be recorded, and implementations are free to ignore previous calls.
- Specified by:
setStatusin interfaceSpan- Parameters:
statusCode- theStatusCodeto set.description- the description of theStatus.- Returns:
- this.
-
recordException
Description copied from interface:SpanRecords information about theThrowableto theSpan.Note that the EXCEPTION_ESCAPED value from the Semantic Conventions cannot be determined by this function. You should record this attribute manually using
Span.recordException(Throwable, Attributes)if you know that an exception is escaping.- Specified by:
recordExceptionin interfaceSpan- Parameters:
exception- theThrowableto record.- Returns:
- this.
-
recordException
Description copied from interface:Span- Specified by:
recordExceptionin interfaceSpan- Parameters:
exception- theThrowableto record.additionalAttributes- the additionalAttributesto record.- Returns:
- this.
-
updateName
Description copied from interface:SpanUpdates theSpanname.If used, this will override the name provided via
Span.Builder.Upon this update, any sampling behavior based on
Spanname will depend on the implementation.- Specified by:
updateNamein interfaceSpan- Parameters:
name- theSpanname.- Returns:
- this.
-
addLink
Description copied from interface:SpanAdds a link to thisSpan.Links are used to link
Spans in different traces. Used (for example) in batching operations, where a single batch handler processes multiple requests from different traces or the same trace.Implementations may ignore calls with an invalid span context.
Callers should prefer to add links before starting the span via
SpanBuilder.addLink(SpanContext, Attributes)if possible. -
end
-
end
Description copied from interface:SpanMarks the end ofSpanexecution with the specified timestamp.Only the timing of the first end call for a given
Spanwill be recorded, and implementations are free to ignore all further calls.Use this method for specifying explicit end options, such as end
Timestamp. When no explicit values are required, useSpan.end(). -
endInternal
private void endInternal(long endEpochNanos) -
isRecording
public boolean isRecording()Description copied from interface:SpanReturnstrueif thisSpanrecords tracing events (e.g.Span.addEvent(String),Span.setAttribute(String, long)).- Specified by:
isRecordingin interfaceSpan- Returns:
trueif thisSpanrecords tracing events.
-
getResource
Resource getResource() -
getKind
Description copied from interface:ReadableSpanReturns the kind of the span.- Specified by:
getKindin interfaceReadableSpan- Returns:
- the kind of the span.
-
getStartEpochNanos
long getStartEpochNanos() -
getImmutableTimedEvents
-
getImmutableAttributes
-
getImmutableLinks
-
toString
-