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