Skip to content

Commit 259041f

Browse files
committed
Update environment variable carrier
1 parent ccdb1d8 commit 259041f

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

opentelemetry-api/src/opentelemetry/propagators/envcarrier.py

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,63 @@
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+
115
import os
216
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)
441

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+
)
549
class EnvironmentGetter(Getter[dict]):
650
"""This class decorates Getter to enable extracting from context and baggage
751
from environment variables.
852
"""
9-
53+
1054
def __init__(self):
1155
self.env_copy = dict(os.environ)
1256
self.carrier = {}
13-
57+
1458
for env_key, env_value in self.env_copy.items():
1559
self.carrier[env_key.lower()] = env_value
16-
60+
1761
def get(self, carrier: dict, key: str) -> typing.Optional[typing.List[str]]:
1862
"""Get a value from the carrier for the given key"""
1963
val = self.carrier.get(key, None)
@@ -22,11 +66,15 @@ def get(self, carrier: dict, key: str) -> typing.Optional[typing.List[str]]:
2266
if isinstance(val, typing.Iterable) and not isinstance(val, str):
2367
return list(val)
2468
return [val]
25-
69+
2670
def keys(self, carrier: dict) -> typing.List[str]:
2771
"""Get all keys from the carrier"""
2872
return list(self.carrier.keys())
2973

74+
75+
@deprecated(
76+
"opentelemetry.propagators.envcarrier.EnvironmentSetter will be removed in a future version.",
77+
)
3078
class EnvironmentSetter(Setter[dict]):
3179
"""This class decorates Setter to enable setting context and baggage
3280
to environment variables.
@@ -41,5 +89,5 @@ def set(self, carrier: typing.Optional[dict], key: str, value: str) -> None:
4189
value: The value to set
4290
"""
4391
env_key = key.upper()
44-
92+
4593
os.environ[env_key] = value

0 commit comments

Comments
 (0)