opentelemetry.trace.span — OpenTelemetry Python documentation (original) (raw)
class opentelemetry.trace.span.Span[source]
Bases: 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 the timestamp argument is omitted.
Return type:
add_link(context, attributes=None)[source]
Adds a Link.
Adds a single Link with the SpanContext of the span to link to and, optionally, attributes passed as arguments. Implementations may ignore calls with an invalid span context if both attributes and TraceState are empty.
Note: It is preferred to add links at span creation, instead of calling this method later since samplers can only consider information already present during span creation.
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 set_status(status, description=None)[source]
Sets the Status of the Span. If used, this will override the default Span status.
Return type:
abstract record_exception(exception, attributes=None, timestamp=None, escaped=False)[source]
Records an exception as a span event.
Return type:
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
classmethod get_default()[source]
Return type:
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 - Tracestatespec, which describes additional restrictions on valid field values.
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.
Updates a key-value pair in tracestate. The provided pair should adhere to w3c tracestate identifiers format.
Parameters:
- key (str) – A valid tracestate key to update
- value (str) – A valid tracestate value to update for key
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.
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.
Creates a w3c tracestate header from a TraceState.
Return type:
Returns:
A string that adheres to the w3c tracestate header format.
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.
classmethod get_default()[source]
Return type:
Return type:
Return type:
Return type:
class opentelemetry.trace.span.SpanContext(trace_id: int, span_id: int, is_remote: bool, trace_flags: TraceFlags | None = 0, trace_state: TraceState | None = [])[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_: TraceFlags_
property trace_state_: TraceState_
class opentelemetry.trace.span.NonRecordingSpan(context)[source]
Bases: Span
The Span that is used when no Span implementation is available.
All operations are no-op except context propagation.
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.
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:
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 the timestamp argument is omitted.
Return type:
add_link(context, attributes=None)[source]
Adds a Link.
Adds a single Link with the SpanContext of the span to link to and, optionally, attributes passed as arguments. Implementations may ignore calls with an invalid span context if both attributes and TraceState are empty.
Note: It is preferred to add links at span creation, instead of calling this method later since samplers can only consider information already present during span creation.
Return type:
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:
set_status(status, description=None)[source]
Sets the Status of the Span. If used, this will override the default Span status.
Return type:
record_exception(exception, attributes=None, timestamp=None, escaped=False)[source]
Records an exception as a span event.
Return type:
opentelemetry.trace.span.format_trace_id(trace_id)[source]
Convenience trace ID formatting method :type trace_id: int:param trace_id: Trace ID int
Return type:
Returns:
The trace ID as 32-byte hexadecimal string
opentelemetry.trace.span.format_span_id(span_id)[source]
Convenience span ID formatting method :type span_id: int:param span_id: Span ID int
Return type:
Returns:
The span ID as 16-byte hexadecimal string