Sub topic 4
Counters, Gauges, Histograms, and Summaries
Assume you can use the Linux command line, read YAML, understand basic TCP and HTTP, and query Prometheus targets. This chapter focuses on how Prometheus metric types behave and how that behavior affects PromQL.
Why metric types matter
A metric is a named measurement. Prometheus stores each measurement as a time series, which is a sequence of timestamped values. The metric type tells you how to interpret those values.
| Type | Meaning | Typical examples | Useful operation |
|---|---|---|---|
| Counter | A value that increases, except when it resets | Requests served, errors, bytes sent | `rate()` or `increase()` |
| Gauge | A value that can rise or fall | Memory used, temperature, queue length | Current value, average, maximum |
| Histogram | Observations counted into value ranges | Request duration, response size | Percentiles and distributions |
| Summary | Client-calculated quantiles and totals | Request duration percentiles | Per-instance quantiles |
Counters
A counter records a total since the process started. It should not be used as a direct “requests per second” value because its number keeps growing.
Processes can restart, causing a counter to reset to zero. The rate() function detects normal resets and calculates the average per-second increase over a time window.
# Average requests per second during the last five minutes
sum by (job) (rate(http_requests_total[5m]))
# Requests received during the last hour
increase(http_requests_total[1h])Use a counter for events, not for a current state. For example, http_requests_total is a counter, while http_requests_in_progress should be a gauge.
Gauges
A gauge represents a value that can move in either direction. Prometheus does not need to calculate a rate before displaying it.
# Current memory usage in bytes
node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes
# Highest queue length during the last ten minutes
max_over_time(application_queue_length[10m])Do not apply rate() to a gauge. A falling gauge is normal and does not represent a counter reset.
Histograms
A histogram records many observations in buckets. A classic Prometheus histogram creates related series:
request_duration_seconds_bucketcounts observations less than or equal to each bucket limit.request_duration_seconds_sumstores the sum of all observed values.request_duration_seconds_countstores the number of observations.
The bucket counts are cumulative. The +Inf bucket contains every observation. To calculate a percentile, combine buckets with rate() and use histogram_quantile().
# Approximate 95th-percentile request duration per job
histogram_quantile(
0.95,
sum by (job, le) (
rate(request_duration_seconds_bucket[5m])
)
)The le label means “less than or equal to.” Keep it in the aggregation because histogram_quantile() needs the bucket boundaries.
Summaries
A summary calculates quantiles inside the application process. It usually exposes series with a quantile label, plus _sum and _count series.
Summaries can provide efficient per-instance quantiles, but their quantiles cannot be meaningfully aggregated across servers. A 95th percentile of two servers is not calculated by averaging their two 95th-percentile values. Histograms are usually the better choice when you need a service-wide percentile.
Common mistakes
- Using a counter’s raw value as a current rate.
- Applying
rate()to a gauge. - Aggregating summary quantiles across instances.
- Omitting
lewhen aggregating histogram buckets. - Choosing buckets that are too broad for the latency range you need.
Exercise
An application exposes request duration as a summary on five servers, and you need one 95th-percentile value for the whole service. What should you change?
Show answer
Expose a histogram instead, then aggregate its bucket series by the required labels and apply histogram_quantile().