Skip to content

Commit

Permalink
Add lambda test
Browse files Browse the repository at this point in the history
  • Loading branch information
tammy-baylis-swi committed Feb 16, 2024
1 parent 33ac032 commit d6f3c1b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit d6f3c1b

Please sign in to comment.