Skip to content

Commit

Permalink
Fix exception handling for events with requestContext (open-telemetry…
Browse files Browse the repository at this point in the history
…#2418)

* Fix exception handling for events with requestContext

* added entry to changelog

* reformatted with black

---------

Co-authored-by: Diego Hurtado <[email protected]>
  • Loading branch information
alessandrobologna and ocelotl authored Apr 25, 2024
1 parent 5375acf commit d5b5925
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2363](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2363))
- `opentelemetry-instrumentation-boto3sqs` Instrument Session and resource
([#2161](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2161))
- `opentelemetry-instrumentation-aws-lambda` Fix exception handling for events with requestContext
([#2418](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2418))
- Use sqlalchemy version in sqlalchemy commenter instead of opentelemetry library version
([#2404](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2404))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches
)

exception = None
result = None
try:
result = call_wrapped(*args, **kwargs)
except Exception as exc: # pylint: disable=W0703
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,31 @@ def test_lambda_handles_handler_exception(self):

exc_env_patch.stop()

def test_lambda_handles_handler_exception_with_api_gateway_proxy_event(
self,
):
exc_env_patch = mock.patch.dict(
"os.environ",
{_HANDLER: "tests.mocks.lambda_function.handler_exc"},
)
exc_env_patch.start()
AwsLambdaInstrumentor().instrument()
# instrumentor re-raises the exception
with self.assertRaises(Exception):
mock_execute_lambda(
{"requestContext": {"http": {"method": "GET"}}}
)

spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertEqual(span.status.status_code, StatusCode.ERROR)
self.assertEqual(len(span.events), 1)
event = span.events[0]
self.assertEqual(event.name, "exception")

exc_env_patch.stop()

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

Expand Down

0 comments on commit d5b5925

Please sign in to comment.