@Immutable public final class DefaultSpan extends java.lang.Object implements Span
DefaultSpan
is the default Span
that is used when no Span
implementation is available. All operations are no-op except context propagation.
Used also to stop tracing, see Tracer.withSpan(io.opentelemetry.trace.Span)
.
Span.Builder, Span.Kind
Modifier and Type | Method and Description |
---|---|
void |
addEvent(java.lang.String name)
Adds an event to the
Span . |
void |
addEvent(java.lang.String name,
Attributes attributes)
Adds an event to the
Span with the given Attributes . |
void |
addEvent(java.lang.String name,
Attributes attributes,
long timestamp)
|
void |
addEvent(java.lang.String name,
long timestamp)
Adds an event to the
Span with the given timestamp , as nanos since epoch. |
static Span |
create(SpanContext spanContext)
Creates an instance of this class with the
SpanContext . |
void |
end()
Marks the end of
Span execution. |
void |
end(EndSpanOptions endOptions)
Marks the end of
Span execution with the specified EndSpanOptions . |
SpanContext |
getContext()
Returns the
SpanContext associated with this Span . |
static Span |
getInvalid()
Returns a
DefaultSpan with an invalid SpanContext . |
boolean |
isRecording()
Returns
true if this Span records tracing events (e.g. |
void |
recordException(java.lang.Throwable exception)
Records information about the
Throwable to the Span . |
void |
recordException(java.lang.Throwable exception,
Attributes additionalAttributes)
Records information about the
Throwable to the Span . |
<T> void |
setAttribute(AttributeKey<T> key,
T value)
Sets an attribute to the
Span . |
void |
setAttribute(java.lang.String key,
boolean value)
Sets an attribute to the
Span . |
void |
setAttribute(java.lang.String key,
double value)
Sets an attribute to the
Span . |
void |
setAttribute(java.lang.String key,
long value)
Sets an attribute to the
Span . |
void |
setAttribute(java.lang.String key,
java.lang.String value)
Sets an attribute to the
Span . |
void |
setStatus(StatusCanonicalCode canonicalCode)
Sets the status to the
Span . |
void |
setStatus(StatusCanonicalCode canonicalCode,
java.lang.String description)
Sets the status to the
Span . |
java.lang.String |
toString() |
void |
updateName(java.lang.String name)
Updates the
Span name. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
setAttribute
public static Span getInvalid()
DefaultSpan
with an invalid SpanContext
.DefaultSpan
with an invalid SpanContext
.public static Span create(SpanContext spanContext)
SpanContext
.spanContext
- the SpanContext
.DefaultSpan
.public void setAttribute(java.lang.String key, java.lang.String value)
Span
Span
. If the Span
previously contained a mapping for
the key, the old value is replaced by the specified value.
If a null or empty String value
is passed in, the behavior is undefined, and hence
strongly discouraged.
Note: It is strongly recommended to use Span.setAttribute(AttributeKey, Object)
, and
pre-allocate your keys, if possible.
setAttribute
in interface Span
key
- the key for this attribute.value
- the value for this attribute.public void setAttribute(java.lang.String key, long value)
Span
Span
. If the Span
previously contained a mapping for
the key, the old value is replaced by the specified value.
Note: It is strongly recommended to use Span.setAttribute(AttributeKey, Object)
, and
pre-allocate your keys, if possible.
setAttribute
in interface Span
key
- the key for this attribute.value
- the value for this attribute.public void setAttribute(java.lang.String key, double value)
Span
Span
. If the Span
previously contained a mapping for
the key, the old value is replaced by the specified value.
Note: It is strongly recommended to use Span.setAttribute(AttributeKey, Object)
, and
pre-allocate your keys, if possible.
setAttribute
in interface Span
key
- the key for this attribute.value
- the value for this attribute.public void setAttribute(java.lang.String key, boolean value)
Span
Span
. If the Span
previously contained a mapping for
the key, the old value is replaced by the specified value.
Note: It is strongly recommended to use Span.setAttribute(AttributeKey, Object)
, and
pre-allocate your keys, if possible.
setAttribute
in interface Span
key
- the key for this attribute.value
- the value for this attribute.public <T> void setAttribute(AttributeKey<T> key, T value)
Span
Span
. If the Span
previously 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.
setAttribute
in interface Span
key
- the key for this attribute.value
- the value for this attribute.public void addEvent(java.lang.String name)
Span
Span
. The timestamp of the event will be the current time.public void addEvent(java.lang.String name, long timestamp)
Span
Span
with the given timestamp
, as nanos since epoch. Note,
this timestamp
is not the same as System.nanoTime()
but may be computed using
it, for example, by taking a difference of readings from System.nanoTime()
and adding
to the span start time.
When possible, it is preferred to use Span.addEvent(String)
at the time the event
occurred.
public void addEvent(java.lang.String name, Attributes attributes)
Span
Span
with the given Attributes
. The timestamp of the event
will be the current time.public void addEvent(java.lang.String name, Attributes attributes, long timestamp)
Span
Span
with the given Attributes
and timestamp
.
Note, this timestamp
is not the same as System.nanoTime()
but may be computed
using it, for example, by taking a difference of readings from System.nanoTime()
and
adding to the span start time.
When possible, it is preferred to use Span.addEvent(String)
at the time the event
occurred.
public void setStatus(StatusCanonicalCode canonicalCode)
Span
Span
.
If used, this will override the default Span
status. Default status code is StatusCanonicalCode.UNSET
.
Only the value of the last call will be recorded, and implementations are free to ignore previous calls.
setStatus
in interface Span
canonicalCode
- the StatusCanonicalCode
to set.public void setStatus(StatusCanonicalCode canonicalCode, java.lang.String description)
Span
Span
.
If used, this will override the default Span
status. Default status code is StatusCanonicalCode.UNSET
.
Only the value of the last call will be recorded, and implementations are free to ignore previous calls.
setStatus
in interface Span
canonicalCode
- the StatusCanonicalCode
to set.description
- the description of the Status
.public void recordException(java.lang.Throwable exception)
Span
Throwable
to the Span
.
Note that SemanticAttributes.EXCEPTION_ESCAPED
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.
recordException
in interface Span
exception
- the Throwable
to record.public void recordException(java.lang.Throwable exception, Attributes additionalAttributes)
Span
Throwable
to the Span
.recordException
in interface Span
exception
- the Throwable
to record.additionalAttributes
- the additional Attributes
to record.public void updateName(java.lang.String name)
Span
Span
name.
If used, this will override the name provided via Span.Builder
.
Upon this update, any sampling behavior based on Span
name will depend on the
implementation.
updateName
in interface Span
name
- the Span
name.public void end()
Span
Span
execution.
Only the timing of the first end call for a given Span
will be recorded, and
implementations are free to ignore all further calls.
public void end(EndSpanOptions endOptions)
Span
Span
execution with the specified EndSpanOptions
.
Only the timing of the first end call for a given Span
will 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, use Span.end()
.
end
in interface Span
endOptions
- the explicit EndSpanOptions
for this Span
.public SpanContext getContext()
Span
SpanContext
associated with this Span
.getContext
in interface Span
SpanContext
associated with this Span
.public boolean isRecording()
Span
true
if this Span
records tracing events (e.g. Span.addEvent(String)
, Span.setAttribute(String, long)
).isRecording
in interface Span
true
if this Span
records tracing events.public java.lang.String toString()
toString
in class java.lang.Object