From 2c3f4e29980e3e0e97d70553444545d91a5a3b75 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Wed, 16 Sep 2020 11:03:34 -0600 Subject: [PATCH] Add support for OTEL_PROPAGATORS Fixes #1058 --- opentelemetry-api/setup.cfg | 3 ++ .../src/opentelemetry/propagators/__init__.py | 31 ++++++++++++++----- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/opentelemetry-api/setup.cfg b/opentelemetry-api/setup.cfg index 9435d92f197..4ad44c64519 100644 --- a/opentelemetry-api/setup.cfg +++ b/opentelemetry-api/setup.cfg @@ -55,6 +55,9 @@ opentelemetry_meter_provider = default_meter_provider = opentelemetry.metrics:DefaultMeterProvider opentelemetry_tracer_provider = default_tracer_provider = opentelemetry.trace:DefaultTracerProvider +opentelemetry_propagator = + tracecontext = opentelemetry.trace.propagation.tracecontext:TraceContextTextMapPropagator + baggage = opentelemetry.baggage.propagation:BaggagePropagator [options.extras_require] test = diff --git a/opentelemetry-api/src/opentelemetry/propagators/__init__.py b/opentelemetry-api/src/opentelemetry/propagators/__init__.py index f34e3c588b4..5f8e9a26e69 100644 --- a/opentelemetry-api/src/opentelemetry/propagators/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagators/__init__.py @@ -53,15 +53,16 @@ def example_route(): https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/api-propagators.md """ +from pkg_resources import iter_entry_points +from logging import getLogger import typing -from opentelemetry.baggage.propagation import BaggagePropagator from opentelemetry.context.context import Context from opentelemetry.propagators import composite from opentelemetry.trace.propagation import textmap -from opentelemetry.trace.propagation.tracecontext import ( - TraceContextTextMapPropagator, -) +from opentelemetry.configuration import Configuration + +logger = getLogger(__name__) def extract( @@ -104,9 +105,25 @@ def inject( get_global_textmap().inject(set_in_carrier, carrier, context) -_HTTP_TEXT_FORMAT = composite.CompositeHTTPPropagator( - [TraceContextTextMapPropagator(), BaggagePropagator()], -) # type: textmap.TextMapPropagator +try: + + propagators = [] + + for propagator in Configuration().get( + "PROPAGATORS", "tracecontext,baggage" + ).split(","): + + propagators.append( + next( + iter_entry_points("opentelemetry_propagator", propagator) + ).load()() + ) + +except Exception: # pylint: disable=broad-except + logger.exception("Failed to load configured propagators") + raise + +_HTTP_TEXT_FORMAT = composite.CompositeHTTPPropagator(propagators) def get_global_textmap() -> textmap.TextMapPropagator: