Skip to content

Commit 837f294

Browse files
fix(integrations): Fix Lambda integration with EventBridge source (getsentry#2546)
When a lambda is triggered by an AWS EventBridge pipe the record contains an explicit "headers" key with an empty list. This breaks the assumption that headers is always a dict or None. Update the AwsLambdaIntegration to explicitly verify that header is a dict before passing it on to the `continue_trace` function. Fixes getsentryGH-2545 --------- Co-authored-by: Ivana Kellyerova <ivana.kellyerova@sentry.io>
1 parent f9ffe96 commit 837f294

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

sentry_sdk/integrations/aws_lambda.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs):
137137
# Starting the thread to raise timeout warning exception
138138
timeout_thread.start()
139139

140-
headers = request_data.get("headers")
141-
# AWS Service may set an explicit `{headers: None}`, we can't rely on `.get()`'s default.
142-
if headers is None:
140+
headers = request_data.get("headers", {})
141+
# Some AWS Services (ie. EventBridge) set headers as a list
142+
# or None, so we must ensure it is a dict
143+
if not isinstance(headers, dict):
143144
headers = {}
144145

145146
transaction = continue_trace(

tests/integrations/aws_lambda/test_aws.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,3 +855,26 @@ def test_handler(event, context):
855855
== error_event["contexts"]["trace"]["trace_id"]
856856
== "471a43a4192642f0b136d5159a501701"
857857
)
858+
859+
860+
def test_basic_with_eventbridge_source(run_lambda_function):
861+
_, events, response = run_lambda_function(
862+
LAMBDA_PRELUDE
863+
+ dedent(
864+
"""
865+
init_sdk()
866+
867+
def test_handler(event, context):
868+
raise Exception("Oh!")
869+
"""
870+
),
871+
b'[{"topic":"lps-ranges","partition":1,"offset":0,"timestamp":1701268939207,"timestampType":"CREATE_TIME","key":"REDACTED","value":"REDACTED","headers":[],"eventSourceArn":"REDACTED","bootstrapServers":"REDACTED","eventSource":"aws:kafka","eventSourceKey":"lps-ranges-1"}]',
872+
)
873+
874+
assert response["FunctionError"] == "Unhandled"
875+
876+
(event,) = events
877+
assert event["level"] == "error"
878+
(exception,) = event["exception"]["values"]
879+
assert exception["type"] == "Exception"
880+
assert exception["value"] == "Oh!"

0 commit comments

Comments
 (0)