Skip to content

feat(devops): Build Prometheus Metrics Middleware and Grafana Dashboard for FastAPI Backend Monitoring#656

Open
advikdivekar wants to merge 2 commits into
ritesh-1918:gssocfrom
advikdivekar:fix/issue-636-prometheus-grafana
Open

feat(devops): Build Prometheus Metrics Middleware and Grafana Dashboard for FastAPI Backend Monitoring#656
advikdivekar wants to merge 2 commits into
ritesh-1918:gssocfrom
advikdivekar:fix/issue-636-prometheus-grafana

Conversation

@advikdivekar
Copy link
Copy Markdown

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 Integration

    • Uses prometheus-fastapi-instrumentator to auto-instrument all HTTP routes
    • Registers custom application-level metrics:
      • helpdesk_ai_analyze_total (Counter) — AI requests by model version & status
      • helpdesk_ai_analyze_duration_seconds (Histogram) — AI latency with p50/p95 buckets
      • helpdesk_active_tickets_total (Gauge) — live active ticket count
      • helpdesk_duplicate_detections_total (Counter) — duplicate flagging rate
      • helpdesk_ocr_processing_total (Counter) — OCR call count by status
    • Secure /metrics endpoint: Bearer token (METRICS_SECRET_TOKEN) or IP allowlist (METRICS_ALLOWED_IPS) — returns 403 otherwise
    • Graceful degradation when prometheus-fastapi-instrumentator is not installed
  • deploy/prometheus/prometheus.yml — Prometheus scrape config targeting backend:8000 with token auth and a node_exporter sidecar for system metrics

  • deploy/grafana/dashboard.json — Grafana dashboard with 9 panels:

    1. HTTP Request Rate by Endpoint
    2. Request Latency p95 (gauge)
    3. AI Analysis Requests by Model & Status
    4. AI Analysis Latency p50/p95
    5. Active Tickets (stat)
    6. Duplicate Detections per 10m (stat)
    7. Memory Usage GiB (stat)
    8. CPU Usage (stat)
    9. Error Rate 4xx/5xx by Endpoint
  • backend/requirements.txt — adds prometheus-fastapi-instrumentator>=6.1.0, prometheus-client>=0.19.0, pycryptodome>=3.20.0

Test Plan

  • Import setup_metrics(app) in backend/main.py and start the server
  • GET /metrics with no token → 403
  • GET /metrics with correct Authorization: Bearer <token> → 200 Prometheus text
  • Import deploy/grafana/dashboard.json into a Grafana instance pointing at Prometheus
  • All 9 panels render without errors

… 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)
@vercel
Copy link
Copy Markdown

vercel Bot commented May 29, 2026

@advikdivekar is attempting to deploy a commit to the ritesh Team on Vercel.

A member of the Team first needs to authorize it.

@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 29, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c65bdae9-22ad-4eee-8e8d-2a8c79090195

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@advikdivekar
Copy link
Copy Markdown
Author

@ritesh-1918 please review my PR, thank you

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant