Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AwsLambdaInstrumentor handles and re-raises handler function exception #2245

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add lambda test
tammy-baylis-swi committed Feb 16, 2024
commit d6f3c1b4be4433f47eb8d1bd792d0e8d52ef6275
Original file line number Diff line number Diff line change
@@ -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")
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
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 @@ def test_lambda_handles_list_event(self):

assert spans

def test_lambda_handles_handler_exception(self):
self.exc_env_patch = mock.patch.dict(
"os.environ",
{_HANDLER: "tests.mocks.lambda_function.handler_exc"},
)
self.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")

self.exc_env_patch.stop()

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