@@ -177,6 +177,7 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
177
177
from __future__ import annotations
178
178
179
179
from typing import TYPE_CHECKING , Any , Collection , cast
180
+ from weakref import WeakSet
180
181
181
182
from starlette import applications
182
183
from starlette .routing import Match
@@ -239,7 +240,7 @@ def instrument_app(
239
240
meter_provider ,
240
241
schema_url = "https://opentelemetry.io/schemas/1.11.0" ,
241
242
)
242
- if not getattr (app , "is_instrumented_by_opentelemetry " , False ):
243
+ if not getattr (app , "_is_instrumented_by_opentelemetry " , False ):
243
244
app .add_middleware (
244
245
OpenTelemetryMiddleware ,
245
246
excluded_urls = _excluded_urls ,
@@ -251,11 +252,10 @@ def instrument_app(
251
252
tracer = tracer ,
252
253
meter = meter ,
253
254
)
254
- app .is_instrumented_by_opentelemetry = True
255
+ app ._is_instrumented_by_opentelemetry = True
255
256
256
257
# 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 )
259
259
260
260
@staticmethod
261
261
def uninstrument_app (app : applications .Starlette ):
@@ -300,7 +300,7 @@ class _InstrumentedStarlette(applications.Starlette):
300
300
_server_request_hook : ServerRequestHook = None
301
301
_client_request_hook : ClientRequestHook = None
302
302
_client_response_hook : ClientResponseHook = None
303
- _instrumented_starlette_apps : set [applications .Starlette ] = set ()
303
+ _instrumented_starlette_apps : WeakSet [applications .Starlette ] = WeakSet ()
304
304
305
305
def __init__ (self , * args : Any , ** kwargs : Any ):
306
306
super ().__init__ (* args , ** kwargs )
@@ -331,9 +331,6 @@ def __init__(self, *args: Any, **kwargs: Any):
331
331
# adding apps to set for uninstrumenting
332
332
_InstrumentedStarlette ._instrumented_starlette_apps .add (self )
333
333
334
- def __del__ (self ):
335
- _InstrumentedStarlette ._instrumented_starlette_apps .discard (self )
336
-
337
334
338
335
def _get_route_details (scope : dict [str , Any ]) -> str | None :
339
336
"""
0 commit comments