Skip to content

Commit 5c279bd

Browse files
authored
Fix AWS tests in POTel (#3879)
Make AWS Lambda tests in POTel.
1 parent 7cf7373 commit 5c279bd

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

sentry_sdk/integrations/aws_lambda.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from copy import deepcopy
66
from datetime import datetime, timedelta, timezone
77
from os import environ
8+
from urllib.parse import urlencode
89

910
import sentry_sdk
1011
from sentry_sdk.consts import OP
@@ -120,6 +121,9 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs):
120121
configured_time = aws_context.get_remaining_time_in_millis()
121122

122123
with sentry_sdk.isolation_scope() as scope:
124+
scope.set_transaction_name(
125+
aws_context.function_name, source=TRANSACTION_SOURCE_COMPONENT
126+
)
123127
timeout_thread = None
124128
with capture_internal_exceptions():
125129
scope.clear_breadcrumbs()
@@ -333,7 +337,7 @@ def event_processor(sentry_event, hint, start_time=start_time):
333337
request["url"] = _get_url(aws_event, aws_context)
334338

335339
if "queryStringParameters" in aws_event:
336-
request["query_string"] = aws_event["queryStringParameters"]
340+
request["query_string"] = urlencode(aws_event["queryStringParameters"])
337341

338342
if "headers" in aws_event:
339343
request["headers"] = _filter_headers(aws_event["headers"])
@@ -373,7 +377,9 @@ def _get_url(aws_event, aws_context):
373377
path = aws_event.get("path", None)
374378

375379
headers = aws_event.get("headers")
376-
if headers is None:
380+
# Some AWS Services (ie. EventBridge) set headers as a list
381+
# or None, so we must ensure it is a dict
382+
if not isinstance(headers, dict):
377383
headers = {}
378384

379385
host = headers.get("Host", None)
@@ -478,7 +484,10 @@ def _prepopulate_attributes(aws_event, aws_context):
478484

479485
for prop, attr in EVENT_TO_ATTRIBUTES.items():
480486
if aws_event.get(prop) is not None:
481-
attributes[attr] = aws_event[prop]
487+
if prop == "queryStringParameters":
488+
attributes[attr] = urlencode(aws_event[prop])
489+
else:
490+
attributes[attr] = aws_event[prop]
482491

483492
for prop, attr in CONTEXT_TO_ATTRIBUTES.items():
484493
if getattr(aws_context, prop, None) is not None:
@@ -487,7 +496,7 @@ def _prepopulate_attributes(aws_event, aws_context):
487496
url = _get_url(aws_event, aws_context)
488497
if url:
489498
if aws_event.get("queryStringParameters"):
490-
url += f"?{aws_event['queryStringParameters']}"
499+
url += f"?{urlencode(aws_event['queryStringParameters'])}"
491500
attributes["url.full"] = url
492501

493502
headers = {}

tests/integrations/aws_lambda/test_aws.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ def truncate_data(data):
6767
6868
if data["contexts"].get("trace") is not None:
6969
cleaned_data["contexts"]["trace"] = data["contexts"].get("trace")
70+
if cleaned_data["contexts"]["trace"].get("data", {}) != {}:
71+
cleaned_data["contexts"]["trace"]["data"] = {"removed": "by truncate_data()"}
7072
7173
if data.get("transaction") is not None:
7274
cleaned_data["transaction"] = data.get("transaction")
@@ -287,7 +289,8 @@ def test_handler(event, context):
287289
"X-Forwarded-Proto": "https"
288290
},
289291
"queryStringParameters": {
290-
"bonkers": "true"
292+
"bonkers": "true",
293+
"wild": "false"
291294
},
292295
"pathParameters": null,
293296
"stageVariables": null,
@@ -312,7 +315,7 @@ def test_handler(event, context):
312315
"X-Forwarded-Proto": "https",
313316
},
314317
"method": "GET",
315-
"query_string": {"bonkers": "true"},
318+
"query_string": "bonkers=true&wild=false",
316319
"url": "https://iwsz2c7uwi.execute-api.us-east-1.amazonaws.com/asd",
317320
}
318321

@@ -487,6 +490,15 @@ def test_handler(event, context):
487490
),
488491
(b"[]", False, 1),
489492
],
493+
ids=[
494+
"int",
495+
"float",
496+
"string",
497+
"bool",
498+
"list",
499+
"list_with_request_data",
500+
"empty_list",
501+
],
490502
)
491503
def test_non_dict_event(
492504
run_lambda_function,
@@ -539,9 +551,7 @@ def test_handler(event, context):
539551
"headers": {"Host": "x1.io", "X-Forwarded-Proto": "https"},
540552
"method": "GET",
541553
"url": "https://x1.io/1",
542-
"query_string": {
543-
"done": "f",
544-
},
554+
"query_string": "done=f",
545555
}
546556
else:
547557
request_data = {"url": "awslambda:///{}".format(function_name)}
@@ -590,7 +600,7 @@ def test_traces_sampler_gets_correct_values_in_sampling_context(
590600

591601
import inspect
592602

593-
_, response = run_lambda_function(
603+
function_code = (
594604
LAMBDA_PRELUDE
595605
+ dedent(inspect.getsource(StringContaining))
596606
+ dedent(inspect.getsource(DictionaryContaining))
@@ -621,7 +631,7 @@ def test_handler(event, context):
621631
{
622632
"http.request.method": "GET",
623633
"url.path": "/sit/stay/rollover",
624-
"url.query": "repeat=again",
634+
"url.query": "repeat=twice",
625635
"url.full": "http://x.io/sit/stay/rollover?repeat=twice",
626636
"network.protocol.name": "http",
627637
"server.address": "x.io",
@@ -643,10 +653,15 @@ def test_handler(event, context):
643653
traces_sampler=traces_sampler,
644654
)
645655
"""
646-
),
647-
b'{"httpMethod": "GET", "path": "/sit/stay/rollover", "query_string": {"repeat": "again"}, "headers": {"Host": "x.io", "X-Forwarded-Proto": "http", "Custom-Header": "Custom Value"}}',
656+
)
648657
)
649658

659+
payload = b'{"httpMethod": "GET", "path": "/sit/stay/rollover", "queryStringParameters": {"repeat": "twice"}, "headers": {"Host": "x.io", "X-Forwarded-Proto": "http", "Custom-Header": "Custom Value"}}'
660+
661+
_, response = run_lambda_function(
662+
code=function_code,
663+
payload=payload,
664+
)
650665
assert response["Payload"]["AssertionError raised"] is False
651666

652667

0 commit comments

Comments
 (0)