|
15 | 15 | """ |
16 | 16 | API for propagation of context. |
17 | 17 |
|
| 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 | +
|
18 | 30 | Example:: |
19 | 31 |
|
20 | 32 | import flask |
@@ -54,14 +66,16 @@ def example_route(): |
54 | 66 | """ |
55 | 67 |
|
56 | 68 | import typing |
| 69 | +from logging import getLogger |
| 70 | + |
| 71 | +from pkg_resources import iter_entry_points |
57 | 72 |
|
58 | | -from opentelemetry.baggage.propagation import BaggagePropagator |
| 73 | +from opentelemetry.configuration import Configuration |
59 | 74 | from opentelemetry.context.context import Context |
60 | 75 | from opentelemetry.propagators import composite |
61 | 76 | from opentelemetry.trace.propagation import textmap |
62 | | -from opentelemetry.trace.propagation.tracecontext import ( |
63 | | - TraceContextTextMapPropagator, |
64 | | -) |
| 77 | + |
| 78 | +logger = getLogger(__name__) |
65 | 79 |
|
66 | 80 |
|
67 | 81 | def extract( |
@@ -104,9 +118,25 @@ def inject( |
104 | 118 | get_global_textmap().inject(set_in_carrier, carrier, context) |
105 | 119 |
|
106 | 120 |
|
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) |
110 | 140 |
|
111 | 141 |
|
112 | 142 | def get_global_textmap() -> textmap.TextMapPropagator: |
|
0 commit comments