Skip to content

Commit 7874714

Browse files
committed
[chore] updates to the envcarrier with tests
1 parent ccdb1d8 commit 7874714

File tree

2 files changed

+451
-24
lines changed

2 files changed

+451
-24
lines changed
Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,92 @@
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
17+
318
from opentelemetry.propagators.textmap import Getter, Setter
419

20+
521
class EnvironmentGetter(Getter[dict]):
6-
"""This class decorates Getter to enable extracting from context and baggage
7-
from environment variables.
22+
"""Getter implementation for extracting context and baggage from environment variables.
23+
24+
EnvironmentGetter creates a case-insensitive lookup from the current environment
25+
variables and provides simple data access without validation.
26+
27+
Example usage:
28+
getter = EnvironmentGetter()
29+
traceparent = getter.get({}, "traceparent")
830
"""
9-
31+
1032
def __init__(self):
11-
self.env_copy = dict(os.environ)
12-
self.carrier = {}
13-
14-
for env_key, env_value in self.env_copy.items():
15-
self.carrier[env_key.lower()] = env_value
16-
17-
def get(self, carrier: dict, key: str) -> typing.Optional[typing.List[str]]:
18-
"""Get a value from the carrier for the given key"""
19-
val = self.carrier.get(key, None)
33+
# Create case-insensitive lookup from current environment
34+
self.carrier = {k.lower(): v for k, v in os.environ.items()}
35+
36+
def get(
37+
self, carrier: dict, key: str
38+
) -> typing.Optional[typing.List[str]]:
39+
"""Get a value from the environment for the given key.
40+
41+
Args:
42+
carrier: Not used for environment getter, maintained for interface compatibility
43+
key: The key to look up (case-insensitive)
44+
45+
Returns:
46+
A list with a single string value if the key exists, None otherwise.
47+
"""
48+
val = self.carrier.get(key.lower())
2049
if val is None:
2150
return None
2251
if isinstance(val, typing.Iterable) and not isinstance(val, str):
2352
return list(val)
2453
return [val]
25-
54+
2655
def keys(self, carrier: dict) -> typing.List[str]:
27-
"""Get all keys from the carrier"""
56+
"""Get all keys from the environment carrier.
57+
58+
Args:
59+
carrier: Not used for environment getter, maintained for interface compatibility
60+
61+
Returns:
62+
List of all environment variable keys (lowercase).
63+
"""
2864
return list(self.carrier.keys())
2965

66+
3067
class EnvironmentSetter(Setter[dict]):
31-
"""This class decorates Setter to enable setting context and baggage
32-
to environment variables.
68+
"""Setter implementation for building environment variable dictionaries.
69+
70+
EnvironmentSetter builds a dictionary of environment variables that
71+
can be passed to utilities like subprocess.run()
72+
73+
Example usage:
74+
setter = EnvironmentSetter()
75+
env_vars = {}
76+
setter.set(env_vars, "traceparent", "00-trace-id-span-id-01")
77+
subprocess.run(myCommand, env=env_vars)
3378
"""
3479

35-
def set(self, carrier: typing.Optional[dict], key: str, value: str) -> None:
36-
"""Set a value in the environment for the given key.
37-
80+
def set(
81+
self, carrier: typing.Optional[dict], key: str, value: str
82+
) -> None:
83+
"""Set a value in the carrier dictionary for the given key.
84+
3885
Args:
39-
carrier: Not used for environment setter, but kept for interface compatibility
40-
key: The key to set
86+
carrier: Dictionary to store environment variables, created if None
87+
key: The key to set (will be converted to uppercase)
4188
value: The value to set
4289
"""
43-
env_key = key.upper()
44-
45-
os.environ[env_key] = value
90+
if carrier is None:
91+
carrier = {}
92+
carrier[key.upper()] = value

0 commit comments

Comments
 (0)