AwsLambdaInstrumentor handles and re-raises handler function exception (#2245)

This commit is contained in:
Tammy Baylis
2024-02-22 09:16:34 -08:00
committed by GitHub
parent 2518a4ac07
commit efb327d4d7
4 changed files with 41 additions and 2 deletions

View File

@ -19,3 +19,7 @@ def handler(event, context):
def rest_api_handler(event, context):
return {"statusCode": 200, "body": "200 ok"}
def handler_exc(event, context):
raise Exception("500 internal server error")

View File

@ -40,7 +40,7 @@ from opentelemetry.propagators.aws.aws_xray_propagator import (
from opentelemetry.semconv.resource import ResourceAttributes
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import NoOpTracerProvider, SpanKind
from opentelemetry.trace import NoOpTracerProvider, SpanKind, StatusCode
from opentelemetry.trace.propagation.tracecontext import (
TraceContextTextMapPropagator,
)
@ -410,6 +410,27 @@ class TestAwsLambdaInstrumentor(TestBase):
assert spans
def test_lambda_handles_handler_exception(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()
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()