feat(devops): Build Prometheus Metrics Middleware and Grafana Dashboard for FastAPI Backend Monitoring#656
Conversation
… for backend monitoring Closes ritesh-1918#636 - Add backend/middleware/metrics.py: prometheus-fastapi-instrumentator setup with custom counters/histograms for AI analyze calls, active tickets, duplicate detections, and OCR; /metrics endpoint secured by Bearer token or IP allowlist via METRICS_SECRET_TOKEN and METRICS_ALLOWED_IPS env vars - Add deploy/prometheus/prometheus.yml: Prometheus scrape config targeting the FastAPI backend with token auth and node_exporter sidecar config - Add deploy/grafana/dashboard.json: Grafana dashboard manifest with 9 panels: HTTP request rate by endpoint, p95 latency gauge, AI analysis rate & latency p50/p95, active tickets, duplicate detection rate, memory/CPU stats, and error rate (4xx/5xx) per endpoint - Update backend/requirements.txt: add prometheus-fastapi-instrumentator, prometheus-client, and pycryptodome (shared with issue 632 encryption work)
|
@advikdivekar is attempting to deploy a commit to the ritesh Team on Vercel. A member of the Team first needs to authorize it. |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@ritesh-1918 please review my PR, thank you |
Summary
Fixes #636
This PR integrates Prometheus telemetry into the FastAPI backend and provides a ready-to-import Grafana dashboard for production visibility.
Changes
backend/middleware/metrics.py— Prometheus Integrationprometheus-fastapi-instrumentatorto auto-instrument all HTTP routeshelpdesk_ai_analyze_total(Counter) — AI requests by model version & statushelpdesk_ai_analyze_duration_seconds(Histogram) — AI latency with p50/p95 bucketshelpdesk_active_tickets_total(Gauge) — live active ticket counthelpdesk_duplicate_detections_total(Counter) — duplicate flagging ratehelpdesk_ocr_processing_total(Counter) — OCR call count by status/metricsendpoint: Bearer token (METRICS_SECRET_TOKEN) or IP allowlist (METRICS_ALLOWED_IPS) — returns 403 otherwiseprometheus-fastapi-instrumentatoris not installeddeploy/prometheus/prometheus.yml— Prometheus scrape config targetingbackend:8000with token auth and anode_exportersidecar for system metricsdeploy/grafana/dashboard.json— Grafana dashboard with 9 panels:backend/requirements.txt— addsprometheus-fastapi-instrumentator>=6.1.0,prometheus-client>=0.19.0,pycryptodome>=3.20.0Test Plan
setup_metrics(app)inbackend/main.pyand start the serverGET /metricswith no token → 403GET /metricswith correctAuthorization: Bearer <token>→ 200 Prometheus textdeploy/grafana/dashboard.jsoninto a Grafana instance pointing at Prometheus