-
Notifications
You must be signed in to change notification settings - Fork 861
[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 1 commit
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
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
| import os | ||
| import typing | ||
| from opentelemetry.propagators.textmap import Getter, Setter | ||
|
|
||
| class EnvironmentGetter(Getter[dict]): | ||
| """This class decorates Getter to enable extracting from context and baggage | ||
| from environment variables. | ||
| """ | ||
|
|
||
| KEY_MAPPING = { | ||
| "TRACEPARENT": "traceparent", | ||
| "TRACESTATE": "tracestate", | ||
| "BAGGAGE": "baggage" | ||
| } | ||
|
|
||
| def __init__(self): | ||
| self.env_copy = dict(os.environ) | ||
| self.carrier = {} | ||
|
|
||
| for env_key, env_value in self.env_copy.items(): | ||
| if env_key in self.KEY_MAPPING: | ||
| self.carrier[self.KEY_MAPPING[env_key]] = env_value | ||
| else: | ||
| self.carrier[env_key] = env_value | ||
|
|
||
| def get(self, carrier: dict, key: str) -> typing.Optional[typing.List[str]]: | ||
| """Get a value from the carrier for the given key""" | ||
| val = self.carrier.get(key, None) | ||
| 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 carrier""" | ||
| return list(self.carrier.keys()) | ||
|
|
||
| class EnvironmentSetter(Setter[dict]): | ||
| """This class decorates Setter to enable setting context and baggage | ||
| to environment variables. | ||
|
adrielp marked this conversation as resolved.
Outdated
|
||
| """ | ||
|
|
||
| KEY_MAPPING = { | ||
| "TRACEPARENT": "traceparent", | ||
| "TRACESTATE": "tracestate", | ||
| "BAGGAGE": "baggage" | ||
| } | ||
|
|
||
| def set(self, carrier: typing.Optional[dict], key: str, value: str) -> None: | ||
| """Set a value in the environment for the given key. | ||
|
|
||
| Args: | ||
| carrier: Not used for environment setter, but kept for interface compatibility | ||
| key: The key to set | ||
| value: The value to set | ||
| """ | ||
| env_key = self.KEY_MAPPING.get(key, key.upper()) | ||
|
|
||
| os.environ[env_key] = value | ||
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.