Interface Span
-
- All Superinterfaces:
io.opentelemetry.context.ImplicitContextKeyed
@ThreadSafe public interface Span extends io.opentelemetry.context.ImplicitContextKeyedAn interface that represents a span. It has an associatedSpanContext.Spans are created by the
SpanBuilder.startSpan()method.Spanmust be ended by callingend().
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classSpan.KindType of span.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default SpanaddEvent(String name)Adds an event to theSpan.default SpanaddEvent(String name, long timestamp, TimeUnit unit)Adds an event to theSpanwith the giventimestamp, as nanos since epoch.SpanaddEvent(String name, io.opentelemetry.api.common.Attributes attributes)Adds an event to theSpanwith the givenAttributes.SpanaddEvent(String name, io.opentelemetry.api.common.Attributes attributes, long timestamp, TimeUnit unit)default SpanaddEvent(String name, io.opentelemetry.api.common.Attributes attributes, Instant timestamp)default SpanaddEvent(String name, Instant timestamp)Adds an event to theSpanwith the giventimestamp, as nanos since epoch.static Spancurrent()voidend()Marks the end ofSpanexecution.voidend(long timestamp, TimeUnit unit)Marks the end ofSpanexecution with the specified timestamp.default voidend(Instant timestamp)Marks the end ofSpanexecution with the specified timestamp.static SpanfromContext(io.opentelemetry.context.Context context)static SpanfromContextOrNull(io.opentelemetry.context.Context context)static SpangetInvalid()Returns an invalidSpan.SpanContextgetSpanContext()Returns theSpanContextassociated with thisSpan.booleanisRecording()Returnstrueif thisSpanrecords tracing events (e.g.default SpanrecordException(Throwable exception)SpanrecordException(Throwable exception, io.opentelemetry.api.common.Attributes additionalAttributes)default SpansetAttribute(io.opentelemetry.api.common.AttributeKey<Long> key, int value)Sets an attribute to theSpan.<T> SpansetAttribute(io.opentelemetry.api.common.AttributeKey<T> key, T value)Sets an attribute to theSpan.default SpansetAttribute(String key, boolean value)Sets an attribute to theSpan.default SpansetAttribute(String key, double value)Sets an attribute to theSpan.default SpansetAttribute(String key, long value)Sets an attribute to theSpan.default SpansetAttribute(String key, String value)Sets an attribute to theSpan.default SpansetStatus(StatusCode statusCode)Sets the status to theSpan.SpansetStatus(StatusCode statusCode, String description)Sets the status to theSpan.default io.opentelemetry.context.ContextstoreInContext(io.opentelemetry.context.Context context)SpanupdateName(String name)Updates theSpanname.static Spanwrap(SpanContext spanContext)Returns a non-recordingSpanthat holds the providedSpanContextbut has no functionality.
-
-
-
Method Detail
-
current
static Span current()
-
fromContext
static Span fromContext(io.opentelemetry.context.Context context)
-
fromContextOrNull
@Nullable static Span fromContextOrNull(io.opentelemetry.context.Context context)
-
getInvalid
static Span getInvalid()
-
wrap
static Span wrap(SpanContext spanContext)
Returns a non-recordingSpanthat holds the providedSpanContextbut has no functionality. It will not be exported and all tracing operations are no-op, but it can be used to propagate a validSpanContextdownstream.
-
setAttribute
default Span setAttribute(String key, @Nonnull String value)
Sets an attribute to theSpan. If theSpanpreviously contained a mapping for the key, the old value is replaced by the specified value.If a null or empty String
valueis passed in, the behavior is undefined, and hence strongly discouraged.Note: It is strongly recommended to use
setAttribute(AttributeKey, Object), and pre-allocate your keys, if possible.- Parameters:
key- the key for this attribute.value- the value for this attribute.- Returns:
- this.
-
setAttribute
default Span setAttribute(String key, long value)
Sets an attribute to theSpan. If theSpanpreviously contained a mapping for the key, the old value is replaced by the specified value.Note: It is strongly recommended to use
setAttribute(AttributeKey, Object), and pre-allocate your keys, if possible.- Parameters:
key- the key for this attribute.value- the value for this attribute.- Returns:
- this.
-
setAttribute
default Span setAttribute(String key, double value)
Sets an attribute to theSpan. If theSpanpreviously contained a mapping for the key, the old value is replaced by the specified value.Note: It is strongly recommended to use
setAttribute(AttributeKey, Object), and pre-allocate your keys, if possible.- Parameters:
key- the key for this attribute.value- the value for this attribute.- Returns:
- this.
-
setAttribute
default Span setAttribute(String key, boolean value)
Sets an attribute to theSpan. If theSpanpreviously contained a mapping for the key, the old value is replaced by the specified value.Note: It is strongly recommended to use
setAttribute(AttributeKey, Object), and pre-allocate your keys, if possible.- Parameters:
key- the key for this attribute.value- the value for this attribute.- Returns:
- this.
-
setAttribute
<T> Span setAttribute(io.opentelemetry.api.common.AttributeKey<T> key, @Nonnull T value)
Sets 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.
- Parameters:
key- the key for this attribute.value- the value for this attribute.- Returns:
- this.
-
setAttribute
default Span setAttribute(io.opentelemetry.api.common.AttributeKey<Long> key, int value)
Sets an attribute to theSpan. If theSpanpreviously contained a mapping for the key, the old value is replaced by the specified value.- Parameters:
key- the key for this attribute.value- the value for this attribute.- Returns:
- this.
-
addEvent
default Span addEvent(String name)
Adds an event to theSpan. The timestamp of the event will be the current time.- Parameters:
name- the name of the event.- Returns:
- this.
-
addEvent
default Span addEvent(String name, long timestamp, TimeUnit unit)
Adds 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
addEvent(String)at the time the event occurred.- Parameters:
name- the name of the event.timestamp- the explicit event timestamp since epoch.unit- the unit of the timestamp- Returns:
- this.
-
addEvent
default Span addEvent(String name, Instant timestamp)
Adds 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
addEvent(String)at the time the event occurred.- Parameters:
name- the name of the event.timestamp- the explicit event timestamp since epoch.- Returns:
- this.
-
addEvent
Span addEvent(String name, io.opentelemetry.api.common.Attributes attributes)
Adds an event to theSpanwith the givenAttributes. The timestamp of the event will be the current time.- Parameters:
name- the name of the event.attributes- the attributes that will be added; these are associated with this event, not theSpanas forsetAttribute().- Returns:
- this.
-
addEvent
Span addEvent(String name, io.opentelemetry.api.common.Attributes attributes, long timestamp, TimeUnit unit)
Adds 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
addEvent(String)at the time the event occurred.- 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.
-
addEvent
default Span addEvent(String name, io.opentelemetry.api.common.Attributes attributes, Instant timestamp)
Adds 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
addEvent(String)at the time the event occurred.- 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.- Returns:
- this.
-
setStatus
default Span setStatus(StatusCode statusCode)
Sets 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.
- Parameters:
statusCode- theStatusCodeto set.- Returns:
- this.
-
setStatus
Span setStatus(StatusCode statusCode, String description)
Sets 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.
- Parameters:
statusCode- theStatusCodeto set.description- the description of theStatus.- Returns:
- this.
-
recordException
default Span recordException(Throwable exception)
Records information about theThrowableto theSpan.Note that
SemanticAttributes.EXCEPTION_ESCAPEDcannot be determined by this function. You should record this attribute manually usingrecordException(Throwable, Attributes)if you know that an exception is escaping.- Parameters:
exception- theThrowableto record.- Returns:
- this.
-
recordException
Span recordException(Throwable exception, io.opentelemetry.api.common.Attributes additionalAttributes)
- Parameters:
exception- theThrowableto record.additionalAttributes- the additionalAttributesto record.- Returns:
- this.
-
updateName
Span updateName(String name)
Updates 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.- Parameters:
name- theSpanname.- Returns:
- this.
-
end
void end()
Marks 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
void end(long timestamp, TimeUnit unit)Marks 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, useend().- Parameters:
timestamp- the explicit timestamp from the epoch, for thisSpan.0indicates current time should be used.unit- the unit of the timestamp
-
end
default void end(Instant timestamp)
Marks 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, useend().- Parameters:
timestamp- the explicit timestamp from the epoch, for thisSpan.0indicates current time should be used.
-
getSpanContext
SpanContext getSpanContext()
Returns theSpanContextassociated with thisSpan.- Returns:
- the
SpanContextassociated with thisSpan.
-
isRecording
boolean isRecording()
Returnstrueif thisSpanrecords tracing events (e.g.addEvent(String),setAttribute(String, long)).- Returns:
trueif thisSpanrecords tracing events.
-
storeInContext
default io.opentelemetry.context.Context storeInContext(io.opentelemetry.context.Context context)
- Specified by:
storeInContextin interfaceio.opentelemetry.context.ImplicitContextKeyed
-
-