composite.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. import logging
  15. import typing
  16. from deprecated import deprecated
  17. from opentelemetry.context.context import Context
  18. from opentelemetry.propagators import textmap
  19. logger = logging.getLogger(__name__)
  20. class CompositePropagator(textmap.TextMapPropagator):
  21. """CompositePropagator provides a mechanism for combining multiple
  22. propagators into a single one.
  23. Args:
  24. propagators: the list of propagators to use
  25. """
  26. def __init__(
  27. self, propagators: typing.Sequence[textmap.TextMapPropagator]
  28. ) -> None:
  29. self._propagators = propagators
  30. def extract(
  31. self,
  32. carrier: textmap.CarrierT,
  33. context: typing.Optional[Context] = None,
  34. getter: textmap.Getter[textmap.CarrierT] = textmap.default_getter,
  35. ) -> Context:
  36. """Run each of the configured propagators with the given context and carrier.
  37. Propagators are run in the order they are configured, if multiple
  38. propagators write the same context key, the propagator later in the list
  39. will override previous propagators.
  40. See `opentelemetry.propagators.textmap.TextMapPropagator.extract`
  41. """
  42. for propagator in self._propagators:
  43. context = propagator.extract(carrier, context, getter=getter)
  44. return context # type: ignore
  45. def inject(
  46. self,
  47. carrier: textmap.CarrierT,
  48. context: typing.Optional[Context] = None,
  49. setter: textmap.Setter[textmap.CarrierT] = textmap.default_setter,
  50. ) -> None:
  51. """Run each of the configured propagators with the given context and carrier.
  52. Propagators are run in the order they are configured, if multiple
  53. propagators write the same carrier key, the propagator later in the list
  54. will override previous propagators.
  55. See `opentelemetry.propagators.textmap.TextMapPropagator.inject`
  56. """
  57. for propagator in self._propagators:
  58. propagator.inject(carrier, context, setter=setter)
  59. @property
  60. def fields(self) -> typing.Set[str]:
  61. """Returns a set with the fields set in `inject`.
  62. See
  63. `opentelemetry.propagators.textmap.TextMapPropagator.fields`
  64. """
  65. composite_fields = set()
  66. for propagator in self._propagators:
  67. for field in propagator.fields:
  68. composite_fields.add(field)
  69. return composite_fields
  70. @deprecated(version="1.2.0", reason="You should use CompositePropagator") # type: ignore
  71. class CompositeHTTPPropagator(CompositePropagator):
  72. """CompositeHTTPPropagator provides a mechanism for combining multiple
  73. propagators into a single one.
  74. """