-
Notifications
You must be signed in to change notification settings - Fork 436
feat(user-agent): add custom header User-Agent to AWS SDK requests #2267
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
leandrodamascena
merged 28 commits into
aws-powertools:develop
from
roger-zhangg:user-agent
Jun 1, 2023
Merged
Changes from 9 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
6c36aa6
POC of user-agent
roger-zhangg 23f4155
Changes for Heitor's review
roger-zhangg 52ebc8b
Merge branch 'awslabs:develop' into user-agent
roger-zhangg a694ca2
Merge remote-tracking branch 'origin/user-agent' into user-agent
roger-zhangg dc8d1ad
Changes for Heitor's review
roger-zhangg 6404ee2
Changes for Heitor's review
roger-zhangg b2402e0
add patching function for resource
roger-zhangg deeeb08
Merge branch 'awslabs:develop' into user-agent
roger-zhangg b0c2e95
Merge remote-tracking branch 'origin/user-agent' into user-agent
roger-zhangg d1a0128
add importlib-metadata in poetry
roger-zhangg 5c7a5a2
user-agent: fixing small things
leandrodamascena 17d6df1
fix poetry
leandrodamascena 31a4c07
Merge remote-tracking branch 'upstream/develop' into user-agent
leandrodamascena 0dec5b2
fix mypy
leandrodamascena afe0ee9
fix mypy
leandrodamascena 00b75da
fix poetry
leandrodamascena 37bf656
fix mypy
leandrodamascena c53e384
feat(user-agent): using default botocore initializer + minor changes
leandrodamascena 60d188e
Merge branch 'awslabs:develop' into user-agent
roger-zhangg 12150e2
change back to use register in initializer
roger-zhangg 6b86d40
add docstring
roger-zhangg 85247b4
merge poetry
leandrodamascena ad61d62
sync upstream
roger-zhangg d88f04b
sync upstream
roger-zhangg 6c7767b
style/typo fixes for review
roger-zhangg 8cb4b96
merge develop
leandrodamascena 7148647
chore: docstring
leandrodamascena b79bd83
chore: docstring
leandrodamascena 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
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,70 @@ | ||
import logging | ||
import os | ||
|
||
from importlib_metadata import version | ||
|
||
pt_version = version("aws-lambda-powertools") | ||
|
||
try: | ||
from botocore import handlers | ||
except ImportError: | ||
# if botocore failed to import, user might be using custom runtime. We can ignore here | ||
handlers = None | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
ENV_KEY = "AWS_EXECUTION_ENV" | ||
EXEC_ENV = os.environ.get(ENV_KEY, "NA") | ||
target_sdk_event = "request-created" | ||
|
||
|
||
# add user-agent like powertools/2.14.1 EXEC-ENV/AWS_Lambda_python3.9 | ||
def _add_pt_version(request, **kwargs): | ||
headers = request.headers | ||
# assume we always have a User-Agent. | ||
# Q: do we need to check if field: User-Agent exist in request.headers? | ||
# Q: do we care if User-Agent string exceed 256 characters? | ||
headers["User-Agent"] = f"{headers['User-Agent']} PT/no-op/{pt_version} PTEnv/{EXEC_ENV}" | ||
|
||
|
||
# creates the `add_feature_string` function with given feature parameter | ||
def _create_feature_function(feature): | ||
def add_pt_feature(request, **kwargs): | ||
headers = request.headers | ||
# Actually, only one handler can be registered, registering a new one will replace the prev handler | ||
# We don't need to replace/detect previous registered user-agent | ||
headers["User-Agent"] = f"{headers['User-Agent']} PT/{feature}/{pt_version} PTEnv/{EXEC_ENV}" | ||
|
||
# return created function | ||
return add_pt_feature | ||
|
||
|
||
# add feature user-agent to given sdk session | ||
def register_feature_to_session(session, feature): | ||
try: | ||
session.events.register(target_sdk_event, _create_feature_function(feature)) | ||
except AttributeError as e: | ||
logger.debug(f"session passed in doesn't have a event system:{e}") | ||
|
||
|
||
# add feature user-agent to given sdk client | ||
def register_feature_to_client(client, feature): | ||
try: | ||
client.meta.events.register(target_sdk_event, _create_feature_function(feature)) | ||
except AttributeError as e: | ||
logger.debug(f"session passed in doesn't have a event system:{e}") | ||
|
||
|
||
# add feature user-agent to given sdk session.resource | ||
def register_feature_to_resource(resource, feature): | ||
try: | ||
resource.meta.client.meta.events.register(target_sdk_event, _create_feature_function(feature)) | ||
except AttributeError as e: | ||
logger.debug(f"resource passed in doesn't have a event system:{e}") | ||
|
||
|
||
# register add_pt_version for all AWS SDK in runtime | ||
def inject_user_agent(): | ||
if handlers: | ||
# register add_user_agent to BUILTIN_HANDLERS so every aws sdk session will have this event registered | ||
handlers.BUILTIN_HANDLERS.append((target_sdk_event, _add_pt_version)) |
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
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
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
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.