Skip to the page

Observability for developers · core

Metric types, PromQL and cardinality

A counter only goes up, a gauge moves both ways, and a histogram counts observations into buckets. Use rate() on counters, histogram_quantile on bucket rates, and keep label values few.

The notes

What you must know

  • Counter: only increases and resets to 0 on restart (http_requests_total). Query it with rate() or increase(), which handle resets. A raw counter drops when pods restart.
  • Gauge: goes up and down (orders_in_queue, connections in use, memory). Query the value or avg_over_time and max_over_time. Never use rate() on a gauge.
  • Histogram: counts observations into buckets (_bucket{le="0.5"}) plus _sum and _count. p99 is histogram_quantile(0.99, sum by (le) (rate(x_bucket[5m]))). You can add buckets from many pods before the quantile.
  • Summary: the client computes quantiles. Exact per pod, but quantiles cannot be combined across pods, and averaging them gives a number that is the p99 of nothing.
  • Without sum by (le) you get one p99 per pod and label. Keep le and any label you want to split by, such as route.
  • Cardinality: every unique label combination is a new time series. One histogram label set makes about two dozen series (buckets, +Inf, _sum, _count). Unbounded labels (full path with ids, merchant id, error text, a client-supplied header) can make millions of series and run Prometheus out of memory.
  • Use the route template (/orders/:id), a status class, and a small fixed set of error types. Put ids and error text in logs and traces. Clients can send random header values on purpose, so never take a label from user input.
  • Use seconds and base units, and name counters with _total.

The red pen

Common traps: mistake → fix

  • Mistake: Averages show latency well

    Fix: They hide slow requests. Use percentiles from histograms.

  • Mistake: More labels means better detail

    Fix: Each value multiplies the series count. High-cardinality ids belong in logs and traces.

  • Mistake: I can use rate() on any metric

    Fix: rate() is for counters. Gauges use deriv or the plain value.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is the difference between a counter, a gauge and a histogram?
  2. How do you get p99 latency in PromQL?
  3. Why is averaging summary quantiles across pods wrong?
  4. Why can a merchant_id label kill Prometheus?
  5. Review this metrics code and PromQL dashboard and list what is wrong.

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview