From 24e2981b9575b5fdeb441f17aedf37fff3799bac Mon Sep 17 00:00:00 2001 From: Evan Pavlica Date: Thu, 30 Mar 2023 13:08:51 -0700 Subject: [PATCH 1/2] Prevent AttributeError from being raised when lambda event is a list rather than a dict --- CHANGELOG.md | 5 +++++ .../opentelemetry/instrumentation/aws_lambda/__init__.py | 2 +- .../tests/test_aws_lambda_instrumentation_manual.py | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce84cd8a67..c173a1f896 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Fixed + +- Fix `AttributeError` when AWS Lambda handler receives a list event + ([#1738](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1738)) + ## Version 1.17.0/0.38b0 (2023-03-22) ### Added diff --git a/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py b/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py index 9385400494..5ab2782aca 100644 --- a/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py @@ -342,7 +342,7 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches # If the request came from an API Gateway, extract http attributes from the event # https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md#api-gateway # https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-server-semantic-conventions - if lambda_event and lambda_event.get("requestContext"): + if isinstance(lambda_event, dict) and lambda_event.get("requestContext"): span.set_attribute(SpanAttributes.FAAS_TRIGGER, "http") if lambda_event.get("version") == "2.0": diff --git a/instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py b/instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py index fd3c9f88ca..1df7499d31 100644 --- a/instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py +++ b/instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py @@ -399,6 +399,15 @@ def test_api_gateway_http_api_proxy_event_sets_attributes(self): }, ) + def test_lambda_handles_list_event(self): + AwsLambdaInstrumentor().instrument() + + mock_execute_lambda([{"message": "test"}]) + + spans = self.memory_exporter.get_finished_spans() + + assert spans + def test_uninstrument(self): AwsLambdaInstrumentor().instrument() From d815b3426ad21581221d746ae55e909aac00927e Mon Sep 17 00:00:00 2001 From: Evan Pavlica Date: Fri, 31 Mar 2023 10:22:50 -0700 Subject: [PATCH 2/2] Satisfy linter not sure how I missed this when I first ran tox. Black is frustratingly opinionated for my taste. --- .../src/opentelemetry/instrumentation/aws_lambda/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py b/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py index 5ab2782aca..4404839c66 100644 --- a/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py @@ -342,7 +342,9 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches # If the request came from an API Gateway, extract http attributes from the event # https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md#api-gateway # https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-server-semantic-conventions - if isinstance(lambda_event, dict) and lambda_event.get("requestContext"): + if isinstance(lambda_event, dict) and lambda_event.get( + "requestContext" + ): span.set_attribute(SpanAttributes.FAAS_TRIGGER, "http") if lambda_event.get("version") == "2.0":