mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-08-05 22:09:56 +08:00
AwsLambdaInstrumentor handles and re-raises handler function exception (#2245)
This commit is contained in:
@ -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")
|
||||
|
@ -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()
|
||||
|
||||
|
Reference in New Issue
Block a user