Skip to content

Commit b16709e

Browse files
DivModeemdnetotammy-baylis-swixrmx
authored
Fix ImportError with slash-delimited Lambda handler paths (#3894)
* Fix ImportError with slash-delimited Lambda handler paths AWS Lambda accepts handler paths in both formats: - Slash-delimited: python/functions/api.handler - Dot-delimited: python.functions.api.handler The instrumentation was failing when slash-delimited paths were used because it attempted to import invalid Python module paths containing forward slashes. This fix normalizes the handler path by converting slashes to dots before splitting the module and function names. Fixes #1465 * Add changelog and unit test for slash-delimited handler fix - Add changelog entry in Unreleased section - Add unit test verifying both slash and dot-delimited handler paths work - Test confirms instrumentation handles both formats correctly Related to #1465 * Update CHANGELOG.md Co-authored-by: Tammy Baylis <[email protected]> --------- Co-authored-by: Emídio Neto <[email protected]> Co-authored-by: Tammy Baylis <[email protected]> Co-authored-by: Riccardo Magliocchetti <[email protected]>
1 parent e63e626 commit b16709e

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424

2525
- `opentelemetry-instrumentation-botocore`: Handle dict input in _decode_tool_use for Bedrock streaming
2626
([#3875](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3875))
27+
- `opentelemetry-instrumentation-aws-lambda`: Fix ImportError with slash-delimited handler paths
28+
([#3894](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3894))
2729

2830
## Version 1.38.0/0.59b0 (2025-10-16)
2931

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ def _instrument(self, **kwargs):
460460
)
461461
return
462462
# pylint: disable=attribute-defined-outside-init
463+
# Convert slash-delimited paths to dot-delimited for valid Python imports
464+
lambda_handler = lambda_handler.replace("/", ".")
463465
(
464466
self._wrapped_module_name,
465467
self._wrapped_function_name,

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,53 @@ def test_sqs_event_sets_attributes(self):
727727
MOCK_LAMBDA_CONTEXT_ATTRIBUTES,
728728
)
729729

730+
def test_slash_delimited_handler_path(self):
731+
"""Test that slash-delimited handler paths work correctly.
732+
733+
AWS Lambda accepts both slash-delimited (python/functions/api.handler)
734+
and dot-delimited (python.functions.api.handler) handler paths.
735+
This test ensures the instrumentation handles both formats.
736+
"""
737+
# Test slash-delimited format
738+
slash_env_patch = mock.patch.dict(
739+
"os.environ",
740+
{_HANDLER: "tests/mocks/lambda_function.handler"},
741+
)
742+
slash_env_patch.start()
743+
AwsLambdaInstrumentor().instrument()
744+
745+
mock_execute_lambda()
746+
747+
spans = self.memory_exporter.get_finished_spans()
748+
self.assertEqual(len(spans), 1)
749+
self.assertSpanHasAttributes(
750+
spans[0],
751+
MOCK_LAMBDA_CONTEXT_ATTRIBUTES,
752+
)
753+
754+
slash_env_patch.stop()
755+
AwsLambdaInstrumentor().uninstrument()
756+
self.memory_exporter.clear()
757+
758+
# Test dot-delimited format (should still work)
759+
dot_env_patch = mock.patch.dict(
760+
"os.environ",
761+
{_HANDLER: "tests.mocks.lambda_function.handler"},
762+
)
763+
dot_env_patch.start()
764+
AwsLambdaInstrumentor().instrument()
765+
766+
mock_execute_lambda()
767+
768+
spans = self.memory_exporter.get_finished_spans()
769+
self.assertEqual(len(spans), 1)
770+
self.assertSpanHasAttributes(
771+
spans[0],
772+
MOCK_LAMBDA_CONTEXT_ATTRIBUTES,
773+
)
774+
775+
dot_env_patch.stop()
776+
730777
def test_lambda_handles_handler_exception_with_api_gateway_proxy_event(
731778
self,
732779
):

0 commit comments

Comments
 (0)