Skip to content
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

refactor: avoid exception when lambda_handler envs not present in aws-lambda instrumentation #2750

Merged
Show file tree
Hide file tree
Changes from 9 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +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.
Ronald-TR marked this conversation as resolved.
Show resolved Hide resolved
([#2750](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2750))

## 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -422,6 +422,14 @@ def _instrument(self, **kwargs):
request.
"""
lambda_handler = os.environ.get(ORIG_HANDLER, os.environ.get(_HANDLER))
if not lambda_handler:
emdneto marked this conversation as resolved.
Show resolved Hide resolved
logger.warning(
(
"Could not find the ORIG_HANDLER or _HANDLER in the environment variables. ",
"Is the instrumentation running inside an AWS Lambda?",
Ronald-TR marked this conversation as resolved.
Show resolved Hide resolved
)
)
return
# pylint: disable=attribute-defined-outside-init
(
self._wrapped_module_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,20 @@ def test_lambda_handles_handler_exception_with_api_gateway_proxy_event(

exc_env_patch.stop()

def test_lambda_handles_should_do_nothing_when_environment_variables_not_present(
self,
):
exc_env_patch = mock.patch.dict(
"os.environ",
{_HANDLER: ""},
)
exc_env_patch.start()
AwsLambdaInstrumentor().instrument()

spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)
exc_env_patch.stop()

def test_uninstrument(self):
AwsLambdaInstrumentor().instrument()

Expand Down