Skip to content

Commit 34ed916

Browse files
committed
fix(fastapi-instrumentation): linter fix
1 parent 0b79069 commit 34ed916

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

instrumentation/opentelemetry-instrumentation-fastapi/demo_memory_leak_fix.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,14 @@ def demonstrate_multiple_cycles():
6666
print(f"Initial refcount: {initial_refcount}")
6767

6868
# Perform multiple cycles
69-
for i in range(3):
69+
for cycle_num in range(3):
7070
FastAPIInstrumentor.instrument_app(app)
7171
FastAPIInstrumentor.uninstrument_app(app)
7272
current_refcount = sys.getrefcount(app)
7373
set_size = len(_InstrumentedFastAPI._instrumented_fastapi_apps)
74-
print(f"Cycle {i+1}: refcount={current_refcount}, set_size={set_size}")
74+
print(
75+
f"Cycle {cycle_num+1}: refcount={current_refcount}, set_size={set_size}"
76+
)
7577

7678
final_refcount = sys.getrefcount(app)
7779
final_set_size = len(_InstrumentedFastAPI._instrumented_fastapi_apps)
@@ -92,18 +94,18 @@ def demonstrate_multiple_apps():
9294
apps = [fastapi.FastAPI() for _ in range(3)]
9395

9496
print("Instrumenting all apps...")
95-
for i, app in enumerate(apps):
97+
for app_idx, app in enumerate(apps):
9698
FastAPIInstrumentor.instrument_app(app)
97-
print(f"App {i}: refcount={sys.getrefcount(app)}")
99+
print(f"App {app_idx}: refcount={sys.getrefcount(app)}")
98100

99101
print(
100102
f"Set size after instrumenting: {len(_InstrumentedFastAPI._instrumented_fastapi_apps)}"
101103
)
102104

103105
print("Uninstrumenting all apps...")
104-
for i, app in enumerate(apps):
106+
for app_idx, app in enumerate(apps):
105107
FastAPIInstrumentor.uninstrument_app(app)
106-
print(f"App {i}: refcount={sys.getrefcount(app)}")
108+
print(f"App {app_idx}: refcount={sys.getrefcount(app)}")
107109

108110
final_set_size = len(_InstrumentedFastAPI._instrumented_fastapi_apps)
109111
print(f"Final set size: {final_set_size}")

instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_memory_leak.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
import fastapi
1919

20-
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
20+
from opentelemetry.instrumentation.fastapi import (
21+
FastAPIInstrumentor,
22+
_InstrumentedFastAPI,
23+
)
2124

2225

2326
class 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

Comments
 (0)