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
+
1
15
import os
2
16
import typing
3
- from opentelemetry .propagators .textmap import Getter , Setter
17
+ from opentelemetry .propagators .textmap import Getter , Setter , TextMapPropagator
18
+ from typing_extensions import deprecated
19
+
20
+ class EnvironmentVariableCarrier (dict ):
21
+ """
22
+ A carrier for environment variables.
23
+
24
+ This class provides a dictionary-like interface for accessing and modifying
25
+ environment variables. It is used by the `EnvironmentVariablePropagator`
26
+ to inject and extract context information from the environment.
27
+ """
28
+
29
+ def __init__ (self ):
30
+ super ().__init__ ()
31
+ self ._environ = os .environ
32
+
33
+ def __getitem__ (self , key : str ) -> str :
34
+ return self ._environ .get (key , super ().__getitem__ (key ))
35
+
36
+ def __setitem__ (self , key : str , value : str ) -> None :
37
+ super ().__setitem__ (key , value )
38
+
39
+ def __delitem__ (self , key : str ) -> None :
40
+ super ().__delitem__ (key )
4
41
42
+ def __iter__ (self ) -> typing .Iterator [str ]:
43
+ return iter (set (self ._environ .keys ()) | set (super ().keys ()))
44
+
45
+
46
+ @deprecated (
47
+ "opentelemetry.propagators.envcarrier.EnvironmentGetter will be removed in a future version." ,
48
+ )
5
49
class EnvironmentGetter (Getter [dict ]):
6
50
"""This class decorates Getter to enable extracting from context and baggage
7
51
from environment variables.
8
52
"""
9
-
53
+
10
54
def __init__ (self ):
11
55
self .env_copy = dict (os .environ )
12
56
self .carrier = {}
13
-
57
+
14
58
for env_key , env_value in self .env_copy .items ():
15
59
self .carrier [env_key .lower ()] = env_value
16
-
60
+
17
61
def get (self , carrier : dict , key : str ) -> typing .Optional [typing .List [str ]]:
18
62
"""Get a value from the carrier for the given key"""
19
63
val = self .carrier .get (key , None )
@@ -22,11 +66,15 @@ def get(self, carrier: dict, key: str) -> typing.Optional[typing.List[str]]:
22
66
if isinstance (val , typing .Iterable ) and not isinstance (val , str ):
23
67
return list (val )
24
68
return [val ]
25
-
69
+
26
70
def keys (self , carrier : dict ) -> typing .List [str ]:
27
71
"""Get all keys from the carrier"""
28
72
return list (self .carrier .keys ())
29
73
74
+
75
+ @deprecated (
76
+ "opentelemetry.propagators.envcarrier.EnvironmentSetter will be removed in a future version." ,
77
+ )
30
78
class EnvironmentSetter (Setter [dict ]):
31
79
"""This class decorates Setter to enable setting context and baggage
32
80
to environment variables.
@@ -41,5 +89,5 @@ def set(self, carrier: typing.Optional[dict], key: str, value: str) -> None:
41
89
value: The value to set
42
90
"""
43
91
env_key = key .upper ()
44
-
92
+
45
93
os .environ [env_key ] = value
0 commit comments