-
Notifications
You must be signed in to change notification settings - Fork 917
[carriers] add env carrier #4609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
04ed353
[carriers] add env carrier
adrielp 5b7f35f
[chore] updates
adrielp ccdb1d8
Merge remote-tracking branch 'origin/main' into env-carrier-proto
adrielp 7874714
[chore] updates to the envcarrier with tests
adrielp a35fb8b
[chore] update changelog
adrielp c36948d
Merge branch 'main' into env-carrier-proto
adrielp a3b778f
Merge branch 'main' into env-carrier-proto
adrielp d201419
[chore] set env carrier to private due to instability
adrielp 2a737e5
Merge branch 'main' into env-carrier-proto
adrielp 9502153
chore: update tests
adrielp c09388e
chore: run ruff and follow ignore pattern for tests with dedicated im…
adrielp 0069089
chore: apply suggestions from code review
adrielp ba5462c
[chore] use MutableMapping and take in suggested changes
adrielp 4564ff0
Merge branch 'main' into env-carrier-proto
adrielp 5d36786
Merge branch 'main' into env-carrier-proto
adrielp fe2305e
Merge branch 'main' into env-carrier-proto
adrielp d649966
Merge branch 'main' into env-carrier-proto
adrielp 6c31f00
Merge branch 'main' into env-carrier-proto
xrmx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
opentelemetry-api/src/opentelemetry/propagators/envcarrier.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
| import typing | ||
|
|
||
| from opentelemetry.propagators.textmap import Getter, Setter | ||
|
|
||
|
|
||
| class EnvironmentGetter(Getter[dict]): | ||
| """Getter implementation for extracting context and baggage from environment variables. | ||
|
|
||
| EnvironmentGetter creates a case-insensitive lookup from the current environment | ||
| variables and provides simple data access without validation. | ||
|
|
||
| Example usage: | ||
| getter = EnvironmentGetter() | ||
| traceparent = getter.get({}, "traceparent") | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| # Create case-insensitive lookup from current environment | ||
| self.carrier = {k.lower(): v for k, v in os.environ.items()} | ||
|
|
||
| def get( | ||
| self, carrier: dict, key: str | ||
| ) -> typing.Optional[typing.List[str]]: | ||
| """Get a value from the environment for the given key. | ||
|
|
||
| Args: | ||
| carrier: Not used for environment getter, maintained for interface compatibility | ||
| key: The key to look up (case-insensitive) | ||
|
|
||
| Returns: | ||
| A list with a single string value if the key exists, None otherwise. | ||
| """ | ||
| val = self.carrier.get(key.lower()) | ||
| if val is None: | ||
| return None | ||
| if isinstance(val, typing.Iterable) and not isinstance(val, str): | ||
| return list(val) | ||
| return [val] | ||
|
|
||
| def keys(self, carrier: dict) -> typing.List[str]: | ||
| """Get all keys from the environment carrier. | ||
|
|
||
| Args: | ||
| carrier: Not used for environment getter, maintained for interface compatibility | ||
|
|
||
| Returns: | ||
| List of all environment variable keys (lowercase). | ||
| """ | ||
| return list(self.carrier.keys()) | ||
|
|
||
|
|
||
| class EnvironmentSetter(Setter[dict]): | ||
| """Setter implementation for building environment variable dictionaries. | ||
|
|
||
| EnvironmentSetter builds a dictionary of environment variables that | ||
| can be passed to utilities like subprocess.run() | ||
|
|
||
| Example usage: | ||
| setter = EnvironmentSetter() | ||
| env_vars = {} | ||
| setter.set(env_vars, "traceparent", "00-trace-id-span-id-01") | ||
| subprocess.run(myCommand, env=env_vars) | ||
| """ | ||
|
|
||
| def set( | ||
| self, carrier: typing.Optional[dict], key: str, value: str | ||
| ) -> None: | ||
| """Set a value in the carrier dictionary for the given key. | ||
|
|
||
| Args: | ||
| carrier: Dictionary to store environment variables, created if None | ||
| key: The key to set (will be converted to uppercase) | ||
| value: The value to set | ||
| """ | ||
| if carrier is None: | ||
| carrier = {} | ||
| carrier[key.upper()] = value |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.