opentelemetry.trace.span¶
-
class
opentelemetry.trace.span.
Span
[source]¶ Bases:
abc.ABC
A span represents a single operation within a trace.
-
abstract
end
(end_time=None)[source]¶ Sets the current time as the span’s end time.
The span’s end time is the wall time at which the operation finished.
Only the first call to
end
should modify the span, and implementations are free to ignore or raise on further calls.- Return type
-
abstract
get_span_context
()[source]¶ Gets the span’s SpanContext.
Get an immutable, serializable identifier for this span that can be used to create new child spans.
- Return type
- Returns
A
opentelemetry.trace.SpanContext
with a copy of this span’s immutable state.
-
abstract
set_attributes
(attributes)[source]¶ Sets Attributes.
Sets Attributes with the key and value passed as arguments dict.
Note: The behavior of
None
value attributes is undefined, and hence strongly discouraged. It is also preferred to set attributes at span creation, instead of calling this method later since samplers can only consider information already present during span creation.- Return type
-
abstract
set_attribute
(key, value)[source]¶ Sets an Attribute.
Sets a single Attribute with the key and value passed as arguments.
Note: The behavior of
None
value attributes is undefined, and hence strongly discouraged. It is also preferred to set attributes at span creation, instead of calling this method later since samplers can only consider information already present during span creation.- Return type
-
abstract
add_event
(name, attributes=None, timestamp=None)[source]¶ Adds an
Event
.Adds a single
Event
with the name and, optionally, a timestamp and attributes passed as arguments. Implementations should generate a timestamp if thetimestamp
argument is omitted.- Return type
-
abstract
update_name
(name)[source]¶ Updates the
Span
name.This will override the name provided via
opentelemetry.trace.Tracer.start_span()
.Upon this update, any sampling behavior based on Span name will depend on the implementation.
- Return type
-
abstract
is_recording
()[source]¶ Returns whether this span will be recorded.
Returns true if this Span is active and recording information like events with the add_event operation and attributes using set_attribute.
- Return type
-
abstract
-
class
opentelemetry.trace.span.
TraceFlags
[source]¶ Bases:
int
A bitmask that represents options specific to the trace.
The only supported option is the “sampled” flag (
0x01
). If set, this flag indicates that the trace may have been sampled upstream.See the W3C Trace Context - Traceparent spec for details.
-
DEFAULT
= 0¶
-
SAMPLED
= 1¶
-
-
class
opentelemetry.trace.span.
TraceState
(entries=None)[source]¶ -
A list of key-value pairs representing vendor-specific trace info.
Keys and values are strings of up to 256 printable US-ASCII characters. Implementations should conform to the W3C Trace Context - Tracestate spec, which describes additional restrictions on valid field values.
-
add
(key, value)[source]¶ Adds a key-value pair to tracestate. The provided pair should adhere to w3c tracestate identifiers format.
- Parameters
- Return type
- Returns
A new TraceState with the modifications applied.
If the provided key-value pair is invalid or results in tracestate that violates tracecontext specification, they are discarded and same tracestate will be returned.
-
update
(key, value)[source]¶ Updates a key-value pair in tracestate. The provided pair should adhere to w3c tracestate identifiers format.
- Parameters
- Return type
- Returns
A new TraceState with the modifications applied.
If the provided key-value pair is invalid or results in tracestate that violates tracecontext specification, they are discarded and same tracestate will be returned.
-
delete
(key)[source]¶ Deletes a key-value from tracestate.
- Parameters
key (
str
) – A valid tracestate key to remove key-value pair from tracestate- Return type
- Returns
A new TraceState with the modifications applied.
If the provided key-value pair is invalid or results in tracestate that violates tracecontext specification, they are discarded and same tracestate will be returned.
-
to_header
()[source]¶ Creates a w3c tracestate header from a TraceState.
- Return type
- Returns
A string that adheres to the w3c tracestate header format.
-
classmethod
from_header
(header_list)[source]¶ Parses one or more w3c tracestate header into a TraceState.
- Parameters
header_list (
List
[str
]) – one or more w3c tracestate headers.- Return type
- Returns
A valid TraceState that contains values extracted from the tracestate header.
If the format of one headers is illegal, all values will be discarded and an empty tracestate will be returned.
If the number of keys is beyond the maximum, all values will be discarded and an empty tracestate will be returned.
-
-
class
opentelemetry.trace.span.
SpanContext
(trace_id: int, span_id: int, is_remote: bool, trace_flags: Optional[opentelemetry.trace.span.TraceFlags] = 0, trace_state: Optional[opentelemetry.trace.span.TraceState] = [])[source]¶ Bases:
Tuple
[int
,int
,bool
,TraceFlags
,TraceState
,bool
]The state of a Span to propagate between processes.
This class includes the immutable attributes of a
Span
that must be propagated to a span’s children and across process boundaries.- Parameters
trace_id – The ID of the trace that this span belongs to.
span_id – This span’s ID.
is_remote – True if propagated from a remote parent.
trace_flags – Trace options to propagate.
trace_state – Tracing-system-specific info to propagate.
-
property
trace_flags
¶ - Return type
-
property
trace_state
¶ - Return type
-
class
opentelemetry.trace.span.
NonRecordingSpan
(context)[source]¶ Bases:
opentelemetry.trace.span.Span
The Span that is used when no Span implementation is available.
All operations are no-op except context propagation.
-
get_span_context
()[source]¶ Gets the span’s SpanContext.
Get an immutable, serializable identifier for this span that can be used to create new child spans.
- Return type
- Returns
A
opentelemetry.trace.SpanContext
with a copy of this span’s immutable state.
-
is_recording
()[source]¶ Returns whether this span will be recorded.
Returns true if this Span is active and recording information like events with the add_event operation and attributes using set_attribute.
- Return type
-
end
(end_time=None)[source]¶ Sets the current time as the span’s end time.
The span’s end time is the wall time at which the operation finished.
Only the first call to
end
should modify the span, and implementations are free to ignore or raise on further calls.- Return type
-
set_attributes
(attributes)[source]¶ Sets Attributes.
Sets Attributes with the key and value passed as arguments dict.
Note: The behavior of
None
value attributes is undefined, and hence strongly discouraged. It is also preferred to set attributes at span creation, instead of calling this method later since samplers can only consider information already present during span creation.- Return type
-
set_attribute
(key, value)[source]¶ Sets an Attribute.
Sets a single Attribute with the key and value passed as arguments.
Note: The behavior of
None
value attributes is undefined, and hence strongly discouraged. It is also preferred to set attributes at span creation, instead of calling this method later since samplers can only consider information already present during span creation.- Return type
-
add_event
(name, attributes=None, timestamp=None)[source]¶ Adds an
Event
.Adds a single
Event
with the name and, optionally, a timestamp and attributes passed as arguments. Implementations should generate a timestamp if thetimestamp
argument is omitted.- Return type
-
update_name
(name)[source]¶ Updates the
Span
name.This will override the name provided via
opentelemetry.trace.Tracer.start_span()
.Upon this update, any sampling behavior based on Span name will depend on the implementation.
- Return type
-