Spaces:
Paused
Paused
Fix import errors, shadowing, and openapi serialization issues
Browse files
app/monitoring/memory_sniper.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tracemalloc
|
| 2 |
+
import logging
|
| 3 |
+
from fastapi import Request
|
| 4 |
+
|
| 5 |
+
# Start tracing at import time (once per process)
|
| 6 |
+
tracemalloc.start()
|
| 7 |
+
|
| 8 |
+
def log_memory_snapshot(request: Request | None = None):
|
| 9 |
+
"""Take a memory snapshot and log top 5 memory consumers.
|
| 10 |
+
Optionally include request info for context.
|
| 11 |
+
"""
|
| 12 |
+
snapshot = tracemalloc.take_snapshot()
|
| 13 |
+
top_stats = snapshot.statistics('lineno')
|
| 14 |
+
logging.info('[MEMORY SNAPSHOT] Top 5 Memory Consumers:')
|
| 15 |
+
for idx, stat in enumerate(top_stats[:5], start=1):
|
| 16 |
+
logging.info(f"#{idx}: {stat.traceback.format()[-1].filename}:{stat.traceback.format()[-1].lineno} - {stat.size / 1024:.2f} KiB")
|
| 17 |
+
if request:
|
| 18 |
+
logging.info(f'Request path: {request.url.path}')
|