Fix exception handling for events with requestContext (#2418)

* Fix exception handling for events with requestContext

* added entry to changelog

* reformatted with black

---------

Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
This commit is contained in:
Alessandro Bologna
2024-04-25 11:57:39 -04:00
committed by GitHub
parent 5375acf534
commit d5b5925cf8
3 changed files with 28 additions and 0 deletions

View File

@ -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)) ([#2363](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2363))
- `opentelemetry-instrumentation-boto3sqs` Instrument Session and resource - `opentelemetry-instrumentation-boto3sqs` Instrument Session and resource
([#2161](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2161)) ([#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 - Use sqlalchemy version in sqlalchemy commenter instead of opentelemetry library version
([#2404](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2404)) ([#2404](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2404))

View File

@ -365,6 +365,7 @@ def _instrument(
) )
exception = None exception = None
result = None
try: try:
result = call_wrapped(*args, **kwargs) result = call_wrapped(*args, **kwargs)
except Exception as exc: # pylint: disable=W0703 except Exception as exc: # pylint: disable=W0703

View File

@ -436,6 +436,31 @@ class TestAwsLambdaInstrumentor(TestBase):
exc_env_patch.stop() 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): def test_uninstrument(self):
AwsLambdaInstrumentor().instrument() AwsLambdaInstrumentor().instrument()