Skip to content

Commit 15e3b54

Browse files
committed
Add support for OTEL_PROPAGATORS
Fixes #1058
1 parent d8edd50 commit 15e3b54

5 files changed

Lines changed: 89 additions & 10 deletions

File tree

opentelemetry-api/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Add support for `OTEL_PROPAGATORS`
6+
([#1058](https://github.com/open-telemetry/opentelemetry-python/pull/1058))
57
- Store `int`s as `int`s in the global Configuration object
68
([#1118](https://github.com/open-telemetry/opentelemetry-python/pull/1118))
79

opentelemetry-api/setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ opentelemetry_meter_provider =
5555
default_meter_provider = opentelemetry.metrics:DefaultMeterProvider
5656
opentelemetry_tracer_provider =
5757
default_tracer_provider = opentelemetry.trace:DefaultTracerProvider
58+
opentelemetry_propagator =
59+
tracecontext = opentelemetry.trace.propagation.tracecontext:TraceContextTextMapPropagator
60+
baggage = opentelemetry.baggage.propagation:BaggagePropagator
5861

5962
[options.extras_require]
6063
test =

opentelemetry-api/src/opentelemetry/propagators/__init__.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515
"""
1616
API for propagation of context.
1717
18+
The propagators for the
19+
`opentelemetry.propagators.composite.CompositeHTTPPropagator` can be defined
20+
via configuration in the `OTEL_PROPAGATORS` environment variable. This variable
21+
should be set to a comma-separated string of names of values for the
22+
`opentelemetry_propagator` entry point. For example, setting `OTEL_PROPAGATORS`
23+
to `tracecontext,baggage` (which is the default value) would instantiate
24+
`opentelemetry.propagators.composite.CompositeHTTPPropagator` with 2
25+
propagators, one of type `opentelemetry.trace.propagation.tracecontext.TraceContextTextMapPropagator`
26+
and other of type `opentelemetry.baggage.propagation.BaggagePropagator`. Notice
27+
that these propagator classes are defined as `opentelemetry_propagator` entry
28+
points in the `setup.cfg` file of `opentelemetry`.
29+
1830
Example::
1931
2032
import flask
@@ -54,14 +66,16 @@ def example_route():
5466
"""
5567

5668
import typing
69+
from logging import getLogger
70+
71+
from pkg_resources import iter_entry_points
5772

58-
from opentelemetry.baggage.propagation import BaggagePropagator
73+
from opentelemetry.configuration import Configuration
5974
from opentelemetry.context.context import Context
6075
from opentelemetry.propagators import composite
6176
from opentelemetry.trace.propagation import textmap
62-
from opentelemetry.trace.propagation.tracecontext import (
63-
TraceContextTextMapPropagator,
64-
)
77+
78+
logger = getLogger(__name__)
6579

6680

6781
def extract(
@@ -104,9 +118,25 @@ def inject(
104118
get_global_textmap().inject(set_in_carrier, carrier, context)
105119

106120

107-
_HTTP_TEXT_FORMAT = composite.CompositeHTTPPropagator(
108-
[TraceContextTextMapPropagator(), BaggagePropagator()],
109-
) # type: textmap.TextMapPropagator
121+
try:
122+
123+
propagators = []
124+
125+
for propagator in (
126+
Configuration().get("PROPAGATORS", "tracecontext,baggage").split(",")
127+
):
128+
129+
propagators.append(
130+
next(
131+
iter_entry_points("opentelemetry_propagator", propagator)
132+
).load()()
133+
)
134+
135+
except Exception: # pylint: disable=broad-except
136+
logger.exception("Failed to load configured propagators")
137+
raise
138+
139+
_HTTP_TEXT_FORMAT = composite.CompositeHTTPPropagator(propagators)
110140

111141

112142
def get_global_textmap() -> textmap.TextMapPropagator:

opentelemetry-api/tests/configuration/test_configuration.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020

2121

2222
class TestConfiguration(TestCase):
23-
def tearDown(self) -> None:
24-
# This call resets the attributes of the Configuration class so that
25-
# each test is executed in the same conditions.
23+
24+
# These calls reset the attributes of the Configuration class so that each
25+
# test is executed in the same conditions.
26+
def setUp(self) -> None:
2627
Configuration._reset()
2728

2829
def test_singleton(self) -> None:
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from importlib import reload
16+
from unittest import TestCase
17+
from unittest.mock import patch
18+
19+
from opentelemetry.baggage.propagation import BaggagePropagator
20+
from opentelemetry.trace.propagation.tracecontext import (
21+
TraceContextTextMapPropagator,
22+
)
23+
24+
25+
class TestPropagators(TestCase):
26+
@patch("opentelemetry.propagators.composite.CompositeHTTPPropagator")
27+
def test_default_composite_propagators(self, mock_compositehttppropagator):
28+
def test_propagators(propagators):
29+
30+
propagators = {propagator.__class__ for propagator in propagators}
31+
32+
self.assertEqual(len(propagators), 2)
33+
self.assertEqual(
34+
propagators, {TraceContextTextMapPropagator, BaggagePropagator}
35+
)
36+
37+
mock_compositehttppropagator.configure_mock(
38+
**{"side_effect": test_propagators}
39+
)
40+
41+
import opentelemetry.propagators
42+
43+
reload(opentelemetry.propagators)

0 commit comments

Comments
 (0)