|
| 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 |
| 17 | + |
3 | 18 | from opentelemetry.propagators.textmap import Getter, Setter |
4 | 19 |
|
| 20 | + |
5 | 21 | 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") |
8 | 30 | """ |
9 | | - |
| 31 | + |
10 | 32 | 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()) |
20 | 49 | if val is None: |
21 | 50 | return None |
22 | 51 | if isinstance(val, typing.Iterable) and not isinstance(val, str): |
23 | 52 | return list(val) |
24 | 53 | return [val] |
25 | | - |
| 54 | + |
26 | 55 | 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 | + """ |
28 | 64 | return list(self.carrier.keys()) |
29 | 65 |
|
| 66 | + |
30 | 67 | 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) |
33 | 78 | """ |
34 | 79 |
|
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 | +
|
38 | 85 | 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) |
41 | 88 | value: The value to set |
42 | 89 | """ |
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