Track four golden signals per service with Prometheus — alert on latency, errors, and saturation.
Observability: Four Golden Signals
# 1. Latency -- response time (p50, p95, p99)
# 2. Traffic -- requests per second
# 3. Errors -- 4xx + 5xx rate
# 4. Saturation -- CPU, memory, queue depth
from prometheus_client import Counter, Histogram
import time
REQUESTS = Counter("requests_total","Requests",["endpoint","status"])
LATENCY = Histogram("latency_seconds","Latency",["endpoint"])
@app.middleware("http")
async def metrics(req, call_next):
start = time.time()
resp = await call_next(req)
LATENCY.labels(endpoint=req.url.path).observe(time.time()-start)
REQUESTS.labels(endpoint=req.url.path,status=resp.status_code).inc()
return resp
# PromQL alert: error rate > 1%
# rate(requests_total{status=~"5.."}[5m]) > 0.01