azure.core.tracing.ext.opentelemetry_span package

Implements azure.core.tracing.AbstractSpan to wrap OpenTelemetry spans.

class azure.core.tracing.ext.opentelemetry_span.OpenTelemetrySpan(span: Optional[Span] = None, name: Optional[str] = 'span', **kwargs: Any)[source]

OpenTelemetry plugin for Azure client libraries.

  • span (Span) – The OpenTelemetry span to wrap, or nothing to create a new one.

  • name (str) – The name of the OpenTelemetry span to create if a new span is needed

Keyword Arguments
  • kind (SpanKind) – The span kind of this span.

  • links (list[Link]) – The list of links to be added to the span.

add_attribute(key: str, value: Union[str, int])None[source]

Add attribute (key value pair) to the current span.

  • key (str) – The key of the key value pair

  • value (str) – The value of the key value pair

classmethod change_context(span: opentelemetry.trace.span.Span) → ContextManager[source]

Change the context for the life of this context manager.


Set the end time for a span.

classmethod get_current_span() → opentelemetry.trace.span.Span[source]

Get the current span from the execution context. Return None otherwise.

classmethod get_current_tracer() → opentelemetry.trace.Tracer[source]

Get the current tracer from the execution context. Return None otherwise.


Return traceparent string as defined in W3C trace context specification.

Example: Value = 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01 base16(version) = 00 base16(trace-id) = 4bf92f3577b34da6a3ce929d0e0e4736 base16(parent-id) = 00f067aa0ba902b7 base16(trace-flags) = 01 // sampled


a traceparent string

Return type


Links the context to the current tracer.


traceparent (str) – A complete traceparent

Given a dictionary, extracts the context and links the context to the current tracer.


headers (dict) – A key value pair dictionary

classmethod set_current_span(span: opentelemetry.trace.span.Span)None[source]

Not supported by OpenTelemetry.

classmethod set_current_tracer(_: opentelemetry.trace.Tracer)None[source]

Set the given tracer as the current tracer in the execution context. :param tracer: The tracer to set the current tracer as :type tracer: :class: OpenTelemetry.trace.Tracer

set_http_attributes(request: HttpRequest, response: Optional[HttpResponseType] = None)None

Add correct attributes for a http client span.

  • request (HttpRequest) – The request made

  • response (HttpResponse or AsyncHttpResponse) – The response received by the server. Is None if no response received.

span(name: Optional[str] = 'span', **kwargs: Any) → OpenTelemetrySpan[source]

Create a child span for the current span and append it to the child spans list in the span instance. :param name: Name of the child span :type name: str :keyword SpanKind kind: The span kind of this span. :keyword links: The list of links to be added to the span. :paramtype links: list[Link] :return: The OpenTelemetrySpan that is wrapping the child span instance

to_header() → Dict[str, str][source]

Returns a dictionary with the header labels and values. :return: A key value pair dictionary

classmethod with_current_context(func: Callable) → Callable[source]

Passes the current spans to the new context the function will be run in.


func – The function that will be run in the new context


The target the pass in instead of the function

property kind

Get the span kind of this span.

property span_instance

The OpenTelemetry span that is being wrapped.