Module 6 · Reliability and infrastructureLesson 62 of 887 minIntermediate

Prometheus and Grafana

Prometheus collects numbers from your services on a schedule, Grafana charts them, and alert rules page someone when they go wrong.

Try it

Turn on a traffic spike and time how long the alert takes. Then set the scrape interval to 8 seconds and try again.

Prometheus is the most widely used open-source metrics system, especially with Kubernetes.

How it works

  1. Each service exposes a plain-text /metrics page with current values, for example http_requests_total{route="/cart",status="500"} 42.
  2. Prometheus scrapes every target on an interval (often 15 to 30 seconds) and stores the values as time series.
  3. Labels (route, status, region, instance) let you slice the data.
  4. You query with PromQL, for example the error rate: rate(http_requests_total{status=~"5.."}[5m]).

This is a pull model: Prometheus decides when to collect, and a target that stops answering is itself a signal. Short-lived jobs that can't be scraped push to a Pushgateway instead.

Metric types

  • Counter: only goes up (requests served). Use rate() to get per-second values.
  • Gauge: goes up and down (memory in use, queue length).
  • Histogram: counts values in buckets (request durations), so you can compute percentiles like p95.

Grafana

Grafana connects to Prometheus (and many other sources) and draws dashboards: graphs, heatmaps, single-stat panels. Good dashboards start from user-facing signals (error rate, latency) before machine details (CPU).

Alerting

Alert rules are PromQL expressions with a duration:

alert: HighErrorRate
expr: job:errors:rate5m > 0.05
for: 5m

for means "only fire if it stays true for 5 minutes", which avoids waking people for blips. Alertmanager groups related alerts, silences them during maintenance, and routes them to Slack, email or a pager.

Watch out for

  • High cardinality: a label like user_id creates a separate series per user and can overwhelm Prometheus. Keep labels to small, fixed sets.
  • Scrape interval trade-off: shorter means faster detection and more storage.

Quick check

Prometheus scrapes every 15 seconds and an alert needs 2 bad scrapes in a row. Roughly how quickly can it fire after a problem starts?