Skip to content

Commit c359291

Browse files
fix: Fix header extraction for AWS Lambda/ApiGateway (#945)
Co-authored-by: Markus Unterwaditzer <[email protected]>
1 parent e3549b3 commit c359291

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

sentry_sdk/integrations/aws_lambda.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs):
134134
# Starting the thread to raise timeout warning exception
135135
timeout_thread.start()
136136

137-
headers = request_data.get("headers", {})
137+
headers = request_data.get("headers")
138+
# AWS Service may set an explicit `{headers: None}`, we can't rely on `.get()`'s default.
139+
if headers is None:
140+
headers = {}
138141
transaction = Transaction.continue_from_headers(
139142
headers, op="serverless.function", name=aws_context.function_name
140143
)
@@ -337,11 +340,15 @@ def event_processor(sentry_event, hint, start_time=start_time):
337340
if _should_send_default_pii():
338341
user_info = sentry_event.setdefault("user", {})
339342

340-
id = aws_event.get("identity", {}).get("userArn")
343+
identity = aws_event.get("identity")
344+
if identity is None:
345+
identity = {}
346+
347+
id = identity.get("userArn")
341348
if id is not None:
342349
user_info.setdefault("id", id)
343350

344-
ip = aws_event.get("identity", {}).get("sourceIp")
351+
ip = identity.get("sourceIp")
345352
if ip is not None:
346353
user_info.setdefault("ip_address", ip)
347354

@@ -363,7 +370,11 @@ def event_processor(sentry_event, hint, start_time=start_time):
363370
def _get_url(aws_event, aws_context):
364371
# type: (Any, Any) -> str
365372
path = aws_event.get("path", None)
366-
headers = aws_event.get("headers", {})
373+
374+
headers = aws_event.get("headers")
375+
if headers is None:
376+
headers = {}
377+
367378
host = headers.get("Host", None)
368379
proto = headers.get("X-Forwarded-Proto", None)
369380
if proto and host and path:

0 commit comments

Comments
 (0)