@@ -179,8 +179,6 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
179
179
from functools import partial
180
180
from typing import TYPE_CHECKING , Any , Collection
181
181
182
- from starlette import applications
183
- from starlette .routing import Match
184
182
from wrapt import wrap_function_wrapper
185
183
186
184
from opentelemetry .instrumentation .asgi import OpenTelemetryMiddleware
@@ -218,11 +216,11 @@ class StarletteInstrumentor(BaseInstrumentor):
218
216
See `BaseInstrumentor`.
219
217
"""
220
218
221
- _instrumented_starlette_apps : set [ applications . Starlette ] = set ()
219
+ _instrumented_starlette_apps = set ()
222
220
223
221
@staticmethod
224
222
def instrument_app (
225
- app : applications . Starlette ,
223
+ app ,
226
224
server_request_hook : ServerRequestHook = None ,
227
225
client_request_hook : ClientRequestHook = None ,
228
226
client_response_hook : ClientResponseHook = None ,
@@ -253,7 +251,7 @@ def instrument_app(
253
251
)
254
252
255
253
@staticmethod
256
- def uninstrument_app (app : applications . Starlette ):
254
+ def uninstrument_app (app ):
257
255
app .user_middleware = [
258
256
x
259
257
for x in app .user_middleware
@@ -308,6 +306,8 @@ def instrumented_init(
308
306
309
307
return result
310
308
309
+ from starlette import applications
310
+
311
311
# Wrap Starlette's __init__ method to add instrumentation
312
312
wrap_function_wrapper (
313
313
applications .Starlette ,
@@ -326,11 +326,13 @@ def _uninstrument(self, **kwargs: Any):
326
326
for app in list (StarletteInstrumentor ._instrumented_starlette_apps ):
327
327
self .uninstrument_app (app )
328
328
329
+ from starlette import applications
330
+
329
331
unwrap (applications .Starlette , "__init__" )
330
332
331
333
@staticmethod
332
334
def _add_instrumentation_middleware (
333
- app : applications . Starlette ,
335
+ app ,
334
336
tracer ,
335
337
meter ,
336
338
server_request_hook ,
@@ -366,6 +368,8 @@ def _get_route_details(scope: dict[str, Any]) -> str | None:
366
368
Returns:
367
369
The path to the route if found, otherwise None.
368
370
"""
371
+ from starlette .routing import Match
372
+
369
373
app = scope ["app" ]
370
374
route : str | None = None
371
375
0 commit comments