Skip to content

Commit 8164887

Browse files
Ronald-TRxrmx
authored andcommitted
refactor: avoid exception when lambda_handler envs not present in aws-lambda instrumentation (open-telemetry#2750)
1 parent e3f5585 commit 8164887

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10-
### Fixed
10+
## Fixed
1111

12+
- `opentelemetry-instrumentation-aws-lambda` Avoid exception when a handler is not present.
13+
([#2750](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2750))
1214
- `opentelemetry-instrumentation-django` Fix regression - `http.target` re-added back to old semconv duration metrics
1315
([#2746](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2746))
1416
- `opentelemetry-instrumentation-grpc` Fixes the issue with the gRPC instrumentation not working with the 1.63.0 and higher version of gRPC

instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def _instrument(self, **kwargs):
410410
"""Instruments Lambda Handlers on AWS Lambda.
411411
412412
See more:
413-
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md#instrumenting-aws-lambda
413+
https://github.com/open-telemetry/semantic-conventions/blob/main/docs/faas/aws-lambda.md
414414
415415
Args:
416416
**kwargs: Optional arguments
@@ -422,6 +422,14 @@ def _instrument(self, **kwargs):
422422
request.
423423
"""
424424
lambda_handler = os.environ.get(ORIG_HANDLER, os.environ.get(_HANDLER))
425+
if not lambda_handler:
426+
logger.warning(
427+
(
428+
"Could not find the ORIG_HANDLER or _HANDLER in the environment variables. ",
429+
"This instrumentation requires the OpenTelemetry Lambda extension installed.",
430+
)
431+
)
432+
return
425433
# pylint: disable=attribute-defined-outside-init
426434
(
427435
self._wrapped_module_name,

instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py

+14
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,20 @@ def test_lambda_handles_handler_exception_with_api_gateway_proxy_event(
495495

496496
exc_env_patch.stop()
497497

498+
def test_lambda_handles_should_do_nothing_when_environment_variables_not_present(
499+
self,
500+
):
501+
exc_env_patch = mock.patch.dict(
502+
"os.environ",
503+
{_HANDLER: ""},
504+
)
505+
exc_env_patch.start()
506+
AwsLambdaInstrumentor().instrument()
507+
508+
spans = self.memory_exporter.get_finished_spans()
509+
self.assertEqual(len(spans), 0)
510+
exc_env_patch.stop()
511+
498512
def test_uninstrument(self):
499513
AwsLambdaInstrumentor().instrument()
500514

0 commit comments

Comments
 (0)