opentelemetry.propagate package

Module contents

API for propagation of context.

The propagators for the opentelemetry.propagators.composite.CompositePropagator can be defined via configuration in the OTEL_PROPAGATORS environment variable. This variable should be set to a comma-separated string of names of values for the opentelemetry_propagator entry point. For example, setting OTEL_PROPAGATORS to tracecontext,baggage (which is the default value) would instantiate opentelemetry.propagators.composite.CompositePropagator with 2 propagators, one of type opentelemetry.trace.propagation.tracecontext.TraceContextTextMapPropagator and other of type opentelemetry.baggage.propagation.W3CBaggagePropagator. Notice that these propagator classes are defined as opentelemetry_propagator entry points in the pyproject.toml file of opentelemetry.

Example:

import flask
import requests
from opentelemetry import propagate


PROPAGATOR = propagate.get_global_textmap()


def get_header_from_flask_request(request, key):
    return request.headers.get_all(key)

def set_header_into_requests_request(request: requests.Request,
                                        key: str, value: str):
    request.headers[key] = value

def example_route():
    context = PROPAGATOR.extract(
        get_header_from_flask_request,
        flask.request
    )
    request_to_downstream = requests.Request(
        "GET", "http://httpbin.org/get"
    )
    PROPAGATOR.inject(
        set_header_into_requests_request,
        request_to_downstream,
        context=context
    )
    session = requests.Session()
    session.send(request_to_downstream.prepare())
opentelemetry.propagate.extract(carrier, context=None, getter=<opentelemetry.propagators.textmap.DefaultGetter object>)[source]

Uses the configured propagator to extract a Context from the carrier.

Parameters:
  • getter (Getter[TypeVar(CarrierT)]) – an object which contains a get function that can retrieve zero or more values from the carrier and a keys function that can get all the keys from carrier.

  • carrier (TypeVar(CarrierT)) – and object which contains values that are used to construct a Context. This object must be paired with an appropriate getter which understands how to extract a value from it.

  • context (Optional[Context]) – an optional Context to use. Defaults to root context if not set.

Return type:

Context

opentelemetry.propagate.inject(carrier, context=None, setter=<opentelemetry.propagators.textmap.DefaultSetter object>)[source]

Uses the configured propagator to inject a Context into the carrier.

Parameters:
  • carrier (TypeVar(CarrierT)) – the medium used by Propagators to read values from and write values to. Should be paired with setter, which should know how to set header values on the carrier.

  • context (Optional[Context]) – An optional Context to use. Defaults to current context if not set.

  • setter (Setter[TypeVar(CarrierT)]) – An optional Setter object that can set values on the carrier.

Return type:

None

opentelemetry.propagate.get_global_textmap()[source]
Return type:

TextMapPropagator

opentelemetry.propagate.set_global_textmap(http_text_format)[source]
Return type:

None