Skip to content

fix(integrations): Fix Lambda integration with EventBridge source #2546

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 4 additions & 3 deletions sentry_sdk/integrations/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs):
# Starting the thread to raise timeout warning exception
timeout_thread.start()

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

transaction = continue_trace(
Expand Down
23 changes: 23 additions & 0 deletions tests/integrations/aws_lambda/test_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,3 +855,26 @@ def test_handler(event, context):
== error_event["contexts"]["trace"]["trace_id"]
== "471a43a4192642f0b136d5159a501701"
)


def test_basic_with_eventbridge_source(run_lambda_function):
_, events, response = run_lambda_function(
LAMBDA_PRELUDE
+ dedent(
"""
init_sdk()

def test_handler(event, context):
raise Exception("Oh!")
"""
),
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"}]',
)

assert response["FunctionError"] == "Unhandled"

(event,) = events
assert event["level"] == "error"
(exception,) = event["exception"]["values"]
assert exception["type"] == "Exception"
assert exception["value"] == "Oh!"