Skip to content

contrib/serverless/aws: normalize headers with v1 event format #1982

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2024
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
20 changes: 19 additions & 1 deletion elasticapm/contrib/serverless/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ def prep_kwargs(kwargs=None):
return kwargs


def should_normalize_headers(event: dict) -> bool:
"""
Helper to decide if we should normalize headers or not depending on the event

Even if the documentation says that headers are lowercased it's not always the case for format version 1.0
https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
"""

request_context = event.get("requestContext", {})
return ("elb" in request_context or "requestId" in request_context) and "http" not in request_context


class _lambda_transaction(object):
"""
Context manager for creating transactions around AWS Lambda functions.
Expand Down Expand Up @@ -162,7 +174,13 @@ def __enter__(self):
# service like Step Functions, and is unlikely to be standardized
# in any way. We just have to rely on our defaults in this case.
self.event = {}
trace_parent = TraceParent.from_headers(self.event.get("headers") or {})

headers = self.event.get("headers") or {}
if headers and should_normalize_headers(self.event):
normalized_headers = {k.lower(): v for k, v in headers.items()}
else:
normalized_headers = headers
trace_parent = TraceParent.from_headers(normalized_headers)

global COLD_START
cold_start = COLD_START
Expand Down
1 change: 1 addition & 0 deletions tests/contrib/serverless/aws_elb_test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"connection": "Keep-Alive",
"host": "blabla.com",
"user-agent": "Apache-HttpClient/4.5.13 (Java/11.0.15)",
"TraceParent": "00-12345678901234567890123456789012-1234567890123456-01",
"x-amzn-trace-id": "Root=1-xxxxxxxxxxxxxx",
"x-forwarded-for": "199.99.99.999",
"x-forwarded-port": "443",
Expand Down
22 changes: 21 additions & 1 deletion tests/contrib/serverless/aws_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@

from elasticapm import capture_span
from elasticapm.conf import constants
from elasticapm.contrib.serverless.aws import capture_serverless, get_data_from_request, get_data_from_response
from elasticapm.contrib.serverless.aws import (
capture_serverless,
get_data_from_request,
get_data_from_response,
should_normalize_headers,
)


@pytest.fixture
Expand Down Expand Up @@ -300,6 +305,7 @@ def test_func(event, context):
assert transaction["context"]["request"]["headers"]
assert transaction["context"]["response"]["status_code"] == 200
assert transaction["context"]["service"]["origin"]["name"] == "lambda-279XGJDqGZ5rsrHC2Fjr"
assert transaction["trace_id"] == "12345678901234567890123456789012"


def test_capture_serverless_s3(event_s3, context, elasticapm_client):
Expand Down Expand Up @@ -477,3 +483,17 @@ def test_func(event, context):

test_func(event_api2, context)
assert len(elasticapm_client.events[constants.TRANSACTION]) == 1


def test_should_normalize_headers_true(event_api, event_elb):
assert should_normalize_headers(event_api) is True
assert should_normalize_headers(event_elb) is True


def test_should_normalize_headers_false(event_api2, event_lurl, event_s3, event_s3_batch, event_sqs, event_sns):
assert should_normalize_headers(event_api2) is False
assert should_normalize_headers(event_lurl) is False
assert should_normalize_headers(event_s3) is False
assert should_normalize_headers(event_s3_batch) is False
assert should_normalize_headers(event_sqs) is False
assert should_normalize_headers(event_sns) is False