Skip to content

Commit 4c9c03f

Browse files
committed
Add lazy loading
1 parent 6b67910 commit 4c9c03f

File tree

1 file changed

+10
-6
lines changed
  • instrumentation/opentelemetry-instrumentation-starlette/src/opentelemetry/instrumentation/starlette

1 file changed

+10
-6
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,6 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
179179
from functools import partial
180180
from typing import TYPE_CHECKING, Any, Collection
181181

182-
from starlette import applications
183-
from starlette.routing import Match
184182
from wrapt import wrap_function_wrapper
185183

186184
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
@@ -218,11 +216,11 @@ class StarletteInstrumentor(BaseInstrumentor):
218216
See `BaseInstrumentor`.
219217
"""
220218

221-
_instrumented_starlette_apps: set[applications.Starlette] = set()
219+
_instrumented_starlette_apps = set()
222220

223221
@staticmethod
224222
def instrument_app(
225-
app: applications.Starlette,
223+
app,
226224
server_request_hook: ServerRequestHook = None,
227225
client_request_hook: ClientRequestHook = None,
228226
client_response_hook: ClientResponseHook = None,
@@ -253,7 +251,7 @@ def instrument_app(
253251
)
254252

255253
@staticmethod
256-
def uninstrument_app(app: applications.Starlette):
254+
def uninstrument_app(app):
257255
app.user_middleware = [
258256
x
259257
for x in app.user_middleware
@@ -308,6 +306,8 @@ def instrumented_init(
308306

309307
return result
310308

309+
from starlette import applications
310+
311311
# Wrap Starlette's __init__ method to add instrumentation
312312
wrap_function_wrapper(
313313
applications.Starlette,
@@ -326,11 +326,13 @@ def _uninstrument(self, **kwargs: Any):
326326
for app in list(StarletteInstrumentor._instrumented_starlette_apps):
327327
self.uninstrument_app(app)
328328

329+
from starlette import applications
330+
329331
unwrap(applications.Starlette, "__init__")
330332

331333
@staticmethod
332334
def _add_instrumentation_middleware(
333-
app: applications.Starlette,
335+
app,
334336
tracer,
335337
meter,
336338
server_request_hook,
@@ -366,6 +368,8 @@ def _get_route_details(scope: dict[str, Any]) -> str | None:
366368
Returns:
367369
The path to the route if found, otherwise None.
368370
"""
371+
from starlette.routing import Match
372+
369373
app = scope["app"]
370374
route: str | None = None
371375

0 commit comments

Comments
 (0)