Repository: boto/botocore
Pull request #2733 (2022-08-04 15:50:33)

Fix encoding scheme for trace ids
This is a follow up to #2727 to fix the encoding scheme for our trace IDs. The change omits characters that cannot be encoded to preserve content semantics with services parsing the header.

codecov-commenter: # [Codecov](https://codecov.io/gh/boto/botocore/pull/2733?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto) Report
> Merging [#2733](https://codecov.io/gh/boto/botocore/pull/2733?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto) (3ca364d) into [develop](https://codecov.io/gh/boto/botocore/commit/572be2a555ed12ec7480e900a77036a7e462ae8a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto) (572be2a) will **not change** coverage.
> The diff coverage is `100.00%`.

> :exclamation: Current head 3ca364d differs from pull request most recent head 9fd637f. Consider uploading reports for the commit 9fd637f to get more accurate results

```diff
@@           Coverage Diff            @@
##           develop    #2733   +/-   ##
========================================
  Coverage    95.25%   95.25%           
========================================
  Files           60       60           
  Lines        12278    12278           
========================================
  Hits         11696    11696           
  Misses         582      582           
```


| [Impacted Files](https://codecov.io/gh/boto/botocore/pull/2733?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto) | Coverage Δ | |
|---|---|---|
| [botocore/handlers.py](https://codecov.io/gh/boto/botocore/pull/2733/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto#diff-Ym90b2NvcmUvaGFuZGxlcnMucHk=) | `95.05% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto)


Patch:
diff --git a/.changes/next-release/enhancement-Lambda-61491.json b/.changes/next-release/enhancement-Lambda-61491.json
new file mode 100644
index 000000000..ca70dd6ff
--- /dev/null
+++ b/.changes/next-release/enhancement-Lambda-61491.json
@@ -0,0 +1,5 @@
+{
+  "type": "enhancement",
+  "category": "Lambda",
+  "description": "Add support for Trace ID in Lambda environments"
+}
diff --git a/botocore/handlers.py b/botocore/handlers.py
index 5c09ffb90..019fe0bcf 100644
--- a/botocore/handlers.py
+++ b/botocore/handlers.py
@@ -102,11 +102,11 @@ def handle_service_name_alias(service_name, **kwargs):
 
 def add_recursion_detection_header(params, **kwargs):
     has_lambda_name = 'AWS_LAMBDA_FUNCTION_NAME' in os.environ
-    trace_id = os.environ.get('_X_AMZ_TRACE_ID')
+    trace_id = os.environ.get('_X_AMZN_TRACE_ID')
     if has_lambda_name and trace_id:
         headers = params['headers']
         if 'X-Amzn-Trace-Id' not in headers:
-            headers['X-Amzn-Trace-Id'] = quote(trace_id)
+            headers['X-Amzn-Trace-Id'] = quote(trace_id, safe='-=;:+&[]{}"\',')
 
 
 def escape_xml_payload(params, **kwargs):


Test patch:
diff --git a/tests/unit/test_handlers.py b/tests/unit/test_handlers.py
index ab7b47ebb..60c139860 100644
--- a/tests/unit/test_handlers.py
+++ b/tests/unit/test_handlers.py
@@ -1519,26 +1519,58 @@ class TestPrependToHost(unittest.TestCase):
 @pytest.mark.parametrize(
     'environ, header_before, header_after',
     [
-        ({'AWS_LAMBDA_FUNCTION_NAME': 'foo'}, {}, {}),
-        ({'_X_AMZ_TRACE_ID': 'bar'}, {}, {}),
+        ({}, {}, {}),
+        ({'AWS_LAMBDA_FUNCTION_NAME': 'some-function'}, {}, {}),
         (
-            {'AWS_LAMBDA_FUNCTION_NAME': 'foo', '_X_AMZ_TRACE_ID': 'bar'},
+            {
+                '_X_AMZN_TRACE_ID': (
+                    'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;'
+                    'Sampled=1;lineage=a87bd80c:0,68fd508a:5,c512fbe3:2'
+                )
+            },
+            {},
             {},
-            {'X-Amzn-Trace-Id': 'bar'},
         ),
         (
-            {'AWS_LAMBDA_FUNCTION_NAME': 'foo', '_X_AMZ_TRACE_ID': 'bar'},
-            {'X-Amzn-Trace-Id': 'fizz'},
-            {'X-Amzn-Trace-Id': 'fizz'},
+            {
+                'AWS_LAMBDA_FUNCTION_NAME': 'some-function',
+                '_X_AMZN_TRACE_ID': (
+                    'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;'
+                    'Sampled=1;lineage=a87bd80c:0,68fd508a:5,c512fbe3:2'
+                ),
+            },
+            {},
+            {
+                'X-Amzn-Trace-Id': (
+                    'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;'
+                    'Sampled=1;lineage=a87bd80c:0,68fd508a:5,c512fbe3:2'
+                )
+            },
+        ),
+        (
+            {
+                'AWS_LAMBDA_FUNCTION_NAME': 'some-function',
+                '_X_AMZN_TRACE_ID': 'EnvValue',
+            },
+            {'X-Amzn-Trace-Id': 'OriginalValue'},
+            {'X-Amzn-Trace-Id': 'OriginalValue'},
         ),
         (
             {
                 'AWS_LAMBDA_FUNCTION_NAME': 'foo',
-                '_X_AMZ_TRACE_ID': 'first\nsecond',
+                '_X_AMZN_TRACE_ID': 'first\nsecond',
             },
             {},
             {'X-Amzn-Trace-Id': 'first%0Asecond'},
         ),
+        (
+            {
+                'AWS_LAMBDA_FUNCTION_NAME': 'foo',
+                '_X_AMZN_TRACE_ID': 'test123-=;:+&[]{}\"\'',
+            },
+            {},
+            {'X-Amzn-Trace-Id': 'test123-=;:+&[]{}\"\''},
+        ),
     ],
 )
 def test_add_recursion_detection_header(environ, header_before, header_after):