High-performance FastAPI Backend with native Prometheus instrumentation, health checks, and SRE failure injection capabilities.
The Cloud Monitor Backend is an asynchronous microservice powering the Cloud Monitor observability stack. It exposes:
- Core Application Endpoints: Service discovery, versioning, and operational status.
- Production Health Checks: Standard
/healthroute with sub-component status and uptime. - Prometheus Telemetry: Real-time
/metricsendpoint exposing HTTP request counts, latency histograms, error rates, and custom gauges. - SRE Fault Injection API: Dedicated endpoints to simulate HTTP 500 errors, synthetic request latency, and health state toggling to validate observability alerts.
+-------------------------------------------------------------+
| DOCKER HOST |
| |
| +-----------------------+ +--------------------+ |
| | | GET | | |
| | Prometheus Server |------->| Cloud Monitor | |
| | (Scrapes :8000) |/metrics| FastAPI Backend | |
| | | | (Port :8000) | |
| +-----------------------+ +---------+----------+ |
| ^ |
| | |
+----------------------------------------------|--------------+
|
HTTP Requests / Docs
|
+----------+---------+
| Client / Operator |
| Swagger UI / cURL |
+--------------------+
-
Clone the repository:
git clone https://github.com/blackpython34/cloud_monitor.git cd cloud_monitor -
(Optional) Configure environment variables:
# Linux / macOS: cp .env.example .env # Windows (PowerShell): Copy-Item .env.example .env
-
Build the Docker container:
docker build -t cloudmonitor-backend:latest ./backend
-
Run the container:
docker run -d \ -p 8000:8000 \ --name cloudmonitor-backend \ cloudmonitor-backend:latest
-
Verify container status:
docker ps -f name=cloudmonitor-backend
You can launch the backend service directly using the included docker-compose.yml:
# 1. Clone repository & enter directory
git clone https://github.com/blackpython34/cloud_monitor.git
cd cloud_monitor
# 2. Build and launch the backend container in background
docker compose up -d --build backend
# 3. Check service status
docker compose ps backend
# 4. View live container logs
docker compose logs -f backend
# 5. Stop the backend service
docker compose stop backendIf you prefer to run the service locally using Python:
- Python 3.10+ (Python 3.12 recommended)
- pip and virtualenv
# On Linux / macOS:
python3 -m venv venv
source venv/bin/activate
# On Windows (PowerShell):
python -m venv venv
.\venv\Scripts\Activate.ps1pip install --upgrade pip
pip install -r backend/requirements.txt# Run from repository root
uvicorn backend.app.main:app --host 0.0.0.0 --port 8000 --reloadOnce running, access interactive OpenAPI documentation at:
- Swagger UI: http://localhost:8000/docs
- ReDoc UI: http://localhost:8000/redoc
| Method | Endpoint | Description | Expected Status |
|---|---|---|---|
GET |
/ |
Root service info and directory links | 200 OK |
GET |
/health |
Service health, uptime, and component statuses | 200 OK |
GET |
/metrics |
Prometheus metrics exposition format | 200 OK |
GET |
/api/v1/status |
High-level operational status | 200 OK |
POST |
/api/v1/simulate/error |
Simulates an HTTP 500 error for alert validation | 500 Internal Error |
GET |
/api/v1/simulate/latency |
Simulates delay (?delay=1.5) for SLO/P95 testing |
200 OK |
POST |
/api/v1/simulate/health-toggle |
Toggles health gauge (?status=false or true) |
200 OK |
# 1. Health check
curl -s http://localhost:8000/health | jq
# Sample response:
# {
# "status": "healthy",
# "service": "CloudMonitor-API",
# "version": "1.0.0",
# "environment": "development",
# "uptime_seconds": 45.2,
# "components": {
# "api": "healthy",
# "metrics_exporter": "healthy",
# "engine": "active"
# }
# }
# 2. Scrape Prometheus metrics
curl -s http://localhost:8000/metrics | head -n 25
# 3. Simulate API Latency (1.5 seconds)
curl -s "http://localhost:8000/api/v1/simulate/latency?delay=1.5"
# 4. Simulate HTTP 500 Internal Error
curl -s -X POST http://localhost:8000/api/v1/simulate/error
# 5. Toggle Health Status to degraded for alerting tests
curl -s -X POST "http://localhost:8000/api/v1/simulate/health-toggle?status=false"
# 6. Restore Health Status to healthy
curl -s -X POST "http://localhost:8000/api/v1/simulate/health-toggle?status=true"The backend natively exports standard Prometheus metrics:
http_requests_total{method, endpoint, status_code}: Counter of all processed HTTP requests.http_request_duration_seconds{method, endpoint}: Histogram measuring request latency percentiles (P50, P90, P95, P99).app_health_status: Gauge set to1when healthy,0when degraded/unhealthy.simulated_errors_total: Counter recording simulated SRE fault injections.- Standard Python runtime metrics (
process_cpu_seconds_total,process_resident_memory_bytes,python_gc_*).
- Non-Root Execution: Runs under a dedicated system user and group (
cloudmonitor) for container hardening. - Embedded Healthcheck:
HEALTHCHECK --interval=10s --timeout=5s --start-period=5s --retries=3 \ CMD curl -f http://localhost:8000/health || exit 1
- Lean Footprint: Based on
python:3.12-slimto minimize image size and attack surface. - Optimized Python Runtime: Uses
PYTHONDONTWRITEBYTECODE=1andPYTHONUNBUFFERED=1for real-time Docker logging.
This project is licensed under the Apache License 2.0.