Skip to content

Commit 41f1ccc

Browse files
committed
fix
1 parent 3d8dd4f commit 41f1ccc

File tree

2 files changed

+13
-41
lines changed

2 files changed

+13
-41
lines changed

instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
203203
from opentelemetry.instrumentation.fastapi.package import _instruments
204204
from opentelemetry.instrumentation.fastapi.version import __version__
205205
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
206-
from opentelemetry.metrics import Meter, get_meter
206+
from opentelemetry.metrics import MeterProvider, get_meter
207207
from opentelemetry.semconv.trace import SpanAttributes
208-
from opentelemetry.trace import Tracer, get_tracer
208+
from opentelemetry.trace import TracerProvider, get_tracer
209209
from opentelemetry.util.http import (
210210
get_excluded_urls,
211211
parse_excluded_urls,
@@ -230,8 +230,8 @@ def instrument_app(
230230
server_request_hook: ServerRequestHook = None,
231231
client_request_hook: ClientRequestHook = None,
232232
client_response_hook: ClientResponseHook = None,
233-
tracer_provider: Tracer | None = None,
234-
meter_provider: Meter | None = None,
233+
tracer_provider: TracerProvider | None = None,
234+
meter_provider: MeterProvider | None = None,
235235
excluded_urls: str | None = None,
236236
http_capture_headers_server_request: list[str] | None = None,
237237
http_capture_headers_server_response: list[str] | None = None,
@@ -362,12 +362,7 @@ def _instrument(self, **kwargs):
362362
_InstrumentedFastAPI._http_capture_headers_sanitize_fields = (
363363
kwargs.get("http_capture_headers_sanitize_fields")
364364
)
365-
_excluded_urls = kwargs.get("excluded_urls")
366-
_InstrumentedFastAPI._excluded_urls = (
367-
_excluded_urls_from_env
368-
if _excluded_urls is None
369-
else parse_excluded_urls(_excluded_urls)
370-
)
365+
_InstrumentedFastAPI._excluded_urls = kwargs.get("excluded_urls")
371366
_InstrumentedFastAPI._meter_provider = kwargs.get("meter_provider")
372367
_InstrumentedFastAPI._exclude_spans = kwargs.get("exclude_spans")
373368
fastapi.FastAPI = _InstrumentedFastAPI

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

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ def setUp(self):
170170
self._instrumentor = otel_fastapi.FastAPIInstrumentor()
171171
self._app = self._create_app()
172172
self._app.add_middleware(HTTPSRedirectMiddleware)
173-
self._client = TestClient(self._app)
173+
self._client = TestClient(self._app, base_url="https://testserver:443")
174+
self._client.__enter__() # run the lifespan, initialize the middleware stack
174175

175176
def tearDown(self):
176177
super().tearDown()
@@ -205,19 +206,11 @@ async def _(param: str):
205206
async def _():
206207
return {"message": "ok"}
207208

208-
@app.get("/error")
209-
async def _():
210-
raise UnhandledException("This is an unhandled exception")
211-
212209
app.mount("/sub", app=sub_app)
213210

214211
return app
215212

216213

217-
class UnhandledException(Exception):
218-
pass
219-
220-
221214
class TestBaseManualFastAPI(TestBaseFastAPI):
222215
@classmethod
223216
def setUpClass(cls):
@@ -406,26 +399,6 @@ def test_fastapi_excluded_urls(self):
406399
spans = self.memory_exporter.get_finished_spans()
407400
self.assertEqual(len(spans), 0)
408401

409-
def test_fastapi_unhandled_exception(self):
410-
"""If the application has an unhandled error the instrumentation should capture that a 500 response is returned."""
411-
try:
412-
self._client.get("/error")
413-
except UnhandledException:
414-
pass
415-
else:
416-
self.fail("Expected UnhandledException")
417-
418-
spans = self.memory_exporter.get_finished_spans()
419-
self.assertEqual(len(spans), 3)
420-
for span in spans:
421-
self.assertIn("GET /error", span.name)
422-
self.assertEqual(
423-
span.attributes[SpanAttributes.HTTP_ROUTE], "/error"
424-
)
425-
self.assertEqual(
426-
span.attributes[SpanAttributes.HTTP_STATUS_CODE], 500
427-
)
428-
429402
def test_fastapi_excluded_urls_not_env(self):
430403
"""Ensure that given fastapi routes are excluded when passed explicitly (not in the environment)"""
431404
app = self._create_app_explicit_excluded_urls()
@@ -1152,9 +1125,13 @@ def test_request(self):
11521125
def test_mulitple_way_instrumentation(self):
11531126
self._instrumentor.instrument_app(self._app)
11541127
count = 0
1155-
for middleware in self._app.user_middleware:
1156-
if middleware.cls is OpenTelemetryMiddleware:
1128+
app = self._app.middleware_stack
1129+
while True:
1130+
if isinstance(app, OpenTelemetryMiddleware):
11571131
count += 1
1132+
if app is None:
1133+
break
1134+
app = getattr(app, "app", None)
11581135
self.assertEqual(count, 1)
11591136

11601137
def test_uninstrument_after_instrument(self):

0 commit comments

Comments
 (0)