What it is
A metric is a numeric measurement sampled over time and stored as a time series: a name, a set of labels and a sequence of (timestamp, value) points. Because each series stores aggregates rather than individual events, its cost depends on the number of series, not on traffic.
Why it exists
You need to know continuously, and cheaply, whether a system is healthy: how much traffic, how many errors, how slow, how full. Metrics feed dashboards and alerts, show trends over months, and let you compare before and after a change.
How it works
Counters only increase (requests served, errors, bytes sent) and reset on restart. You query their rate over a window, e.g. rate(http_requests_total[5m]), which handles resets.
Gauges go up and down: in-flight requests, queue depth, memory in use, pool connections. You read the value or an aggregate (average, max) over time.
Histograms count observations into buckets (≤ 50 ms, ≤ 100 ms, …). Percentiles are estimated from the buckets, so bucket boundaries should bracket the values you care about; native/exponential histograms (Prometheus, OpenTelemetry) adjust resolution automatically. Histograms can be aggregated across instances; pre-computed per-instance percentiles (summaries) cannot be averaged meaningfully.
Collection. Either the backend scrapes an HTTP endpoint on each process every 10–60 s (Prometheus model), or processes push (OTLP, StatsD). Either way a time series database stores the points.
Little's law links them: average requests in flight = arrival rate × average time in system. At 80 req/s and 90 ms, about 7 requests are in flight; if latency rises at the same traffic, the gauge rises with it.
Percentiles. Pxx is the latency xx% of requests beat. A percentile reacts once more than (100 − xx)% of requests are affected: P50 needs a majority, P99 just 1%. Averages move partially and describe neither the fast nor the slow group.
RED and USE. For each request-serving service, track Rate, Errors and Duration (Tom Wilkie). For each resource, track Utilisation, Saturation (queued work) and Errors (Brendan Gregg). RED is the user's view; USE finds the cause.
When to use it
For anything you want to graph, alert on or trend: traffic, errors, latency distributions, resource usage, queue depths, business counters (orders per minute). Metrics are the basis of SLOs and burn-rate alerts.
When not to
- To explain individual requests: use logs and traces.
- For high-cardinality dimensions (user id, request id, full URL with ids): each value creates a new series.
- For exact billing or audit records: scrapes can miss the last interval before a crash.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Histogram instead of average | Percentiles, tail visibility | More series (one per bucket) |
| More labels | Finer breakdowns | Multiplied series count, memory, cost |
| Short scrape interval | Faster detection, finer graphs | More load and storage |
| Long retention / downsampling | Long-term trends | Storage cost, or lost detail when downsampled |
| Pull (scrape) | Simple target discovery, up/down signal | Needs network reachability to every process |
Alternatives
- Logs for per-event detail; log-derived metrics exist but cost more.
- Traces for per-request timing; span metrics (RED from spans) can be generated in the collector.
- Continuous profiling for "which code uses the CPU" questions metrics cannot answer.
Failure modes
- Averages hiding tails: an average-latency dashboard stays calm while 5% of users wait seconds.
- Cardinality explosion: a user-id or URL label creates millions of series and takes the TSDB down.
- Averaging percentiles: averaging per-instance P99s is mathematically meaningless; aggregate histograms instead.
- Rate over counter resets handled wrongly, or gauges graphed as rates.
- Aggregation hiding one bad instance: a fleet-wide P99 can look fine while one instance is awful; break down by instance.
- Missing saturation signals: utilisation looks fine (60%) while a queue grows behind a single slow resource.
In production
Instrument every service with RED metrics as histograms, labelled by route template, status class and instance; add USE metrics for pools, queues, CPU and memory; expose runtime metrics (GC, threads). Keep label sets bounded and review cardinality. Build dashboards top-down (user journey → service → instance → resource) and alert on SLO burn, not on every metric.
Common misconceptions
- "Average latency is a good summary." It describes no one when latency is bimodal.
- "P50 is what most users experience." A page that makes 30 requests almost certainly includes one beyond the P95.
- "High utilisation means the resource is the problem." Check saturation: a 100% busy pool with an empty queue is fine; a growing queue is not.
- "More labels are free." Every new value is a new series.
Interview angle
Expect "what would you monitor for this service?" Answer with RED per service, USE per resource, latency as histograms with P95/P99, breakdowns by instance and route, and SLO-based alerts. Explain why averages and per-instance percentile averaging mislead, use Little's law to sanity-check numbers, and mention cardinality limits.
Model assumptions
Simulated: four app servers with 4 workers, 40 ms mean service time (lognormal, cv 0.3) and room for 8 waiting requests each, behind a round-robin balancer; the degraded server is 4× slower (about 25 req/s capacity). Arrivals are Poisson; network hops are 20 ms and 5 ms. Metric tiles show the simulation's exact percentiles over a 5-second window, not histogram estimates; metric names, labels and queries in the annotations are illustrative Prometheus-style examples, and the metrics database is scenery. Numbers show behaviour, not benchmarks.