Skip to content

Commit aac8922

Browse files
author
Ronald-TR
committed
refactor: raise exception when lambda_handler envs not present in aws-lambda instrumentation
1 parent c45a620 commit aac8922

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,13 @@ 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+
raise ValueError(
427+
(
428+
"Could not find the ORIG_HANDLER or _HANDLER in the environment variables. ",
429+
"This instrumentation is runnning inside an aws lambda?",
430+
)
431+
)
425432
# pylint: disable=attribute-defined-outside-init
426433
(
427434
self._wrapped_module_name,

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

+16
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,22 @@ def test_lambda_handles_handler_exception_with_api_gateway_proxy_event(
495495

496496
exc_env_patch.stop()
497497

498+
def test_lambda_handles_raises_exception_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+
507+
with self.assertRaises(Exception):
508+
AwsLambdaInstrumentor().instrument()
509+
510+
spans = self.memory_exporter.get_finished_spans()
511+
self.assertEqual(len(spans), 0)
512+
exc_env_patch.stop()
513+
498514
def test_uninstrument(self):
499515
AwsLambdaInstrumentor().instrument()
500516

0 commit comments

Comments
 (0)