Class SdkSpan
- java.lang.Object
-
- io.opentelemetry.sdk.trace.SdkSpan
-
- All Implemented Interfaces:
Span,ImplicitContextKeyed,ReadableSpan,ReadWriteSpan
@ThreadSafe final class SdkSpan extends java.lang.Object implements ReadWriteSpan
Implementation for theSpanclass that records trace events.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classSdkSpan.EndState
-
Field Summary
Fields Modifier and Type Field Description private AttributesMapattributesprivate AnchoredClockclockprivate SpanContextcontextprivate longendEpochNanosprivate java.util.List<EventData>eventsprivate static AttributeKey<java.lang.String>EXCEPTION_MESSAGEprivate static AttributeKey<java.lang.String>EXCEPTION_STACKTRACEprivate static AttributeKey<java.lang.String>EXCEPTION_TYPEprivate SdkSpan.EndStatehasEndedprivate InstrumentationScopeInfoinstrumentationScopeInfoprivate SpanKindkind(package private) java.util.List<LinkData>linksprivate java.lang.Objectlockprivate static java.util.logging.Loggerloggerprivate java.lang.Stringnameprivate SpanContextparentSpanContextprivate Resourceresourceprivate java.lang.ThreadspanEndingThreadThe thread on whichend()is called and which will be invoking theSpanProcessors.private SpanLimitsspanLimitsprivate SpanProcessorspanProcessorprivate longstartEpochNanosprivate StatusDatastatusprivate inttotalRecordedEventsprivate inttotalRecordedLinks
-
Constructor Summary
Constructors Modifier Constructor Description privateSdkSpan(SpanContext context, java.lang.String name, InstrumentationScopeInfo instrumentationScopeInfo, SpanKind kind, SpanContext parentSpanContext, SpanLimits spanLimits, SpanProcessor spanProcessor, AnchoredClock clock, Resource resource, AttributesMap attributes, java.util.List<LinkData> links, int totalRecordedLinks, long startEpochNanos)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description ReadWriteSpanaddEvent(java.lang.String name)Adds an event to theSpan.ReadWriteSpanaddEvent(java.lang.String name, long timestamp, java.util.concurrent.TimeUnit unit)Adds an event to theSpanwith the giventimestamp, as nanos since epoch.ReadWriteSpanaddEvent(java.lang.String name, Attributes attributes)Adds an event to theSpanwith the givenAttributes.ReadWriteSpanaddEvent(java.lang.String name, Attributes attributes, long timestamp, java.util.concurrent.TimeUnit unit)SpanaddLink(SpanContext spanContext, Attributes attributes)Adds a link to thisSpan.private voidaddTimedEvent(EventData timedEvent)voidend()Marks the end ofSpanexecution.voidend(long timestamp, java.util.concurrent.TimeUnit unit)Marks 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.AttributesgetAttributes()Returns the Span attributes.(package private) AnchoredClockgetClock()Returns theAnchoredClockused by thisSpan.private AttributesgetImmutableAttributes()private java.util.List<LinkData>getImmutableLinks()private java.util.List<EventData>getImmutableTimedEvents()InstrumentationLibraryInfogetInstrumentationLibraryInfo()Deprecated.InstrumentationScopeInfogetInstrumentationScopeInfo()Returns the instrumentation scope specified when creating the tracer which produced this span.SpanKindgetKind()Returns the kind of the span.longgetLatencyNanos()Returns the latency of theSpanin nanos.java.lang.StringgetName()Returns the name of theSpan.SpanContextgetParentSpanContext()(package private) ResourcegetResource()SpanContextgetSpanContext()Returns theSpanContextassociated with thisSpan.(package private) longgetStartEpochNanos()booleanhasEnded()Returns whether this Span has already been ended.private booleanisModifiableByCurrentThread()booleanisRecording()Returnstrueif thisSpanrecords tracing events (e.g.ReadWriteSpanrecordException(java.lang.Throwable exception)Records information about theThrowableto theSpan.ReadWriteSpanrecordException(java.lang.Throwable exception, Attributes additionalAttributes)Records information about theThrowableto theSpan.<T> ReadWriteSpansetAttribute(AttributeKey<T> key, T value)Sets an attribute to theSpan.ReadWriteSpansetStatus(StatusCode statusCode, java.lang.String description)Sets the status to theSpan.(package private) static SdkSpanstartSpan(SpanContext context, java.lang.String name, InstrumentationScopeInfo instrumentationScopeInfo, SpanKind kind, Span parentSpan, Context parentContext, SpanLimits spanLimits, SpanProcessor spanProcessor, Clock tracerClock, Resource resource, AttributesMap attributes, java.util.List<LinkData> links, int totalRecordedLinks, long userStartEpochNanos)Creates and starts a span with the given configuration.SpanDatatoSpanData()This converts this instance into an immutable SpanData instance, for use in export.java.lang.StringtoString()ReadWriteSpanupdateName(java.lang.String name)Updates theSpanname.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.opentelemetry.context.ImplicitContextKeyed
makeCurrent
-
Methods inherited from interface io.opentelemetry.api.trace.Span
addEvent, addEvent, addLink, end, setAllAttributes, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setStatus, storeInContext
-
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
spanLimits
private final SpanLimits spanLimits
-
context
private final SpanContext context
-
parentSpanContext
private final SpanContext parentSpanContext
-
spanProcessor
private final SpanProcessor spanProcessor
-
kind
private final SpanKind kind
-
clock
private final AnchoredClock clock
-
resource
private final Resource resource
-
instrumentationScopeInfo
private final InstrumentationScopeInfo instrumentationScopeInfo
-
startEpochNanos
private final long startEpochNanos
-
lock
private final java.lang.Object lock
-
name
private java.lang.String name
-
attributes
@Nullable private AttributesMap attributes
-
events
@Nullable private java.util.List<EventData> events
-
totalRecordedEvents
private int totalRecordedEvents
-
links
@Nullable java.util.List<LinkData> links
-
totalRecordedLinks
private int totalRecordedLinks
-
status
private StatusData status
-
endEpochNanos
private long endEpochNanos
-
hasEnded
private SdkSpan.EndState hasEnded
-
spanEndingThread
@Nullable private java.lang.Thread 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
private static final AttributeKey<java.lang.String> EXCEPTION_TYPE
-
EXCEPTION_MESSAGE
private static final AttributeKey<java.lang.String> EXCEPTION_MESSAGE
-
EXCEPTION_STACKTRACE
private static final AttributeKey<java.lang.String> EXCEPTION_STACKTRACE
-
-
Constructor Detail
-
SdkSpan
private SdkSpan(SpanContext context, java.lang.String name, InstrumentationScopeInfo instrumentationScopeInfo, SpanKind kind, SpanContext parentSpanContext, SpanLimits spanLimits, SpanProcessor spanProcessor, AnchoredClock clock, Resource resource, @Nullable AttributesMap attributes, @Nullable java.util.List<LinkData> links, int totalRecordedLinks, long startEpochNanos)
-
-
Method Detail
-
startSpan
static SdkSpan startSpan(SpanContext context, java.lang.String name, InstrumentationScopeInfo instrumentationScopeInfo, SpanKind kind, Span parentSpan, Context parentContext, SpanLimits spanLimits, SpanProcessor spanProcessor, Clock tracerClock, Resource resource, @Nullable AttributesMap attributes, @Nullable java.util.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
public SpanData 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
@Nullable public <T> T getAttribute(AttributeKey<T> key)
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(java.lang.String, java.lang.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
public Attributes getAttributes()
Description copied from interface:ReadableSpanReturns the Span attributes.Attributes can be changed during the lifetime of the Span by using
Span.setAttribute(java.lang.String, java.lang.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
public SpanContext getSpanContext()
Description copied from interface:SpanReturns theSpanContextassociated with thisSpan.- Specified by:
getSpanContextin interfaceReadableSpan- Specified by:
getSpanContextin interfaceSpan- Returns:
- the
SpanContextassociated with thisSpan.
-
getParentSpanContext
public SpanContext getParentSpanContext()
Description copied from interface:ReadableSpan- Specified by:
getParentSpanContextin interfaceReadableSpan- Returns:
- the parent
SpanContextof theSpan
-
getName
public java.lang.String getName()
Returns the name of theSpan.- Specified by:
getNamein interfaceReadableSpan- Returns:
- the name of the
Span.
-
getInstrumentationLibraryInfo
@Deprecated public InstrumentationLibraryInfo 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
public InstrumentationScopeInfo 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
public <T> ReadWriteSpan setAttribute(AttributeKey<T> key, T value)
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
public ReadWriteSpan addEvent(java.lang.String name)
Description copied from interface:SpanAdds an event to theSpan. The timestamp of the event will be the current time.
-
addEvent
public ReadWriteSpan addEvent(java.lang.String name, long timestamp, java.util.concurrent.TimeUnit unit)
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
public ReadWriteSpan addEvent(java.lang.String name, Attributes attributes)
Description copied from interface:SpanAdds an event to theSpanwith the givenAttributes. The timestamp of the event will be the current time.
-
addEvent
public ReadWriteSpan addEvent(java.lang.String name, Attributes attributes, long timestamp, java.util.concurrent.TimeUnit unit)
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
private void addTimedEvent(EventData timedEvent)
-
setStatus
public ReadWriteSpan setStatus(StatusCode statusCode, @Nullable java.lang.String description)
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
public ReadWriteSpan recordException(java.lang.Throwable exception)
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
public ReadWriteSpan recordException(java.lang.Throwable exception, Attributes additionalAttributes)
Description copied from interface:SpanRecords information about theThrowableto theSpan.- Specified by:
recordExceptionin interfaceSpan- Parameters:
exception- theThrowableto record.additionalAttributes- the additionalAttributesto record.- Returns:
- this.
-
updateName
public ReadWriteSpan updateName(java.lang.String name)
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
public Span addLink(SpanContext spanContext, Attributes attributes)
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
public void end()
Description copied from interface:SpanMarks the end ofSpanexecution.Only the timing of the first end call for a given
Spanwill be recorded, and implementations are free to ignore all further calls.
-
end
public void end(long timestamp, java.util.concurrent.TimeUnit unit)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
public SpanKind 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
private java.util.List<EventData> getImmutableTimedEvents()
-
getImmutableAttributes
private Attributes getImmutableAttributes()
-
getImmutableLinks
private java.util.List<LinkData> getImmutableLinks()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-