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 def __delitem__(self, key: str) -> None:
15 raise ValueError
16
17 def setdefault(self, key: str, default: object = None) -> object:
18 raise ValueError
19
20 def pop(self, key: str, *args: object) -> object:
21 raise ValueError
22
23 def popitem(self) -> tuple[str, object]:
24 raise ValueError
25
26 def clear(self) -> None:
27 raise ValueError
28
29 def update(self, *args: object, **kwargs: object) -> None:
30 raise ValueError
31
32 def __ior__(self, other: object) -> Context:
33 raise ValueError
34
35
36class _RuntimeContext(ABC):
37 """The RuntimeContext interface provides a wrapper for the different
38 mechanisms that are used to propagate context in Python.
39 Implementations can be made available via entry_points and
40 selected through environment variables.
41 """
42
43 @abstractmethod
44 def attach(self, context: Context) -> Token[Context]:
45 """Sets the current `Context` object. Returns a
46 token that can be used to reset to the previous `Context`.
47
48 Args:
49 context: The Context to set.
50 """
51
52 @abstractmethod
53 def get_current(self) -> Context:
54 """Returns the current `Context` object."""
55
56 @abstractmethod
57 def detach(self, token: Token[Context]) -> None:
58 """Resets Context to a previous value
59
60 Args:
61 token: A reference to a previous Context.
62 """
63
64
65__all__ = ["Context"]