OpenTelemetryScope Class

Base class for OpenTelemetry tracing scopes in the SDK.

Initialize the OpenTelemetry scope.

Constructor

OpenTelemetryScope(operation_name: str, activity_name: str, agent_details: AgentDetails | None = None, span_details: SpanDetails | None = None)

Parameters

Name Description
operation_name
Required

The name of the operation being traced

activity_name
Required

The name of the activity for display purposes

agent_details

Optional agent details

Default value: None
span_details

Optional span configuration including parent context, start/end times, span kind, and span links. Subclasses may override span_details.span_kind before calling this constructor; defaults to SpanKind.CLIENT.

Default value: None

Methods

dispose

Dispose the scope and finalize telemetry data collection.

get_context

Get the OpenTelemetry context for this scope's span.

This method returns a Context object containing this scope's span, which can be used to propagate trace context to child operations or downstream services.

inject_context_to_headers

Inject this span's trace context into W3C HTTP headers.

Returns a dictionary of headers containing traceparent and optionally tracestate that can be forwarded to downstream services or stored for later context propagation.

record_attributes

Record multiple attribute key/value pairs for telemetry tracking.

This method allows setting multiple custom attributes on the span at once.

record_cancellation

Record task cancellation.

record_error

Record an error in the span.

record_response

Record an response in the span.

set_end_time

Set a custom end time for the scope.

When set, dispose() will pass this value to span.end() instead of using the current wall-clock time. This is useful when the actual end time of the operation is known before the scope is disposed.

set_tag_maybe

Set a tag on the span if the value is not None.

dispose

Dispose the scope and finalize telemetry data collection.

dispose() -> None

get_context

Get the OpenTelemetry context for this scope's span.

This method returns a Context object containing this scope's span, which can be used to propagate trace context to child operations or downstream services.

get_context() -> Context | None

Returns

Type Description

A Context containing this scope's span, or None if telemetry is disabled or no span exists.

inject_context_to_headers

Inject this span's trace context into W3C HTTP headers.

Returns a dictionary of headers containing traceparent and optionally tracestate that can be forwarded to downstream services or stored for later context propagation.

inject_context_to_headers() -> dict[str, str]

Returns

Type Description

A dictionary containing W3C trace context headers. Returns an empty dictionary if telemetry is disabled or no span exists.

record_attributes

Record multiple attribute key/value pairs for telemetry tracking.

This method allows setting multiple custom attributes on the span at once.

record_attributes(attributes: dict[str, Any] | list[tuple[str, Any]]) -> None

Parameters

Name Description
attributes
Required

Dictionary or list of tuples containing attribute key-value pairs. Keys that are None or empty will be skipped.

record_cancellation

Record task cancellation.

record_cancellation() -> None

record_error

Record an error in the span.

record_error(exception: Exception) -> None

Parameters

Name Description
exception
Required

The exception that occurred

record_response

Record an response in the span.

record_response(response: str) -> None

Parameters

Name Description
response
Required

The response content to record

set_end_time

Set a custom end time for the scope.

When set, dispose() will pass this value to span.end() instead of using the current wall-clock time. This is useful when the actual end time of the operation is known before the scope is disposed.

set_end_time(end_time: datetime) -> None

Parameters

Name Description
end_time
Required

The end time as a datetime object.

set_tag_maybe

Set a tag on the span if the value is not None.

set_tag_maybe(name: str, value: Any) -> None

Parameters

Name Description
name
Required

The name of the tag

value
Required

The value to set (will be skipped if None)