From 7a8b0848fc711bf3675257201e989be8cbeca13b Mon Sep 17 00:00:00 2001 From: Ronald Rodrigues Farias Date: Mon, 29 Jul 2024 12:16:21 -0300 Subject: [PATCH 01/10] refactor: raise exception when lambda_handler envs not present in aws-lambda instrumentation --- .../instrumentation/aws_lambda/__init__.py | 7 +++++++ .../test_aws_lambda_instrumentation_manual.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) 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 c320c12bde..29a5b53bb5 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 @@ -422,6 +422,13 @@ def _instrument(self, **kwargs): request. """ lambda_handler = os.environ.get(ORIG_HANDLER, os.environ.get(_HANDLER)) + if not lambda_handler: + raise ValueError( + ( + "Could not find the ORIG_HANDLER or _HANDLER in the environment variables. ", + "This instrumentation is runnning inside an aws lambda?", + ) + ) # pylint: disable=attribute-defined-outside-init ( self._wrapped_module_name, 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 9f25524e43..66f637ee2b 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 @@ -495,6 +495,22 @@ def test_lambda_handles_handler_exception_with_api_gateway_proxy_event( exc_env_patch.stop() + def test_lambda_handles_raises_exception_when_environment_variables_not_present( + self, + ): + exc_env_patch = mock.patch.dict( + "os.environ", + {_HANDLER: ""}, + ) + exc_env_patch.start() + + with self.assertRaises(Exception): + AwsLambdaInstrumentor().instrument() + + spans = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans), 0) + exc_env_patch.stop() + def test_uninstrument(self): AwsLambdaInstrumentor().instrument() From b644506e3e5140730d73c16b5ae30262cb326c78 Mon Sep 17 00:00:00 2001 From: Ronald Rodrigues Farias Date: Mon, 29 Jul 2024 17:20:18 -0300 Subject: [PATCH 02/10] refactor: do nothing with aws-lambda instrumentation when code is running outside a lambda --- .../opentelemetry/instrumentation/aws_lambda/__init__.py | 3 ++- .../tests/test_aws_lambda_instrumentation_manual.py | 6 ++---- 2 files changed, 4 insertions(+), 5 deletions(-) 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 29a5b53bb5..33a3b90391 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 @@ -423,12 +423,13 @@ def _instrument(self, **kwargs): """ lambda_handler = os.environ.get(ORIG_HANDLER, os.environ.get(_HANDLER)) if not lambda_handler: - raise ValueError( + logger.warning( ( "Could not find the ORIG_HANDLER or _HANDLER in the environment variables. ", "This instrumentation is runnning inside an aws lambda?", ) ) + return # pylint: disable=attribute-defined-outside-init ( self._wrapped_module_name, 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 66f637ee2b..00940547ea 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 @@ -495,7 +495,7 @@ def test_lambda_handles_handler_exception_with_api_gateway_proxy_event( exc_env_patch.stop() - def test_lambda_handles_raises_exception_when_environment_variables_not_present( + def test_lambda_handles_should_do_nothing_when_environment_variables_not_present( self, ): exc_env_patch = mock.patch.dict( @@ -503,9 +503,7 @@ def test_lambda_handles_raises_exception_when_environment_variables_not_present( {_HANDLER: ""}, ) exc_env_patch.start() - - with self.assertRaises(Exception): - AwsLambdaInstrumentor().instrument() + AwsLambdaInstrumentor().instrument() spans = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans), 0) From 2add1602897df3615d8e9f4c1ecb079d61d805e5 Mon Sep 17 00:00:00 2001 From: Ronald Rodrigues Farias Date: Mon, 29 Jul 2024 17:24:49 -0300 Subject: [PATCH 03/10] fix: see more link in docstring --- .../src/opentelemetry/instrumentation/aws_lambda/__init__.py | 2 +- 1 file changed, 1 insertion(+), 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 33a3b90391..fa62dfbb58 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 @@ -410,7 +410,7 @@ def _instrument(self, **kwargs): """Instruments Lambda Handlers on AWS Lambda. See more: - https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md#instrumenting-aws-lambda + https://github.com/open-telemetry/semantic-conventions/blob/main/docs/faas/aws-lambda.md Args: **kwargs: Optional arguments From fcd10e78352a368fbf49dca76fef2ba6fcc85db1 Mon Sep 17 00:00:00 2001 From: Ronald Rodrigues Farias Date: Mon, 29 Jul 2024 18:24:14 -0300 Subject: [PATCH 04/10] docs: add changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7924d9211e..68fc553622 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Version 1.26.0/0.47b0 (2024-07-23) ### Added - +- `opentelemetry-instrumentation-aws-lambda` Avoid exception when a handler is not present. + ([#2750](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2750)) - `opentelemetry-instrumentation-flask` Add `http.route` and `http.target` to metric attributes ([#2621](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2621)) - `opentelemetry-instrumentation-aws-lambda` Enable global propagator for AWS instrumentation From 9371f4100b681927153dac6db28c8bf5b08e767b Mon Sep 17 00:00:00 2001 From: Ronald Rodrigues Farias Date: Mon, 29 Jul 2024 18:36:35 -0300 Subject: [PATCH 05/10] docs: fix english issues --- .../src/opentelemetry/instrumentation/aws_lambda/__init__.py | 2 +- 1 file changed, 1 insertion(+), 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 fa62dfbb58..0f82d19343 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 @@ -426,7 +426,7 @@ def _instrument(self, **kwargs): logger.warning( ( "Could not find the ORIG_HANDLER or _HANDLER in the environment variables. ", - "This instrumentation is runnning inside an aws lambda?", + "Is the instrumentation runnning inside an AWS Lambda?", ) ) return From 0bff70e2e4f63dab6e450f270331fdd368466cd8 Mon Sep 17 00:00:00 2001 From: Ronald Rodrigues Farias Date: Mon, 29 Jul 2024 18:37:20 -0300 Subject: [PATCH 06/10] docs: fix english issues --- .../src/opentelemetry/instrumentation/aws_lambda/__init__.py | 2 +- 1 file changed, 1 insertion(+), 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 0f82d19343..93e3d09335 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 @@ -426,7 +426,7 @@ def _instrument(self, **kwargs): logger.warning( ( "Could not find the ORIG_HANDLER or _HANDLER in the environment variables. ", - "Is the instrumentation runnning inside an AWS Lambda?", + "Is the instrumentation running inside an AWS Lambda?", ) ) return From 4be2056a76f4d745bceadf1a7ddf297f8faf2084 Mon Sep 17 00:00:00 2001 From: Ronald Rodrigues Farias Date: Mon, 29 Jul 2024 18:42:11 -0300 Subject: [PATCH 07/10] docs: fix changelog session --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68fc553622..70096e82bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,12 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## Unreleased +- `opentelemetry-instrumentation-aws-lambda` Avoid exception when a handler is not present. + ([#2750](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2750)) ## Version 1.26.0/0.47b0 (2024-07-23) ### Added -- `opentelemetry-instrumentation-aws-lambda` Avoid exception when a handler is not present. - ([#2750](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2750)) - `opentelemetry-instrumentation-flask` Add `http.route` and `http.target` to metric attributes ([#2621](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2621)) - `opentelemetry-instrumentation-aws-lambda` Enable global propagator for AWS instrumentation From 630e0abb7b67817c9911eae726ce8bef5580e7a3 Mon Sep 17 00:00:00 2001 From: Ronald Date: Mon, 29 Jul 2024 23:05:48 -0300 Subject: [PATCH 08/10] Update CHANGELOG.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Emídio Neto <9735060+emdneto@users.noreply.github.com> --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70096e82bd..66050addc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## Unreleased + +## Fixed + - `opentelemetry-instrumentation-aws-lambda` Avoid exception when a handler is not present. ([#2750](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2750)) From 03e7c7d7cd197307fa9705dae7612dc3d30c615b Mon Sep 17 00:00:00 2001 From: Ronald Date: Tue, 30 Jul 2024 07:11:49 -0300 Subject: [PATCH 09/10] Update instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py Co-authored-by: Riccardo Magliocchetti --- .../src/opentelemetry/instrumentation/aws_lambda/__init__.py | 2 +- 1 file changed, 1 insertion(+), 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 93e3d09335..883296d85b 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 @@ -426,7 +426,7 @@ def _instrument(self, **kwargs): logger.warning( ( "Could not find the ORIG_HANDLER or _HANDLER in the environment variables. ", - "Is the instrumentation running inside an AWS Lambda?", + "This instrumentation requires the OpenTelemetry Lambda extension installed.", ) ) return From 9819673cb7817b6fe9795b9940459b32bd23c44c Mon Sep 17 00:00:00 2001 From: Ronald Rodrigues Farias Date: Tue, 30 Jul 2024 08:49:05 -0300 Subject: [PATCH 10/10] fix: type in empty line --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66050addc4..329f19966f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Version 1.26.0/0.47b0 (2024-07-23) ### Added + - `opentelemetry-instrumentation-flask` Add `http.route` and `http.target` to metric attributes ([#2621](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2621)) - `opentelemetry-instrumentation-aws-lambda` Enable global propagator for AWS instrumentation