@@ -177,6 +177,7 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
177177from __future__ import annotations
178178
179179from typing import TYPE_CHECKING , Any , Collection , cast
180+ from weakref import WeakSet
180181
181182from starlette import applications
182183from starlette .routing import Match
@@ -239,7 +240,7 @@ def instrument_app(
239240 meter_provider ,
240241 schema_url = "https://opentelemetry.io/schemas/1.11.0" ,
241242 )
242- if not getattr (app , "is_instrumented_by_opentelemetry " , False ):
243+ if not getattr (app , "_is_instrumented_by_opentelemetry " , False ):
243244 app .add_middleware (
244245 OpenTelemetryMiddleware ,
245246 excluded_urls = _excluded_urls ,
@@ -251,11 +252,10 @@ def instrument_app(
251252 tracer = tracer ,
252253 meter = meter ,
253254 )
254- app .is_instrumented_by_opentelemetry = True
255+ app ._is_instrumented_by_opentelemetry = True
255256
256257 # adding apps to set for uninstrumenting
257- if app not in _InstrumentedStarlette ._instrumented_starlette_apps :
258- _InstrumentedStarlette ._instrumented_starlette_apps .add (app )
258+ _InstrumentedStarlette ._instrumented_starlette_apps .add (app )
259259
260260 @staticmethod
261261 def uninstrument_app (app : applications .Starlette ):
@@ -300,7 +300,7 @@ class _InstrumentedStarlette(applications.Starlette):
300300 _server_request_hook : ServerRequestHook = None
301301 _client_request_hook : ClientRequestHook = None
302302 _client_response_hook : ClientResponseHook = None
303- _instrumented_starlette_apps : set [applications .Starlette ] = set ()
303+ _instrumented_starlette_apps : WeakSet [applications .Starlette ] = WeakSet ()
304304
305305 def __init__ (self , * args : Any , ** kwargs : Any ):
306306 super ().__init__ (* args , ** kwargs )
@@ -331,9 +331,6 @@ def __init__(self, *args: Any, **kwargs: Any):
331331 # adding apps to set for uninstrumenting
332332 _InstrumentedStarlette ._instrumented_starlette_apps .add (self )
333333
334- def __del__ (self ):
335- _InstrumentedStarlette ._instrumented_starlette_apps .discard (self )
336-
337334
338335def _get_route_details (scope : dict [str , Any ]) -> str | None :
339336 """
0 commit comments