|
| 1 | +import logging |
| 2 | +import os |
| 3 | + |
| 4 | +from aws_lambda_powertools.shared.version import VERSION |
| 5 | + |
| 6 | +powertools_version = VERSION |
| 7 | +inject_header = True |
| 8 | + |
| 9 | +try: |
| 10 | + import botocore |
| 11 | +except ImportError: |
| 12 | + # if botocore failed to import, user might be using custom runtime and we can't inject header |
| 13 | + inject_header = False |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | +EXEC_ENV = os.environ.get("AWS_EXECUTION_ENV", "NA") |
| 18 | +TARGET_SDK_EVENT = "request-created" |
| 19 | +FEATURE_PREFIX = "PT" |
| 20 | +DEFAULT_FEATURE = "no-op" |
| 21 | +HEADER_NO_OP = f"{FEATURE_PREFIX}/{DEFAULT_FEATURE}/{powertools_version} PTEnv/{EXEC_ENV}" |
| 22 | + |
| 23 | + |
| 24 | +def _initializer_botocore_session(session): |
| 25 | + """ |
| 26 | + This function is used to add an extra header for the User-Agent in the Botocore session, |
| 27 | + as described in the pull request: https://github.com/boto/botocore/pull/2682 |
| 28 | +
|
| 29 | + Parameters |
| 30 | + ---------- |
| 31 | + session : botocore.session.Session |
| 32 | + The Botocore session to which the user-agent function will be registered. |
| 33 | +
|
| 34 | + Raises |
| 35 | + ------ |
| 36 | + Exception |
| 37 | + If there is an issue while adding the extra header for the User-Agent. |
| 38 | +
|
| 39 | + """ |
| 40 | + try: |
| 41 | + session.register(TARGET_SDK_EVENT, _create_feature_function(DEFAULT_FEATURE)) |
| 42 | + except Exception: |
| 43 | + logger.debug("Can't add extra header User-Agent") |
| 44 | + |
| 45 | + |
| 46 | +def _create_feature_function(feature): |
| 47 | + """ |
| 48 | + Create and return the `add_powertools_feature` function. |
| 49 | +
|
| 50 | + The `add_powertools_feature` function is designed to be registered in boto3's event system. |
| 51 | + When registered, it appends the given feature string to the User-Agent header of AWS SDK requests. |
| 52 | +
|
| 53 | + Parameters |
| 54 | + ---------- |
| 55 | + feature : str |
| 56 | + The feature string to be appended to the User-Agent header. |
| 57 | +
|
| 58 | + Returns |
| 59 | + ------- |
| 60 | + add_powertools_feature : Callable |
| 61 | + The `add_powertools_feature` function that modifies the User-Agent header. |
| 62 | +
|
| 63 | +
|
| 64 | + """ |
| 65 | + |
| 66 | + def add_powertools_feature(request, **kwargs): |
| 67 | + try: |
| 68 | + headers = request.headers |
| 69 | + header_user_agent = ( |
| 70 | + f"{headers['User-Agent']} {FEATURE_PREFIX}/{feature}/{powertools_version} PTEnv/{EXEC_ENV}" |
| 71 | + ) |
| 72 | + |
| 73 | + # This function is exclusive to client and resources objects created in Powertools |
| 74 | + # and must remove the no-op header, if present |
| 75 | + if HEADER_NO_OP in headers["User-Agent"] and feature != DEFAULT_FEATURE: |
| 76 | + # Remove HEADER_NO_OP + space |
| 77 | + header_user_agent = header_user_agent.replace(f"{HEADER_NO_OP} ", "") |
| 78 | + |
| 79 | + headers["User-Agent"] = f"{header_user_agent}" |
| 80 | + except Exception: |
| 81 | + logger.debug("Can't find User-Agent header") |
| 82 | + |
| 83 | + return add_powertools_feature |
| 84 | + |
| 85 | + |
| 86 | +# Add feature user-agent to given sdk boto3.session |
| 87 | +def register_feature_to_session(session, feature): |
| 88 | + """ |
| 89 | + Register the given feature string to the event system of the provided boto3 session |
| 90 | + and append the feature to the User-Agent header of the request |
| 91 | +
|
| 92 | + Parameters |
| 93 | + ---------- |
| 94 | + session : boto3.session.Session |
| 95 | + The boto3 session to which the feature will be registered. |
| 96 | + feature : str |
| 97 | + The feature string to be appended to the User-Agent header, e.g., "streaming" in Powertools. |
| 98 | +
|
| 99 | + Raises |
| 100 | + ------ |
| 101 | + AttributeError |
| 102 | + If the provided session does not have an event system. |
| 103 | +
|
| 104 | + """ |
| 105 | + try: |
| 106 | + session.events.register(TARGET_SDK_EVENT, _create_feature_function(feature)) |
| 107 | + except AttributeError as e: |
| 108 | + logger.debug(f"session passed in doesn't have a event system:{e}") |
| 109 | + |
| 110 | + |
| 111 | +# Add feature user-agent to given sdk boto3.client |
| 112 | +def register_feature_to_client(client, feature): |
| 113 | + """ |
| 114 | + Register the given feature string to the event system of the provided boto3 client |
| 115 | + and append the feature to the User-Agent header of the request |
| 116 | +
|
| 117 | + Parameters |
| 118 | + ---------- |
| 119 | + client : boto3.session.Session.client |
| 120 | + The boto3 client to which the feature will be registered. |
| 121 | + feature : str |
| 122 | + The feature string to be appended to the User-Agent header, e.g., "streaming" in Powertools. |
| 123 | +
|
| 124 | + Raises |
| 125 | + ------ |
| 126 | + AttributeError |
| 127 | + If the provided client does not have an event system. |
| 128 | +
|
| 129 | + """ |
| 130 | + try: |
| 131 | + client.meta.events.register(TARGET_SDK_EVENT, _create_feature_function(feature)) |
| 132 | + except AttributeError as e: |
| 133 | + logger.debug(f"session passed in doesn't have a event system:{e}") |
| 134 | + |
| 135 | + |
| 136 | +# Add feature user-agent to given sdk boto3.resource |
| 137 | +def register_feature_to_resource(resource, feature): |
| 138 | + """ |
| 139 | + Register the given feature string to the event system of the provided boto3 resource |
| 140 | + and append the feature to the User-Agent header of the request |
| 141 | +
|
| 142 | + Parameters |
| 143 | + ---------- |
| 144 | + resource : boto3.session.Session.resource |
| 145 | + The boto3 resource to which the feature will be registered. |
| 146 | + feature : str |
| 147 | + The feature string to be appended to the User-Agent header, e.g., "streaming" in Powertools. |
| 148 | +
|
| 149 | + Raises |
| 150 | + ------ |
| 151 | + AttributeError |
| 152 | + If the provided resource does not have an event system. |
| 153 | +
|
| 154 | + """ |
| 155 | + try: |
| 156 | + resource.meta.client.meta.events.register(TARGET_SDK_EVENT, _create_feature_function(feature)) |
| 157 | + except AttributeError as e: |
| 158 | + logger.debug(f"resource passed in doesn't have a event system:{e}") |
| 159 | + |
| 160 | + |
| 161 | +def inject_user_agent(): |
| 162 | + if inject_header: |
| 163 | + # Customize botocore session to inject Powertools header |
| 164 | + # See: https://github.com/boto/botocore/pull/2682 |
| 165 | + botocore.register_initializer(_initializer_botocore_session) |
0 commit comments