Phase 2b: Migrate monitoring.py to @with_error_handling (~19 endpoints) #21
Copy link
Copy link
Closed
Description
Overview
Migrate all endpoints in backend/api/monitoring.py from manual try-catch blocks to centralized @with_error_handling decorator.
Priority: 🔴 HIGH - Monitoring API (system health, metrics, alerts)
Parent Issue: #14
File Size: 1,221 lines (41K)
Scope
File: backend/api/monitoring.py
Total Endpoints: 19 endpoints
REST Endpoints: 18 (0 have decorators - need full migration)
WebSocket Endpoints: 1 (line 767 - protocol-specific, doesn't need decorator)
Exception Handlers: 33 handlers
Start After: analytics.py analysis complete (#20) ✅
Initial Analysis
Decorator Status:
- ❌ 0/18 REST endpoints have @with_error_handling decorator (0%)
- ✅ 1 WebSocket endpoint doesn't need decorator (protocol requirement)
⚠️ Type: Simple Pattern (like terminal.py - add decorators from scratch)
WebSocket Endpoint (No Migration Needed):
- Line 767:
realtime_monitoring_websocket-/realtime
Migration Focus:
- Add @with_error_handling decorator to all 18 REST endpoints
- Remove redundant try-catch blocks that only log and re-raise
- Preserve necessary try-catch (recovery logic, loop safety)
Endpoint List (18 REST + 1 WebSocket)
REST Endpoints (need decorator):
- Line 174:
get_monitoring_status- GET /status - Line 210:
start_monitoring_endpoint- POST /start - Line 247:
stop_monitoring_endpoint- POST /stop - Line 270:
get_performance_dashboard- GET /dashboard - Line 292:
get_dashboard_overview- GET /dashboard/overview - Line 314:
get_current_metrics- GET /metrics/current - Line 331:
query_metrics- POST /metrics/query - Line 428:
get_optimization_recommendations- GET /optimization - Line 454:
get_performance_alerts- GET /alerts - Line 492:
check_alerts- GET /alerts/check - Line 510:
update_performance_threshold- POST /thresholds/update - Line 542:
get_gpu_details- GET /hardware/gpu - Line 581:
get_npu_details- GET /hardware/npu - Line 619:
get_services_health- GET /services/health - Line 683:
export_metrics- GET /export/metrics - Line 1036:
test_performance_monitoring- POST /test/performance - Line 1170:
get_prometheus_metrics- GET /prometheus/metrics - Line 1191:
metrics_health_check- GET /health
WebSocket: Line 767: realtime_monitoring_websocket - /realtime
Migration Status
- Initial analysis complete
- Create migration batches (3-4 endpoints per batch)
- Migrate batch 59 (first 3 endpoints)
- Migrate batch 60
- Migrate batch 61
- Migrate remaining batches
- Add comprehensive tests for all batches
- Update tracking issue Error Handling Refactoring: Phase 2b Progress Tracking #14
Migration Pattern
Decorator to add:
@router.get("/endpoint")
@with_error_handling(
category=ErrorCategory.SERVER_ERROR,
operation="operation_name",
error_code_prefix="MONITORING",
)
async def endpoint_function(...):
...Success Criteria
- All 18 REST endpoints have @with_error_handling decorator
- Redundant try-catch blocks removed
- Necessary try-catch blocks preserved (recovery logic)
- All tests passing (100%)
- Estimated line savings: ~100-150 lines (based on terminal.py pattern)
- No regression in monitoring functionality
Related
- Parent: Error Handling Refactoring: Phase 2b Progress Tracking #14 (Phase 2b Progress Tracking)
- Previous: Phase 2b: Migrate analytics.py to @with_error_handling (~28 endpoints) #20 (analytics.py - no migration needed) ✅
- Similar pattern: Phase 2b: Migrate terminal.py to @with_error_handling (~20+ endpoints) #17 (terminal.py - Simple Pattern)
- Plan:
docs/developer/ERROR_HANDLING_REFACTORING_PLAN.mdPhase 2b
Reactions are currently unavailable
Activity
mrveiss commentedon Oct 30, 2025
✅ Batch 59 Complete: First 3 Endpoints Migrated
Commit:
110e38f- refactor(error-handling): migrate batch 59 endpoints to @with_error_handling (monitoring.py first 3 endpoints)Migrated Endpoints
GET /monitoring/status (
get_monitoring_status, line 175-207)@with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")get_phase9_performance_dashboard(),MonitoringStatusPOST /monitoring/start (
start_monitoring_endpoint, line 210-243)@with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")background_tasks.add_task(),add_phase9_alert_callback()POST /monitoring/stop (
stop_monitoring_endpoint, line 246-265)@with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")stop_monitoring(),phase9_monitor.monitoring_activePattern Used
Simple Pattern (like terminal.py):
Results
ErrorCategory.SERVER_ERRORconsistentlyMONITORINGprefixTesting
Added TestBatch59MonitoringMigrations (11 comprehensive tests):
Test Results:
Progress Update
monitoring.py Migration:
Next Steps:
Related: Part of ERROR_HANDLING_REFACTORING_PLAN.md Phase 2b
Parent Issue: #14
mrveiss commentedon Oct 30, 2025
✅ Batch 60 Complete: 4 Dashboard/Metrics Endpoints Migrated
Commit:
11675c0- refactor(error-handling): migrate batch 60 endpoints to @with_error_handling (monitoring.py dashboard/metrics endpoints)Migrated Endpoints
GET /monitoring/dashboard (
get_performance_dashboard, lines 268-286)@with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")get_phase9_performance_dashboard(), dashboard analysis (health, performance score, bottlenecks, resource utilization)GET /monitoring/dashboard/overview (
get_dashboard_overview, lines 289-307)@with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")get_phase9_performance_dashboard(), dashboard analysis (same analysis as dashboard endpoint)GET /monitoring/metrics/current (
get_current_metrics, lines 310-323)@with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")collect_phase9_metrics(), timestamp, collection_successful flagPOST /monitoring/metrics/query (
query_metrics, lines 326-419)@with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")Pattern Used
Simple Pattern (like terminal.py):
Results
ErrorCategory.SERVER_ERRORconsistentlyMONITORINGprefixTesting
Added TestBatch60MonitoringMigrations (14 comprehensive tests):
Test Results:
Progress Update
monitoring.py Migration:
Next Steps:
Related: Part of ERROR_HANDLING_REFACTORING_PLAN.md Phase 2b
Parent Issue: #14
mrveiss commentedon Oct 30, 2025
✅ Batch 61 Complete: 4 Optimization/Alerts Endpoints Migrated
Commit:
ad7b0eb- refactor(error-handling): migrate batch 61 endpoints to @with_error_handling (monitoring.py optimization/alerts endpoints)Migrated Endpoints
GET /monitoring/optimization/recommendations (
get_optimization_recommendations, lines 422-443)@with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")get_phase9_optimization_recommendations(),OptimizationRecommendationmodelGET /monitoring/alerts (
get_performance_alerts, lines 446-480)@with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")PerformanceAlertmodelGET /monitoring/alerts/check (
check_alerts, lines 483-499)@with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")POST /monitoring/thresholds/update (
update_performance_threshold, lines 502-530)@with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")performance_baselines, updated/created statusPattern Used
Simple Pattern (like terminal.py):
Results
ErrorCategory.SERVER_ERRORconsistentlyMONITORINGprefixTesting
Added TestBatch61MonitoringMigrations (14 comprehensive tests):
Test Results:
Progress Update
monitoring.py Migration:
Next Steps:
Related: Part of ERROR_HANDLING_REFACTORING_PLAN.md Phase 2b
Parent Issue: #14
mrveiss commentedon Oct 30, 2025
✅ Batch 62 Complete - Hardware/Services/Export Endpoints
Endpoints Migrated (4):
GET /hardware/gpu(get_gpu_details) - lines 533-568GET /hardware/npu(get_npu_details) - lines 571-605GET /services/health(get_services_health) - lines 608-668GET /export/metrics(export_metrics) - lines 671-751Changes:
@with_error_handlingdecorators (SERVER_ERROR, MONITORING prefix)Testing:
Results:
Progress:
monitoring.py: 15/18 REST endpoints migrated (83.3%)Commit: 17c4f15
Next: Final batch 63 (3 remaining endpoints) to complete monitoring.py migration
Part of Phase 2b High-Traffic Endpoint Migration
mrveiss commentedon Oct 30, 2025
🎉 Batch 63 Complete - monitoring.py 100% MIGRATED! 🎉
Final Endpoints Migrated (3):
POST /test/performance(test_performance_monitoring) - lines 1023-1042GET /metrics(get_prometheus_metrics) - lines 1162-1185GET /health/metrics(metrics_health_check) - lines 1188-1240Changes:
@with_error_handlingdecorators (SERVER_ERROR, MONITORING prefix)Testing:
Results:
🏆 monitoring.py MIGRATION COMPLETE! 🏆
Final Statistics:
Batch Breakdown:
Coverage:
Excluded by Design:
Commit: 98ce3a7
Next: Move to next Phase 2b priority file per ERROR_HANDLING_REFACTORING_PLAN.md
Closing this issue as monitoring.py is now 100% migrated with all 18 REST endpoints using centralized error handling! 🎉
Part of Phase 2b High-Traffic Endpoint Migration
refactor(error-handling): migrate batch 59 endpoints to @with_error_h…
refactor(error-handling): migrate batch 60 endpoints to @with_error_h…
refactor(error-handling): migrate batch 61 endpoints to @with_error_h…
refactor(error-handling): migrate batch 63 endpoints to @with_error_h…
refactor(error-handling): migrate batch 59 endpoints to @with_error_h…
refactor(error-handling): migrate batch 60 endpoints to @with_error_h…
refactor(error-handling): migrate batch 61 endpoints to @with_error_h…
refactor(error-handling): migrate batch 63 endpoints to @with_error_h…