Skip to content

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
merged 28 commits into from
Jun 1, 2023
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
6c36aa6
POC of user-agent
roger-zhangg May 2, 2023
23f4155
Changes for Heitor's review
roger-zhangg May 15, 2023
52ebc8b
Merge branch 'awslabs:develop' into user-agent
roger-zhangg May 15, 2023
a694ca2
Merge remote-tracking branch 'origin/user-agent' into user-agent
roger-zhangg May 15, 2023
dc8d1ad
Changes for Heitor's review
roger-zhangg May 15, 2023
6404ee2
Changes for Heitor's review
roger-zhangg May 15, 2023
b2402e0
add patching function for resource
roger-zhangg May 16, 2023
deeeb08
Merge branch 'awslabs:develop' into user-agent
roger-zhangg May 16, 2023
b0c2e95
Merge remote-tracking branch 'origin/user-agent' into user-agent
roger-zhangg May 16, 2023
d1a0128
add importlib-metadata in poetry
roger-zhangg May 17, 2023
5c7a5a2
user-agent: fixing small things
leandrodamascena May 17, 2023
17d6df1
fix poetry
leandrodamascena May 17, 2023
31a4c07
Merge remote-tracking branch 'upstream/develop' into user-agent
leandrodamascena May 17, 2023
0dec5b2
fix mypy
leandrodamascena May 17, 2023
afe0ee9
fix mypy
leandrodamascena May 17, 2023
00b75da
fix poetry
leandrodamascena May 17, 2023
37bf656
fix mypy
leandrodamascena May 18, 2023
c53e384
feat(user-agent): using default botocore initializer + minor changes
leandrodamascena May 18, 2023
60d188e
Merge branch 'awslabs:develop' into user-agent
roger-zhangg May 19, 2023
12150e2
change back to use register in initializer
roger-zhangg May 19, 2023
6b86d40
add docstring
roger-zhangg May 19, 2023
85247b4
merge poetry
leandrodamascena May 19, 2023
ad61d62
sync upstream
roger-zhangg May 29, 2023
d88f04b
sync upstream
roger-zhangg May 29, 2023
6c7767b
style/typo fixes for review
roger-zhangg May 29, 2023
8cb4b96
merge develop
leandrodamascena May 31, 2023
7148647
chore: docstring
leandrodamascena May 31, 2023
b79bd83
chore: docstring
leandrodamascena May 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 52 additions & 36 deletions aws_lambda_powertools/shared/user_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@

def _initializer_botocore_session(session):
"""
Pass in Botocore session and register _create_feature_function to append user-agent, for more details see
https://github.com/boto/botocore/pull/2682

session.register:
https://github.com/boto/botocore/blob/develop/botocore/session.py#L703
This function is used to add an extra header for the User-Agent in the Botocore session,
as described in the pull request: https://github.com/boto/botocore/pull/2682

Parameters
----------
session: Botocore.session.Session
A botocore session passed in to register user-agent function
session : botocore.session.Session
The Botocore session to which the user-agent function will be registered.

Raises
------
Exception
If there is an issue while adding the extra header for the User-Agent.

"""
try:
Expand All @@ -43,26 +45,25 @@ def _initializer_botocore_session(session):

def _create_feature_function(feature):
"""
Create a add_powertools_feature function using the given feature parameter
add_powertools_feature will be returned and to be registered in boto3's event system
once registered, add_powertools_feature will append the given feature string to user-agent of AWS SDK's request
Create and return the `add_powertools_feature` function.

The `add_powertools_feature` function is designed to be registered in boto3's event system.
When registered, it appends the given feature string to the User-Agent header of AWS SDK requests.

Parameters
----------
feature: str
feature : str
The feature string to be appended to the User-Agent header.

Returns:
Returns
-------
add_powertools_feature: Callable
add_powertools_feature : Callable
The `add_powertools_feature` function that modifies the User-Agent header.


"""

def add_powertools_feature(request, **kwargs):
"""
Signiture of this function is required at Boto3's event system. See:
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/events.html

"""
try:
headers = request.headers
header_user_agent = (
Expand All @@ -85,15 +86,20 @@ def add_powertools_feature(request, **kwargs):
# Add feature user-agent to given sdk boto3.session
def register_feature_to_session(session, feature):
"""
Register the given feature string given session's event system
When this session makes an api request, the given feature will be appended to request's user-agent
Register the given feature string to the event system of the provided boto3 session
and append the feature to the User-Agent header of the request

Parameters
----------
session: boto3.session.Session
session to register
feature: str
the feature string in Powertools, e.g.:streaming
session : boto3.session.Session
The boto3 session to which the feature will be registered.
feature : str
The feature string to be appended to the User-Agent header, e.g., "streaming" in Powertools.

Raises
------
AttributeError
If the provided session does not have an event system.

"""
try:
Expand All @@ -105,15 +111,20 @@ def register_feature_to_session(session, feature):
# Add feature user-agent to given sdk boto3.client
def register_feature_to_client(client, feature):
"""
Register the given feature string given client's event system
When this client makes an api request, the given feature will be appended to request's user-agent
Register the given feature string to the event system of the provided boto3 client
and append the feature to the User-Agent header of the request

Parameters
----------
client: boto3.session.Session.client
client to register
feature: str
the feature string in Powertools, e.g.:streaming
session : boto3.session.Session.client
The boto3 client to which the feature will be registered.
feature : str
The feature string to be appended to the User-Agent header, e.g., "streaming" in Powertools.

Raises
------
AttributeError
If the provided client does not have an event system.

"""
try:
Expand All @@ -125,15 +136,20 @@ def register_feature_to_client(client, feature):
# Add feature user-agent to given sdk boto3.resource
def register_feature_to_resource(resource, feature):
"""
Register the given feature string given resource's event system
When this resource makes an api request, the given feature will be appended to request's user-agent
Register the given feature string to the event system of the provided boto3 resource
and append the feature to the User-Agent header of the request

Parameters
----------
resource: boto3.session.Session.resource
resource to register
feature: str
the feature string in Powertools, e.g.:streaming
session : boto3.session.Session.resource
The boto3 client to which the feature will be registered.
feature : str
The feature string to be appended to the User-Agent header, e.g., "streaming" in Powertools.

Raises
------
AttributeError
If the provided resource does not have an event system.

"""
try:
Expand Down