1717
1818import fastapi
1919
20- from opentelemetry .instrumentation .fastapi import FastAPIInstrumentor
20+ from opentelemetry .instrumentation .fastapi import (
21+ FastAPIInstrumentor ,
22+ _InstrumentedFastAPI ,
23+ )
2124
2225
2326class TestFastAPIMemoryLeak (unittest .TestCase ):
@@ -43,8 +46,6 @@ def test_refcount_after_uninstrument(self):
4346 )
4447
4548 # Verify that the app was removed from the set
46- from opentelemetry .instrumentation .fastapi import _InstrumentedFastAPI
47-
4849 self .assertNotIn (
4950 app ,
5051 _InstrumentedFastAPI ._instrumented_fastapi_apps ,
@@ -58,7 +59,7 @@ def test_multiple_instrument_uninstrument_cycles(self):
5859 initial_refcount = sys .getrefcount (app )
5960
6061 # Perform multiple instrument/uninstrument cycles
61- for i in range (5 ):
62+ for cycle_num in range (5 ):
6263 FastAPIInstrumentor .instrument_app (app )
6364 FastAPIInstrumentor .uninstrument_app (app )
6465
@@ -70,12 +71,10 @@ def test_multiple_instrument_uninstrument_cycles(self):
7071 final_refcount ,
7172 initial_refcount
7273 + 2 , # Allow small increase due to Python internals
73- f"Refcount after { i + 1 } instrument/uninstrument cycles should not grow significantly" ,
74+ f"Refcount after { cycle_num + 1 } instrument/uninstrument cycles should not grow significantly" ,
7475 )
7576
7677 # Verify that the app is not in the set
77- from opentelemetry .instrumentation .fastapi import _InstrumentedFastAPI
78-
7978 self .assertNotIn (
8079 app ,
8180 _InstrumentedFastAPI ._instrumented_fastapi_apps ,
@@ -96,18 +95,16 @@ def test_multiple_apps_instrument_uninstrument(self):
9695 FastAPIInstrumentor .uninstrument_app (app )
9796
9897 # Check that refcounts are not significantly increased
99- for i , app in enumerate (apps ):
98+ for app_idx , app in enumerate (apps ):
10099 final_refcount = sys .getrefcount (app )
101100 self .assertLessEqual (
102101 final_refcount ,
103- initial_refcounts [i ]
102+ initial_refcounts [app_idx ]
104103 + 2 , # Allow small increase due to Python internals
105- f"App { i } refcount should not grow significantly" ,
104+ f"App { app_idx } refcount should not grow significantly" ,
106105 )
107106
108107 # Verify that no apps are in the set
109- from opentelemetry .instrumentation .fastapi import _InstrumentedFastAPI
110-
111108 for app in apps :
112109 self .assertNotIn (
113110 app ,
@@ -123,7 +120,6 @@ def test_demonstrate_fix(self):
123120 # After the fix: app should be removed from _instrumented_fastapi_apps
124121
125122 FastAPIInstrumentor .instrument_app (app )
126- from opentelemetry .instrumentation .fastapi import _InstrumentedFastAPI
127123
128124 # Verify app is in the set after instrumentation
129125 self .assertIn (app , _InstrumentedFastAPI ._instrumented_fastapi_apps )
0 commit comments