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.
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
- Each service exposes a plain-text
/metricspage with current values, for examplehttp_requests_total{route="/cart",status="500"} 42. - Prometheus scrapes every target on an interval (often 15 to 30 seconds) and stores the values as time series.
- Labels (route, status, region, instance) let you slice the data.
- 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_idcreates 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.