|
| 1 | +# Copyright 2019, 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 | +import abc |
| 15 | +from opentelemetry import loader |
| 16 | +import opentelemetry.propagator.httptextformat as httptextformat |
| 17 | +import opentelemetry.propagator.binaryformat as binaryformat |
| 18 | +from opentelemetry.context import BaseRuntimeContext, Context |
| 19 | + |
| 20 | + |
| 21 | +class Propagator(abc.ABC): |
| 22 | + """Class which encapsulates propagation of values to and from context. |
| 23 | +
|
| 24 | + In contract to using the formatters directly, a propagator object can |
| 25 | + help own configuration around which formatters to use, as well as |
| 26 | + help simplify the work require for integrations to use the intended |
| 27 | + formatters. |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + context: BaseRuntimeContext, |
| 33 | + http_format: httptextformat.HTTPTextFormat, |
| 34 | + binary_format: binaryformat.BinaryFormat, |
| 35 | + ): |
| 36 | + self._context = context |
| 37 | + self._http_format = http_format |
| 38 | + self._binary_format = binary_format |
| 39 | + |
| 40 | + @classmethod |
| 41 | + def create( |
| 42 | + cls, |
| 43 | + http_format: httptextformat.HTTPTextFormat, |
| 44 | + binary_format: binaryformat.BinaryFormat, |
| 45 | + ) -> "Propagator": |
| 46 | + """Create a propagator with the current context.""" |
| 47 | + return Propagator(Context, http_format, binary_format) |
| 48 | + |
| 49 | + @abc.abstractmethod |
| 50 | + def extract( |
| 51 | + self, get_from_carrier: httptextformat.Getter, carrier: object |
| 52 | + ): |
| 53 | + """Extract context data from the carrier, add to the context. |
| 54 | +
|
| 55 | + Using the http_format specified in the constructor, extract the |
| 56 | + data form the carrier passed and add values into the context object. |
| 57 | +
|
| 58 | + Args: |
| 59 | + get_from_carrier: a function that can retrieve zero |
| 60 | + or more values from the carrier. In the case that |
| 61 | + the value does not exist, return an empty list. |
| 62 | + carrier: and object which contains values that are |
| 63 | + used to construct a SpanContext. This object |
| 64 | + must be paired with an appropriate get_from_carrier |
| 65 | + which understands how to extract a value from it. |
| 66 | + """ |
| 67 | + |
| 68 | + @abc.abstractmethod |
| 69 | + def inject( |
| 70 | + self, set_in_carrier: httptextformat.Setter, carrier: object |
| 71 | + ) -> None: |
| 72 | + """Inject values from context into a carrier. |
| 73 | +
|
| 74 | + inject enables the propagation of values into HTTP clients or |
| 75 | + other objects which perform an HTTP request. Using the |
| 76 | + httptextformat configured, inject the context data into |
| 77 | + the carrier with the set_in_carrier method passed. |
| 78 | +
|
| 79 | + Args: |
| 80 | + set_in_carrier: A setter function that can set values |
| 81 | + on the carrier. |
| 82 | + carrier: An object that a place to define HTTP headers. |
| 83 | + Should be paired with set_in_carrier, which should |
| 84 | + know how to set header values on the carrier. |
| 85 | + """ |
| 86 | + |
| 87 | + @abc.abstractmethod |
| 88 | + def from_bytes(self, byte_representation: bytes) -> None: |
| 89 | + """Populate context with data that existed in the byte representation. |
| 90 | +
|
| 91 | + Using the configured binary_format, extract values from the bytes object |
| 92 | + passed into the context object configured. |
| 93 | +
|
| 94 | + Args: |
| 95 | + byte_representation: the bytes to deserialize. |
| 96 | + """ |
| 97 | + |
| 98 | + @abc.abstractmethod |
| 99 | + def to_bytes(self) -> bytes: |
| 100 | + """Creates a byte representation of the context configured. |
| 101 | +
|
| 102 | + to_bytes should read values from the configured context and |
| 103 | + return a bytes object to represent it. |
| 104 | +
|
| 105 | + Returns: |
| 106 | + A bytes representation of the DistributedContext. |
| 107 | + """ |
| 108 | + |
| 109 | + |
| 110 | +def set_propagator(propagator_instance: Propagator) -> None: |
| 111 | + """Set the propagator instance singleton. |
| 112 | + """ |
| 113 | + global _PROPAGATOR |
| 114 | + _PROPAGATOR = propagator_instance |
| 115 | + |
| 116 | + |
| 117 | +def global_propagator() -> Propagator: |
| 118 | + """Return the singleton propagator instance.""" |
| 119 | + return _PROPAGATOR |
| 120 | + |
| 121 | + |
| 122 | +_PROPAGATOR = None # type: typing.Optional[Propagator] |
0 commit comments