Skip to content

Commit 145b2be

Browse files
committed
add test for spans
1 parent 79adade commit 145b2be

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import weakref as _weakref
2121
from contextlib import ExitStack
2222
from timeit import default_timer
23-
from typing import Any, cast
23+
from typing import Any, Final, cast
2424
from unittest.mock import Mock, call, patch
2525

2626
import fastapi
@@ -341,15 +341,29 @@ def test_sub_app_fastapi_call(self):
341341
span.attributes[HTTP_URL],
342342
)
343343

344-
345344
def test_custom_api_router(self):
346345
"""
347346
This test is to ensure that custom API routers the OpenTelemetryMiddleware does not cause issues with
348347
custom API routers that depend on non-standard fields on the ASGI scope.
349348
"""
350-
resp = self._client.get("/custom-router/success")
351-
self.assertEqual(resp.status_code, 200)
352-
349+
resp: Final = self._client.get("/custom-router/success")
350+
spans: Final = self.memory_exporter.get_finished_spans()
351+
expected_span_attributes: Final = {
352+
HTTP_TARGET: "/custom-router/success",
353+
HTTP_URL: "https://testserver/",
354+
}
355+
self.assertEqual(200, resp.status_code)
356+
self.assertTrue(
357+
all(
358+
all(
359+
(
360+
span.attributes[attribute] == expected
361+
for attribute, expected in expected_span_attributes.items()
362+
)
363+
)
364+
for span in spans
365+
)
366+
)
353367

354368
def test_host_fastapi_call(self):
355369
client = TestClient(self._app, base_url="https://testserver2:443")

0 commit comments

Comments
 (0)