Skip to content

Commit 9880358

Browse files
authored
Ensure that lambda headers are always a dict (#1907)
* Ensure that lambda headers are always a dict * CHANGELOG
1 parent 468ecf7 commit 9880358

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ endif::[]
4343
===== Bug fixes
4444
4545
* Fix url argument fetching in aiohttp_client instrumentation {pull}1890[#1890]
46+
* Fix a bug in the AWS Lambda instrumentation when `event["headers"] is None` {pull}1907[#1907]
4647
4748
4849

elasticapm/contrib/serverless/aws.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def __enter__(self):
162162
# service like Step Functions, and is unlikely to be standardized
163163
# in any way. We just have to rely on our defaults in this case.
164164
self.event = {}
165-
trace_parent = TraceParent.from_headers(self.event.get("headers", {}))
165+
trace_parent = TraceParent.from_headers(self.event.get("headers") or {})
166166

167167
global COLD_START
168168
cold_start = COLD_START
@@ -525,7 +525,7 @@ def get_url_dict(event: dict) -> dict:
525525
"""
526526
Reconstruct URL from API Gateway
527527
"""
528-
headers = event.get("headers", {})
528+
headers = event.get("headers") or {}
529529
protocol = headers.get("X-Forwarded-Proto", headers.get("x-forwarded-proto", "https"))
530530
host = headers.get("Host", headers.get("host", ""))
531531
stage = nested_key(event, "requestContext", "stage") or ""

tests/contrib/serverless/aws_tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,18 @@ def test_func(event, context):
455455
assert b"metadata" in request.data
456456
assert b"transaction" in request.data
457457
sending_elasticapm_client.close()
458+
459+
460+
def test_with_headers_as_none(event_api2, context, elasticapm_client):
461+
os.environ["AWS_LAMBDA_FUNCTION_NAME"] = "test_func"
462+
463+
@capture_serverless
464+
def test_func(event, context):
465+
with capture_span("test_span"):
466+
time.sleep(0.01)
467+
return {"statusCode": 200, "headers": {"foo": "bar"}}
468+
469+
event_api2["headers"] = None
470+
471+
test_func(event_api2, context)
472+
assert len(elasticapm_client.events[constants.TRANSACTION]) == 1

0 commit comments

Comments
 (0)