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
15from __future__ import annotations
16
17import typing
18from abc import ABC, abstractmethod
19from contextvars import Token
20
21
22class Context(typing.Dict[str, object]):
23 def __setitem__(self, key: str, value: object) -> None:
24 raise ValueError
25
26
27class _RuntimeContext(ABC):
28 """The RuntimeContext interface provides a wrapper for the different
29 mechanisms that are used to propagate context in Python.
30 Implementations can be made available via entry_points and
31 selected through environment variables.
32 """
33
34 @abstractmethod
35 def attach(self, context: Context) -> Token[Context]:
36 """Sets the current `Context` object. Returns a
37 token that can be used to reset to the previous `Context`.
38
39 Args:
40 context: The Context to set.
41 """
42
43 @abstractmethod
44 def get_current(self) -> Context:
45 """Returns the current `Context` object."""
46
47 @abstractmethod
48 def detach(self, token: Token[Context]) -> None:
49 """Resets Context to a previous value
50
51 Args:
52 token: A reference to a previous Context.
53 """
54
55
56__all__ = ["Context"]