0Pricing
FastAPI Backend Development Bootcamp · Lesson

Prometheus Metrics and RED/USE Dashboards

Expose latency, error, and saturation metrics and visualize service health with Grafana dashboards.

Why Metrics Matter

Logs tell you what happened in one request; metrics tell you how the whole service behaves over time. A metric is a numeric time series sampled at a fixed interval, which makes it cheap to store and fast to aggregate across millions of requests.

For a FastAPI backend you mainly care about three questions:

  • Is it serving traffic? request rate
  • Is it failing? error rate
  • Is it slow? request latency

Prometheus is a pull-based time-series database: it periodically scrapes an HTTP /metrics endpoint your app exposes, stores the samples, and lets you query them with PromQL. Grafana then turns those queries into dashboards.

The Four Metric Types

Prometheus has four core metric types. Picking the right one is the most important modeling decision.

  • Counter — only goes up (or resets to 0 on restart). Use for totals: requests served, errors, bytes sent. You query its rate, not its raw value.
  • Gauge — goes up and down. Use for current state: in-flight requests, memory usage, queue depth, connection pool size.
  • Histogram — buckets observations (e.g. latency) into pre-defined ranges, plus a _sum and _count. Lets you compute quantiles server-side.
  • Summary — like a histogram but computes quantiles client-side; cannot be aggregated across instances. Prefer histograms for latency in distributed services.
from prometheus_client import Counter, Gauge, Histogram

REQUESTS = Counter("http_requests_total", "Total HTTP requests", ["method", "path", "status"])
IN_FLIGHT = Gauge("http_requests_in_flight", "Requests currently being served")
LATENCY = Histogram("http_request_duration_seconds", "Request latency in seconds", ["method", "path"])

REQUESTS.labels("GET", "/users", "200").inc()
IN_FLIGHT.inc()
LATENCY.labels("GET", "/users").observe(0.042)
IN_FLIGHT.dec()

print("counter, gauge and histogram updated")

All lessons in this course

  1. Structured JSON Logging and Correlation IDs
  2. Distributed Tracing with OpenTelemetry
  3. Prometheus Metrics and RED/USE Dashboards
  4. Alerting on SLOs and Error Budgets
← Back to FastAPI Backend Development Bootcamp