context.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright The OpenTelemetry Authors
  2. # SPDX-License-Identifier: Apache-2.0
  3. from __future__ import annotations
  4. from abc import ABC, abstractmethod
  5. from contextvars import Token
  6. class Context(dict[str, object]):
  7. def __setitem__(self, key: str, value: object) -> None:
  8. raise ValueError
  9. def __delitem__(self, key: str) -> None:
  10. raise ValueError
  11. def setdefault(self, key: str, default: object = None) -> object:
  12. raise ValueError
  13. def pop(self, key: str, *args: object) -> object:
  14. raise ValueError
  15. def popitem(self) -> tuple[str, object]:
  16. raise ValueError
  17. def clear(self) -> None:
  18. raise ValueError
  19. def update(self, *args: object, **kwargs: object) -> None:
  20. raise ValueError
  21. def __ior__(self, other: object) -> Context:
  22. raise ValueError
  23. class _RuntimeContext(ABC):
  24. """The RuntimeContext interface provides a wrapper for the different
  25. mechanisms that are used to propagate context in Python.
  26. Implementations can be made available via entry_points and
  27. selected through environment variables.
  28. """
  29. @abstractmethod
  30. def attach(self, context: Context) -> Token[Context]:
  31. """Sets the current `Context` object. Returns a
  32. token that can be used to reset to the previous `Context`.
  33. Args:
  34. context: The Context to set.
  35. """
  36. @abstractmethod
  37. def get_current(self) -> Context:
  38. """Returns the current `Context` object."""
  39. @abstractmethod
  40. def detach(self, token: Token[Context]) -> None:
  41. """Resets Context to a previous value
  42. Args:
  43. token: A reference to a previous Context.
  44. """
  45. __all__ = ["Context"]