Skip to content

Phase 2b: Migrate monitoring.py to @with_error_handling (~19 endpoints) #21

Closed

Description

@mrveiss

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):

  1. 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):

  1. Line 174: get_monitoring_status - GET /status
  2. Line 210: start_monitoring_endpoint - POST /start
  3. Line 247: stop_monitoring_endpoint - POST /stop
  4. Line 270: get_performance_dashboard - GET /dashboard
  5. Line 292: get_dashboard_overview - GET /dashboard/overview
  6. Line 314: get_current_metrics - GET /metrics/current
  7. Line 331: query_metrics - POST /metrics/query
  8. Line 428: get_optimization_recommendations - GET /optimization
  9. Line 454: get_performance_alerts - GET /alerts
  10. Line 492: check_alerts - GET /alerts/check
  11. Line 510: update_performance_threshold - POST /thresholds/update
  12. Line 542: get_gpu_details - GET /hardware/gpu
  13. Line 581: get_npu_details - GET /hardware/npu
  14. Line 619: get_services_health - GET /services/health
  15. Line 683: export_metrics - GET /export/metrics
  16. Line 1036: test_performance_monitoring - POST /test/performance
  17. Line 1170: get_prometheus_metrics - GET /prometheus/metrics
  18. 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

Activity

  1. mrveiss commented on Oct 30, 2025

    @mrveiss
    OwnerAuthor

    ✅ 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

    1. GET /monitoring/status (get_monitoring_status, line 175-207)

      • Applied @with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")
      • Removed redundant try-catch that only raised HTTPException
      • Business logic preserved: get_phase9_performance_dashboard(), MonitoringStatus
      • Lines saved: ~8
    2. POST /monitoring/start (start_monitoring_endpoint, line 210-243)

      • Applied @with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")
      • Removed redundant try-catch that only raised HTTPException
      • Business logic preserved: background_tasks.add_task(), add_phase9_alert_callback()
      • Lines saved: ~9
    3. POST /monitoring/stop (stop_monitoring_endpoint, line 246-265)

      • Applied @with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")
      • Removed redundant try-catch that only raised HTTPException
      • Business logic preserved: stop_monitoring(), phase9_monitor.monitoring_active
      • Lines saved: ~8

    Pattern Used

    Simple Pattern (like terminal.py):

    • Add decorators from scratch (monitoring.py had 0 decorators)
    • Remove redundant try-catch blocks that only log and raise HTTPException
    • Decorator handles all exceptions appropriately with trace IDs and error codes

    Results

    • Code reduction: ~25 lines saved across 3 endpoints
    • Error category: All 3 endpoints use ErrorCategory.SERVER_ERROR consistently
    • Error code prefix: All 3 endpoints use MONITORING prefix
    • Python syntax: ✅ Verified (py_compile passed)
    • Business logic: 100% preserved (monitoring system operations unchanged)

    Testing

    Added TestBatch59MonitoringMigrations (11 comprehensive tests):

    • Decorator presence verification (3 tests)
    • No try-catch blocks verification (3 tests - all expect 0 try blocks)
    • Business logic preservation (3 tests)
    • Decorator configuration consistency (2 tests)

    Test Results:

    • ✅ All 11 batch 59 tests: PASSED
    • ✅ Full test suite: 645 tests PASSED (634 previous + 11 new)
    • ✅ No regressions introduced

    Progress Update

    monitoring.py Migration:

    • Completed: 3/18 REST endpoints (16.7%)
    • Lines saved: 25 lines so far
    • Remaining: 15 REST endpoints

    Next Steps:

    • Batch 60: Next 3-4 endpoints (lines 270-428)
    • Continue systematic migration of remaining endpoints
    • Target: Complete all 18 REST endpoints

    Related: Part of ERROR_HANDLING_REFACTORING_PLAN.md Phase 2b
    Parent Issue: #14

  2. mrveiss commented on Oct 30, 2025

    @mrveiss
    OwnerAuthor

    ✅ 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

    1. GET /monitoring/dashboard (get_performance_dashboard, lines 268-286)

      • Applied @with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")
      • Removed redundant try-catch that only raised HTTPException
      • Business logic preserved: get_phase9_performance_dashboard(), dashboard analysis (health, performance score, bottlenecks, resource utilization)
      • Lines saved: ~9
    2. GET /monitoring/dashboard/overview (get_dashboard_overview, lines 289-307)

      • Applied @with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")
      • Removed redundant try-catch that only raised HTTPException
      • Business logic preserved: get_phase9_performance_dashboard(), dashboard analysis (same analysis as dashboard endpoint)
      • Lines saved: ~9
    3. GET /monitoring/metrics/current (get_current_metrics, lines 310-323)

      • Applied @with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")
      • Removed redundant try-catch that only raised HTTPException
      • Business logic preserved: collect_phase9_metrics(), timestamp, collection_successful flag
      • Lines saved: ~7
    4. POST /monitoring/metrics/query (query_metrics, lines 326-419)

      • Applied @with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")
      • Removed redundant try-catch that only raised HTTPException
      • Business logic preserved: Complex metrics filtering (GPU/NPU/system buffers), time range calculation, trends, alerts
      • Lines saved: ~10

    Pattern Used

    Simple Pattern (like terminal.py):

    • Add decorators from scratch (monitoring.py started with 0 decorators)
    • Remove redundant try-catch blocks that only log and raise HTTPException
    • Decorator handles all exceptions appropriately with trace IDs and error codes

    Results

    • Code reduction: ~35 lines saved across 4 endpoints
    • Error category: All 4 endpoints use ErrorCategory.SERVER_ERROR consistently
    • Error code prefix: All 4 endpoints use MONITORING prefix
    • Python syntax: ✅ Verified (py_compile passed)
    • Business logic: 100% preserved (dashboard/metrics operations unchanged)

    Testing

    Added TestBatch60MonitoringMigrations (14 comprehensive tests):

    • Decorator presence verification (4 tests)
    • No try-catch blocks verification (4 tests - all expect 0 try blocks)
    • Business logic preservation (4 tests - verified dashboard analysis, metrics collection, query filtering)
    • Decorator configuration consistency (2 tests)

    Test Results:

    • ✅ All 14 batch 60 tests: PASSED
    • ✅ Full test suite: 659 tests PASSED (645 previous + 14 new)
    • ✅ No regressions introduced

    Progress Update

    monitoring.py Migration:

    • Completed: 7/18 REST endpoints (38.9%)
      • Batch 59: 3 endpoints (status, start, stop)
      • Batch 60: 4 endpoints (dashboard, dashboard/overview, metrics/current, metrics/query)
    • Lines saved: ~60 lines total (25 from batch 59 + 35 from batch 60)
    • Remaining: 11 REST endpoints

    Next Steps:

    • Batch 61: Next 3-4 endpoints (lines 422-620+)
      • GET /monitoring/optimization/recommendations
      • GET /monitoring/alerts
      • GET /monitoring/alerts/check
      • POST /monitoring/thresholds/update
    • Continue systematic migration of remaining endpoints
    • Target: Complete all 18 REST endpoints

    Related: Part of ERROR_HANDLING_REFACTORING_PLAN.md Phase 2b
    Parent Issue: #14

  3. mrveiss commented on Oct 30, 2025

    @mrveiss
    OwnerAuthor

    ✅ 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

    1. GET /monitoring/optimization/recommendations (get_optimization_recommendations, lines 422-443)

      • Applied @with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")
      • Removed redundant try-catch that only raised HTTPException
      • Business logic preserved: get_phase9_optimization_recommendations(), OptimizationRecommendation model
      • Lines saved: ~10
    2. GET /monitoring/alerts (get_performance_alerts, lines 446-480)

      • Applied @with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")
      • Removed redundant try-catch that only raised HTTPException
      • Business logic preserved: alerts filtering (severity, category), sorting, limit, PerformanceAlert model
      • Lines saved: ~10
    3. GET /monitoring/alerts/check (check_alerts, lines 483-499)

      • Applied @with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")
      • Removed redundant try-catch that only raised HTTPException
      • Business logic preserved: alerts counting, critical/warning counts
      • Lines saved: ~7
    4. POST /monitoring/thresholds/update (update_performance_threshold, lines 502-530)

      • Applied @with_error_handling(category=SERVER_ERROR, error_code_prefix="MONITORING")
      • Removed redundant try-catch that only raised HTTPException
      • Business logic preserved: threshold updates, performance_baselines, updated/created status
      • Lines saved: ~10

    Pattern Used

    Simple Pattern (like terminal.py):

    • Add decorators from scratch (monitoring.py started with 0 decorators)
    • Remove redundant try-catch blocks that only log and raise HTTPException
    • Decorator handles all exceptions appropriately with trace IDs and error codes

    Results

    • Code reduction: ~37 lines saved across 4 endpoints
    • Error category: All 4 endpoints use ErrorCategory.SERVER_ERROR consistently
    • Error code prefix: All 4 endpoints use MONITORING prefix
    • Python syntax: ✅ Verified (py_compile passed)
    • Business logic: 100% preserved (optimization/alerts operations unchanged)

    Testing

    Added TestBatch61MonitoringMigrations (14 comprehensive tests):

    • Decorator presence verification (4 tests)
    • No try-catch blocks verification (4 tests - all expect 0 try blocks)
    • Business logic preservation (4 tests - verified optimization recommendations, alerts filtering, threshold updates)
    • Decorator configuration consistency (2 tests)

    Test Results:

    • ✅ All 14 batch 61 tests: PASSED
    • ✅ Full test suite: 673 tests PASSED (659 previous + 14 new)
    • ✅ No regressions introduced

    Progress Update

    monitoring.py Migration:

    • Completed: 11/18 REST endpoints (61.1%)
      • Batch 59: 3 endpoints (status, start, stop)
      • Batch 60: 4 endpoints (dashboard, dashboard/overview, metrics/current, metrics/query)
      • Batch 61: 4 endpoints (optimization/recommendations, alerts, alerts/check, thresholds/update)
    • Lines saved: ~97 lines total (25 + 35 + 37)
    • Remaining: 7 REST endpoints

    Next Steps:

    • Batch 62: Next 3-4 endpoints (lines 533-683+)
      • GET /monitoring/hardware/gpu
      • GET /monitoring/hardware/npu
      • GET /monitoring/services/health
      • GET /monitoring/export/metrics
    • Continue systematic migration of remaining endpoints
    • Target: Complete all 18 REST endpoints

    Related: Part of ERROR_HANDLING_REFACTORING_PLAN.md Phase 2b
    Parent Issue: #14

  4. mrveiss commented on Oct 30, 2025

    @mrveiss
    OwnerAuthor

    ✅ Batch 62 Complete - Hardware/Services/Export Endpoints

    Endpoints Migrated (4):

    1. GET /hardware/gpu (get_gpu_details) - lines 533-568
    2. GET /hardware/npu (get_npu_details) - lines 571-605
    3. GET /services/health (get_services_health) - lines 608-668
    4. GET /export/metrics (export_metrics) - lines 671-751

    Changes:

    • Added @with_error_handling decorators (SERVER_ERROR, MONITORING prefix)
    • Removed 44 lines of redundant try-catch blocks
    • All business logic preserved
    • Hardware metrics collection (GPU/NPU details)
    • Service health aggregation and status calculation
    • Metrics export functionality (JSON/CSV formats)

    Testing:

    • ✅ 14 comprehensive tests added (TestBatch62MonitoringMigrations)
    • ✅ All 687 tests passing (673 previous + 14 new)
    • ✅ Python syntax verified
    • ✅ No regressions introduced

    Results:

    • Lines saved: ~44 lines (redundant error handling removed)
    • Test coverage: 100% (decorator, pattern, business logic)
    • Automatic trace IDs and error codes
    • Standardized error responses

    Progress:

    • monitoring.py: 15/18 REST endpoints migrated (83.3%)
    • Batches complete: 59, 60, 61, 62
    • Endpoints remaining: 3 (final batch 63)
    • Total lines saved: ~141 lines
    • Total tests: 687 passing (56 new batch tests)

    Commit: 17c4f15

    Next: Final batch 63 (3 remaining endpoints) to complete monitoring.py migration


    Part of Phase 2b High-Traffic Endpoint Migration

  5. mrveiss commented on Oct 30, 2025

    @mrveiss
    OwnerAuthor

    🎉 Batch 63 Complete - monitoring.py 100% MIGRATED! 🎉

    Final Endpoints Migrated (3):

    1. POST /test/performance (test_performance_monitoring) - lines 1023-1042
    2. GET /metrics (get_prometheus_metrics) - lines 1162-1185
    3. GET /health/metrics (metrics_health_check) - lines 1188-1240

    Changes:

    • Added @with_error_handling decorators (SERVER_ERROR, MONITORING prefix)
    • Removed 7 lines of redundant try-catch blocks
    • All business logic preserved
    • Performance test endpoint with @monitor_performance decorator stacking
    • Prometheus metrics export functionality fully migrated
    • Metrics health check functionality fully migrated

    Testing:

    • ✅ 11 comprehensive tests added (TestBatch63MonitoringMigrations)
    • ✅ All 698 tests passing (687 previous + 11 new)
    • ✅ Python syntax verified
    • ✅ No regressions introduced

    Results:

    • Lines saved: ~7 lines (redundant error handling removed)
    • Test coverage: 100% (decorator, pattern, business logic)
    • Automatic trace IDs and error codes
    • Standardized error responses

    🏆 monitoring.py MIGRATION COMPLETE! 🏆

    Final Statistics:

    • 18/18 REST endpoints migrated (100%)
    • Batches complete: 59, 60, 61, 62, 63
    • Total tests: 698 passing (67 new batch tests)
    • Total lines saved: ~148 lines
    • Zero regressions: All tests passing

    Batch Breakdown:

    • Batch 59: 3 endpoints (status, start, stop) - 25 lines saved
    • Batch 60: 4 endpoints (dashboard/metrics) - 35 lines saved
    • Batch 61: 4 endpoints (optimization/alerts) - 37 lines saved
    • Batch 62: 4 endpoints (hardware/services/export) - 44 lines saved
    • Batch 63: 3 endpoints (test/prometheus/health) - 7 lines saved

    Coverage:

    • ✅ Status endpoints (GET /status, POST /start, POST /stop)
    • ✅ Dashboard endpoints (GET /dashboard, GET /dashboard/overview)
    • ✅ Metrics endpoints (GET /metrics/current, POST /metrics/query)
    • ✅ Optimization endpoints (GET /optimization/recommendations)
    • ✅ Alert endpoints (GET /alerts, GET /alerts/check)
    • ✅ Threshold endpoints (POST /thresholds/update)
    • ✅ Hardware endpoints (GET /hardware/gpu, GET /hardware/npu)
    • ✅ Service endpoints (GET /services/health)
    • ✅ Export endpoints (GET /export/metrics)
    • ✅ Test endpoints (POST /test/performance)
    • ✅ Prometheus endpoints (GET /metrics, GET /health/metrics)

    Excluded by Design:

    • WebSocket endpoint (line 754) - Different error handling pattern

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions