Prometheus
Collect and query time-series metrics with Prometheus and PromQL.
Prometheus is a time-series database and monitoring system. Its defining design
is the pull model: Prometheus periodically scrapes a /metrics HTTP endpoint
on each target. Apps expose metrics via client libraries or exporters (e.g.
node_exporter for host metrics).
Metrics have a name, labels (dimensions like method="GET"), and a value.
The four metric types: counter (only increases), gauge (up/down),
histogram, and summary. You query with PromQL and alert via
Alertmanager.
# Requests per second over the last 5 minutes
rate(http_requests_total[5m])
# 95th percentile latency
histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))
# Error ratio
sum(rate(http_requests_total{code=~"5.."}[5m]))
/ sum(rate(http_requests_total[5m]))# scrape config
scrape_configs:
- job_name: api
static_configs:
- targets: ['api:9090'] - Explain the pull model and what a
/metricsendpoint is. - Distinguish a counter from a gauge with an example of each.
- Write a PromQL query for the per-second request rate.
- What does an exporter do?
Cheat Sheet▾
| Concept | Detail |
|---|---|
| Model | Pull (scrape /metrics) |
| Counter | Monotonically increasing |
| Gauge | Goes up and down |
| Histogram | Bucketed distributions |
| Labels | Metric dimensions |
| PromQL | Query language |
| Exporter | Exposes metrics for a system |
| Alertmanager | Routes alerts |
Common Interview Questions▾
How does Prometheus collect metrics?
With a pull model: it scrapes HTTP /metrics endpoints on targets at a set interval. Apps expose metrics via client libraries; exporters expose them for systems that can’t natively.
Why query rate() on a counter?
Counters only increase, so the raw value isn’t meaningful on its own. rate() gives the per-second increase over a window, which is what you actually want to graph/alert on.
What's the difference between a counter and a gauge?
A counter only goes up (e.g. total requests); a gauge can rise and fall (e.g. memory in use, queue length).