What are the four core Prometheus metric types (counter, gauge, histogram, summary)?
Understand the four Prometheus metric types - counter, gauge, histogram, and summary - with clear examples, when to use each, and how to query them.
Expected Interview Answer
Prometheus defines four metric types: a counter (a value that only increases), a gauge (a value that can go up or down), a histogram (buckets that count observations by size), and a summary (client-side computed quantiles over a sliding window).
A counter tracks cumulative totals like requests served and is used with rate(). A gauge measures a point-in-time value like memory in use or queue depth. A histogram buckets observations such as request durations so you can compute quantiles server-side with histogram_quantile(). A summary calculates configurable quantiles inside the client, which is cheaper to query but cannot be aggregated across instances the way histograms can.
- Counters make rates and per-second trends easy with rate()
- Gauges naturally represent fluctuating current values
- Histograms allow aggregatable, server-side quantiles across instances
- Summaries give precise quantiles with low query cost
- Choosing the right type keeps queries correct and cheap
AI Mentor Explanation
A counter is the team's running total, which only ever climbs as runs are added and never falls during the innings. A gauge is the current run rate, rising and dropping over after over. A histogram is a tally of how many deliveries fell into speed bands like slow, medium and fast. A summary is the scorer pre-computing the bowler's median and 90th-percentile pace so you can read it instantly.
Step-by-Step Explanation
Step 1
Pick counter for totals
Use a counter for things that only grow - requests, errors, bytes sent - and query it with rate().
Step 2
Pick gauge for levels
Use a gauge for values that rise and fall, like memory usage, temperature, or queue depth.
Step 3
Pick histogram for distributions
Use a histogram with sensible buckets for latencies or sizes, then apply histogram_quantile().
Step 4
Pick summary for local quantiles
Use a summary when you want client-computed quantiles and don't need cross-instance aggregation.
Step 5
Instrument correctly
Register the metric with the client library, name it with a unit suffix, and increment or observe in code.
What Interviewer Expects
- Correct definition of all four types
- Knowing counters pair with rate()
- Understanding histogram buckets and histogram_quantile()
- The key histogram-vs-summary aggregation difference
- Naming conventions like _total and _seconds suffixes
Common Mistakes
- Using a counter for values that can decrease
- Trying to aggregate summary quantiles across instances
- Reading a counter's raw value instead of its rate()
- Choosing poor histogram bucket boundaries
Best Answer (HR Friendly)
“Prometheus has four ways to record numbers: a counter that only goes up, a gauge that can go up or down, a histogram that groups measurements into ranges, and a summary that reports things like percentiles. You pick whichever matches what you're measuring.”
Code Example
from prometheus_client import Counter, Gauge, Histogram, Summary
requests = Counter('http_requests_total', 'Total HTTP requests')
inflight = Gauge('http_inflight_requests', 'In-flight requests')
latency = Histogram('http_request_duration_seconds', 'Request latency')
resp_size = Summary('http_response_size_bytes', 'Response size')
requests.inc()
inflight.inc(); inflight.dec()
latency.observe(0.23)
resp_size.observe(1024)Follow-up Questions
- When would you choose a histogram over a summary?
- How does histogram_quantile() estimate a percentile?
- Why can't summary quantiles be aggregated across instances?
- What naming suffix conventions do counters and histograms use?
- How does rate() handle counter resets?
MCQ Practice
1. Which metric type should track a value that can both increase and decrease?
A gauge represents a point-in-time value that can go up or down, like memory usage or queue depth.
2. Which type lets you compute aggregatable quantiles server-side across many instances?
Histograms expose buckets that can be summed across instances and fed to histogram_quantile().
3. How should you typically query a counter?
Counters are cumulative, so rate() gives the meaningful per-second change and handles resets.
Flash Cards
Counter — A cumulative value that only increases (or resets to 0); queried with rate().
Gauge — A value that can go up or down, like memory in use or temperature.
Histogram — Buckets counting observations by size; supports server-side aggregatable quantiles.
Summary — Client-computed quantiles over a window; low query cost but not aggregatable across instances.