PromQL rate() and Histograms Without the Confusion
Your Graph tab from the last lesson shows http_requests_total as lines that only climb. A teammate asks how many requests per second shop-demo handles right now, and how slow its slowest requests are. This lesson covers PromQL rate and its partner for latency, histogram_quantile(): per-second speeds, error ratios, averages and an estimated p95.
rate(): from a running total to a speed
As Counters, Gauges and Histograms explained, a counter is a running total that only goes up, and starts again from zero when the app restarts. Its raw value mostly tells you how long the app has run. rate() gives its speed: the average increase per second over a time window.
You write the window as a range selector from PromQL Basics. rate(http_requests_total[5m]) takes each series' samples from the last 5 minutes, about 20 at the lab's 15-second scrape interval, and returns one number per series, as the diagram shows.
The first and last samples rarely sit exactly on the window's edges, so Prometheus also extrapolates: it stretches the increase to cover the whole window. That makes a rate an estimate, rarely a round number.
A counter reset is the drop to zero when the app restarts, and rate() treats any drop as one. If a series goes 550, then 25, it assumes a restart from 0 and counts that step as +25, not −525. Requests served between the last scrape and the restart are lost, so a restart may cause a small dip, never a negative rate.
So never graph a raw counter: its height depends on uptime, and every restart draws a cliff. A rate graph shows the slope itself, in requests per second, and stays smooth across restarts.
Requests per second by route, and an error ratio
rate(http_requests_total[5m]) returns one rate per series, about seven here. To see how busy each route is, wrap it in an aggregation. Always take the rate first and aggregate second, because rate() must see each counter on its own to spot its resets:
sum by (route) (rate(http_requests_total[5m]))
The Table tab shows one row per route, in requests per second. Here it is with illustrative, rounded values; /products leads because loadgen asks for it most often:
{route="/"} 0.47
{route="/checkout"} 1.18
{route="/health"} 0.1
{route="/metrics"} 0.067
{route="/products"} 2.84
{route="/report"} 0.24
An error ratio is the share of requests that failed: here, the rate of failed checkouts divided by the rate of all checkouts. Wrapping each side in sum() leaves one label-free series per side, so the division has one pair to match:
sum(rate(http_requests_total{route="/checkout", status="500"}[5m]))
/
sum(rate(http_requests_total{route="/checkout"}[5m]))
You should see about 0.03, varying a little, because the lab's CHECKOUT_ERROR_RATE makes about 3% of checkouts fail. For all routes, drop the route filters and match status=~"5.." on top.
Histogram series: _bucket, _sum and _count
shop-demo also times each request in a histogram, http_request_duration_seconds. As the metric types lesson explained, a histogram counts observations into cumulative buckets, each with an upper bound in its le ("less than or equal") label. Prometheus stores three kinds of series per method and route, shown trimmed and with illustrative values:
http_request_duration_seconds_bucket{le="0.25", method="POST", route="/checkout"} 2045
http_request_duration_seconds_bucket{le="+Inf", method="POST", route="/checkout"} 2170
http_request_duration_seconds_sum{method="POST", route="/checkout"} 453.5
http_request_duration_seconds_count{method="POST", route="/checkout"} 2170
Each _bucket series counts requests at or below its bound, _count counts all requests, and _sum adds up their durations in seconds. All three are counters, so rate() works on them too: rate() of _sum is seconds of work per second, and of _count requests per second. Divide them for the average duration over the window:
rate(http_request_duration_seconds_sum[5m]) / rate(http_request_duration_seconds_count[5m])
Both sides keep the same labels, so Prometheus divides each route's sum by that route's count, giving one average per method and route. But as Latency and Percentiles showed, an average hides the slow few.
histogram_quantile(): estimating p95
A quantile, or percentile, is the value a given share of requests stayed at or below: p95, the 0.95 quantile, is the duration that 95% of requests did not exceed. histogram_quantile() estimates it from bucket counts, taking the quantile as a fraction and then the buckets:
histogram_quantile(0.95, sum by (le) (rate(http_request_duration_seconds_bucket[5m])))
rate() turns each bucket into requests per second over the last 5 minutes, so the answer describes recent traffic. sum by (le) then adds up the buckets that share a bound, across every route and method, into one combined histogram. Adding buckets is allowed; averaging percentiles is not.
The le in by (le) matters because histogram_quantile() reads each bucket's bound from that label. Keep other labels to split them out: sum by (le, route) gives one p95 per route.
The function finds the first bucket holding at least 95% of the requests and assumes the requests inside it are spread evenly. The answer is an interpolation between two bucket bounds, only as precise as the bounds are close. If the 95% mark lands in the +Inf bucket, you get the highest finite bound, 5 seconds here.
Common mistakes, and which function fits
The first mistake is rate() on a gauge, a value that goes up and down, such as http_requests_in_flight. rate() treats every fall as a counter reset and returns a positive number that measures nothing. The Table tab then shows a yellow Query notice that begins:
PromQL info: metric might not be a counter, name does not end in _total/_sum/_count/_bucket: "http_requests_in_flight"
The check only reads the name, so it is a hint. Read a gauge as it is, or summarise it over time with max_over_time(http_requests_in_flight[5m]).
The second is a window that is too short. rate() needs at least two samples, and since Prometheus 3 a window excludes its start, so [15s] at a 15-second scrape interval usually holds one: Empty query result, or a graph full of gaps. A common rule of thumb is at least four scrape intervals, [1m] here, so one missed scrape does not break the line. Longer windows are smoother but slower to react.
The third is forgetting le. sum(rate(http_request_duration_seconds_bucket[5m])) adds every bucket into one total with no le label, so histogram_quantile() has no bound to read: no data, and a red Query warning that the bucket label "le" is missing. The flowchart sums up which function fits each type.
Check yourself
A teammate graphs rate(http_requests_in_flight[5m]) and sees a steady 0.3. What does that tell them?
Nothing reliable: it is a gauge, so every normal fall is mistaken for a counter reset and added to the "increase". Graph the gauge itself, or max_over_time() of it.
Why does histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket[5m]))) return no data?
sum() without by (le) adds every bucket into one series with no le label, so the function has no bucket bounds to work with. Write sum by (le).
le="0.25" holds 90 of 100 requests and le="0.5" holds all 100. What p95 does histogram_quantile() estimate, and why is it only an estimate?
0.375 seconds: the 95th request is the 5th of the 10 in that bucket, halfway through it. The function assumes they are spread evenly, but they could all sit near 0.26 or near 0.5.