Counters, Gauges and Histograms: Metric Types Explained
You add your first metric to a checkout service, and the client library immediately asks you to choose: Counter, Gauge, Histogram or Summary. Choose badly and your dashboard shows nonsense, such as a request total that crashes to zero at every deploy, or a latency number you cannot combine across servers. Counters, gauges and histograms are the three types you will use almost every day, and each one answers a different kind of question.
Counters: numbers that only go up
A counter is a running total that only increases: requests served, errors returned, bytes sent. Its one exception is a restart. The count lives in the program's memory, so when the process restarts, the counter starts again from zero. By convention a counter's name ends in _total, as in http_requests_total, and any unit goes in the name too, as in _seconds or _bytes.
The raw value of a counter is rarely what you want. "4,281,977 requests" says more about how long the process has been running than about how busy it is now. Instead you graph the counter's rate, meaning how fast it is going up. In PromQL, the query language of Prometheus, you write it like this:
rate(http_requests_total[5m])
Read it as "the average increase per second over the last 5 minutes". If the counter went from 1,000 to 4,000 in five minutes, the rate is 10 requests per second. rate() also spots when a counter drops, treats the drop as a restart, and keeps counting from the new low value, so a deploy does not show up as a giant negative spike on your graph.
Gauges: numbers that go up and down
A gauge is a value that can rise and fall, and you read it as it is: memory in use, items waiting in a queue, open connections, the temperature of a disk. The fuel gauge in the analogy is exactly this. You care about the level right now, not about how much it has moved in total.
Because the current value is the answer, you graph a gauge directly, or summarise it over a window with functions such as max_over_time() and avg_over_time(). Never apply rate() to a gauge. Every time the value falls, rate() would assume a restart and produce a meaningless result.
A quick test helps. Ask "what is it right now?" and see if the answer means something. "Memory in use: 1.2 GB" is useful on its own, so it is a gauge. "Requests: 4,281,977" mostly is not, so it is a counter.
Histograms: how values are spread out
Some questions need more than a total or a level. "How long do requests take?" has many answers at once: most take 50 ms, a few take 2 seconds. A histogram captures that spread. It sorts each observation, for example one request's duration, into buckets. Each bucket has an upper bound called le, short for "less than or equal to".
The buckets are cumulative. A 0.05-second request is counted in the le="0.1" bucket and also in every larger bucket, just as the 5-minute trip is both under 10 and under 30 minutes. The last bucket, le="+Inf", counts every observation. Next to the buckets, a histogram exposes _sum, the total of all observed values, and _count, the number of observations.
Here is a real histogram from Prometheus's own /metrics page, shortened in the middle. It measures how long Prometheus takes to answer HTTP requests, split by a handler label:
# HELP prometheus_http_request_duration_seconds Histogram of latencies for HTTP requests.
# TYPE prometheus_http_request_duration_seconds histogram
prometheus_http_request_duration_seconds_bucket{handler="/metrics",le="0.1"} 2
prometheus_http_request_duration_seconds_bucket{handler="/metrics",le="0.2"} 2
...
prometheus_http_request_duration_seconds_bucket{handler="/metrics",le="+Inf"} 2
prometheus_http_request_duration_seconds_sum{handler="/metrics"} 0.010059459
prometheus_http_request_duration_seconds_count{handler="/metrics"} 2
Both requests to /metrics finished in under 0.1 seconds, so every bucket holds 2, and together they took about 10 ms. Each bucket is its own time series, so this histogram costs ten series per handler plus _sum and _count. In return, percentiles such as "95% of requests were faster than X" can be estimated later, at query time, with histogram_quantile(). Prometheus also has a newer native histogram format that picks bucket bounds automatically; this lesson sticks to the classic form you see in the text output.
Summaries, and choosing the right type
The fourth type, the summary, also measures spread, but it calculates percentiles inside the app, for example the median and the 99th percentile over a sliding window (10 minutes by default in the Go client). It exposes them as series with a quantile label, plus _sum and _count. The catch is that those percentiles cannot be combined: averaging the 99th percentile of ten servers does not give you the 99th percentile of the fleet.
Histogram buckets, by contrast, are plain counts, and counts from many servers can simply be added together before you estimate a percentile. That is why the Prometheus guidance on histograms and summaries usually points you to histograms.
Choosing a type comes down to three quick questions. First, can the value go down? Then it is a gauge. Second, do you need the spread, as for latency or response size? Then you need a histogram or a summary. Third, will you ever combine it across servers? Almost always yes, so pick the histogram. Everything else is a count of events, which makes it a counter.
Check yourself
Which type fits "number of orders waiting in the payment queue"?
A gauge, because the queue shrinks as orders are processed and you want its current level.
A histogram shows le="0.5" with 7 and le="1" with 9. How many requests took more than 0.5 s but at most 1 s?
Two, because buckets are cumulative: subtract the smaller bucket from the larger one, 9 minus 7.
Why does rate() cope with a counter restart but give nonsense on a gauge?
It treats any drop as a reset to zero; that is true for a restarted counter, but a gauge drops all the time in normal use.