azure.core.tracing

class azure.core.tracing.AbstractSpan(span: SpanType | None = None, name: str | None = None, **kwargs: Any)[source]

Wraps a span from a distributed tracing implementation.

If a span is given wraps the span. Else a new span is created. The optional argument name is given to the new span.

Parameters:
  • span (Any) – The span to wrap

  • name (str) – The name of the span

classmethod change_context(span: SpanType) ContextManager[SpanType, bool | None][source]

Change the context for the life of this context manager.

Parameters:

span (Any) – The span to run in the new context

Return type:

contextmanager

Returns:

A context manager that will run the given span in the new context

classmethod get_current_span() SpanType[source]

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

Returns:

The current span

Return type:

AbstractSpan

classmethod get_current_tracer() Any[source]

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

Returns:

The current tracer

Return type:

Any

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

Parameters:
  • traceparent (str) – A string representing a traceparent

  • attributes (dict) – Any additional attributes that should be added to link

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

Parameters:
  • headers (dict) – A dictionary of the request header as key value pairs.

  • attributes (dict) – Any additional attributes that should be added to link

classmethod set_current_span(span: SpanType) None[source]

Set the given span as the current span in the execution context.

Parameters:

span (Any) – The span to set as the current span

classmethod set_current_tracer(tracer: Any) None[source]

Set the given tracer as the current tracer in the execution context.

Parameters:

tracer (Any) – The tracer to set as the current tracer

classmethod with_current_context(func: Callable) Callable[source]

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

Parameters:

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

Returns:

The target the pass in instead of the function

Return type:

callable

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

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

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

  • value (Union[str, int]) – The value of the key value pair

finish() None[source]

Set the end time for a span.

get_trace_parent() str[source]

Return traceparent string.

Returns:

a traceparent string

Return type:

str

set_http_attributes(request: HttpRequest | HttpRequest, response: HttpResponse | AsyncHttpResponse | HttpResponse | AsyncHttpResponse | None = None) None[source]

Add correct attributes for a http client span.

Parameters:
span(name: str = 'child_span', **kwargs: Any) AbstractSpan[SpanType][source]

Create a child span for the current span and append it to the child spans list. The child span must be wrapped by an implementation of AbstractSpan

Parameters:

name (str) – The name of the child span

Returns:

The child span

Return type:

AbstractSpan

start() None[source]

Set the start time for a span.

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

Returns a dictionary with the header labels and values.

Returns:

A dictionary with the header labels and values

Return type:

dict

property kind: SpanKind | None

Get the span kind of this span.

Return type:

SpanKind

Returns:

The span kind of this span

property span_instance: SpanType

Returns the span the class is wrapping.

class azure.core.tracing.HttpSpanMixin[source]

Can be used to get HTTP span attributes settings for free.

set_http_attributes(request: HttpRequest | HttpRequest, response: HttpResponse | AsyncHttpResponse | HttpResponse | AsyncHttpResponse | None = None) None[source]

Add correct attributes for a http client span.

Parameters:

Represents a reference from one span to another span.

Parameters:
  • headers (dict) – A dictionary of the request header as key value pairs.

  • attributes (dict) – Any additional attributes that should be added to link

class azure.core.tracing.SpanKind(*values)[source]

Describes the role or kind of a span within a distributed trace.

This helps to categorize spans based on their relationship to other spans and the type of operation they represent.

CLIENT = 3

Indicates that the span describes a request to some remote service.

CONSUMER = 5

Indicates that the span represents the processing of an operation initiated by a producer.

INTERNAL = 6

Indicates that the span is used internally in the application.

PRODUCER = 4

Indicates that the span describes the initiation or scheduling of a local or remote operation.

SERVER = 2

Indicates that the span describes an operation that handles a remote request.

UNSPECIFIED = 1

Unspecified span kind.

class azure.core.tracing.TracingOptions[source]

Options to configure tracing behavior for operations.

clear()

Remove all items from the dict.

copy()

Return a shallow copy of the dict.

classmethod fromkeys(iterable, value=None, /)

Create a new dictionary with keys from iterable and values set to value.

get(key, default=None, /)

Return the value for key if key is in the dictionary, else default.

items()

Return a set-like object providing a view on the dict’s items.

keys()

Return a set-like object providing a view on the dict’s keys.

pop(k[, d]) v, remove specified key and return the corresponding value.

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) None.  Update D from mapping/iterable E and F.

If E is present and has a .keys() method, then does: for k in E.keys(): D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values()

Return an object providing a view on the dict’s values.

attributes: Mapping[str, str | bool | int | float | Sequence[str] | Sequence[bool] | Sequence[int] | Sequence[float]]

Attributes to include in the spans emitted for the operation.

enabled: bool

Whether tracing is enabled for the operation. By default, if the global setting is enabled, tracing is enabled for all operations. This option can be used to override the global setting for a specific operation.

Submodules

azure.core.tracing.common

Common functions shared by both the sync and the async decorators.

azure.core.tracing.common.change_context(span: AbstractSpan | None) Generator[source]

Execute this block inside the given context and restore it afterwards.

This does not start and ends the span, but just make sure all code is executed within that span.

If span is None, no-op.

Parameters:

span (AbstractSpan) – A span

Return type:

contextmanager

Returns:

A context manager that will run the given span in the new context

azure.core.tracing.common.with_current_context(func: Callable) Any[source]

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

Parameters:

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

Returns:

The func wrapped with correct context

Return type:

callable

azure.core.tracing.decorator

The decorator to apply if you want the given function traced.

azure.core.tracing.decorator.distributed_trace(__func: Callable[[P], T]) Callable[[P], T][source]
azure.core.tracing.decorator.distributed_trace(*, name_of_span: str | None = None, kind: 'SpanKind' | None = None, tracing_attributes: Mapping[str, Any] | None = None, **kwargs: Any) Callable[[Callable[[P], T]], Callable[[P], T]]

Decorator to apply to function to get traced automatically.

Span will use the func name or “name_of_span”.

Note:

This decorator SHOULD NOT be used by application developers. It’s intended to be called by Azure client libraries only.

Application developers should use OpenTelemetry or other tracing libraries to instrument their applications.

Parameters:

__func (callable) – A function to decorate

Keyword Arguments:
  • name_of_span (str) – The span name to replace func name if necessary

  • kind (SpanKind) – The kind of the span. INTERNAL by default.

  • tracing_attributes (Mapping[str, Any] or None) – Attributes to add to the span.

Returns:

The decorated function

Return type:

Any

azure.core.tracing.decorator_async

The decorator to apply if you want the given function traced.

azure.core.tracing.decorator_async.distributed_trace_async(__func: Callable[[P], Awaitable[T]]) Callable[[P], Awaitable[T]][source]
azure.core.tracing.decorator_async.distributed_trace_async(*, name_of_span: str | None = None, kind: 'SpanKind' | None = None, tracing_attributes: Mapping[str, Any] | None = None, **kwargs: Any) Callable[[Callable[[P], Awaitable[T]]], Callable[[P], Awaitable[T]]]

Decorator to apply to function to get traced automatically.

Span will use the func name or “name_of_span”.

Note:

This decorator SHOULD NOT be used by application developers. It’s intended to be called by Azure client libraries only.

Application developers should use OpenTelemetry or other tracing libraries to instrument their applications.

Parameters:

__func (callable) – A function to decorate

Keyword Arguments:
  • name_of_span (str) – The span name to replace func name if necessary

  • kind (SpanKind) – The kind of the span. INTERNAL by default.

  • tracing_attributes (Mapping[str, Any] or None) – Attributes to add to the span.

Returns:

The decorated function

Return type:

Any