TExcelTAD

Aggregation Operators

A query like rate(http_requests_total[5m]) returns one series per instance, route and code: seven for linkly's small dataset, thousands in a real cluster. Aggregation operators collapse them into the few numbers you want on a dashboard.

Operator Result per group
sum, avg, min, max the obvious
count number of series
group 1 (useful to list label combinations)
stddev, stdvar spread
topk(k, …), bottomk(k, …) the k largest / smallest series, labels intact
quantile(φ, …) φ-quantile across series (not over time!)
count_values("label", …) how many series have each value
limitk(k, …), limit_ratio(r, …) a sample of series (newer; for exploring huge results)

Each takes an optional grouping clause: by (labels) keeps only those labels, without (labels) drops those and keeps the rest.

On the linkly data

bash · try it yourself
python3 - > data.om <<'PY'
start = 1767225600
per_sec = {
    ("api-1", "/{code}", "302"): 6, ("api-1", "/{code}", "404"): 0.4, ("api-1", "/links", "201"): 1,
    ("api-2", "/{code}", "302"): 6, ("api-2", "/{code}", "404"): 0.4, ("api-2", "/links", "201"): 1,
    ("api-2", "/{code}", "500"): 0.6,
}
print("# TYPE http_requests_total counter")
for (inst, route, code), r in per_sec.items():
    for i in range(121):
        print(f'http_requests_total{{job="linkly",instance="{inst}",route="{route}",code="{code}"}} {r * 15 * i:g} {start + 15 * i}')
print("# EOF")
PY
promtool tsdb create-blocks-from openmetrics data.om data >/dev/null
echo "global: {}" > prometheus.yml
prometheus --config.file=prometheus.yml --storage.tsdb.path=data --storage.tsdb.retention.time=100y \
  --web.listen-address=127.0.0.1:9090 >prom.log 2>&1 &
for i in $(seq 100); do curl -sf 127.0.0.1:9090/-/ready >/dev/null && break; sleep 0.2; done
# print values with 4 significant digits (rate() results carry floating-point noise like 5.999999999999999)
q() { echo "> $1"; promtool query instant --time=2026-01-01T00:20:00Z http://127.0.0.1:9090 "$1" |
      sed 's/ @\[.*//' | awk '$NF ~ /^[-+.0-9e]+$/ { $NF = sprintf("%.4g", $NF) } { print }' | sort; }

q 'sum(rate(http_requests_total[5m]))'
q 'sum by (instance) (rate(http_requests_total[5m]))'
q 'sum without (instance, job) (rate(http_requests_total[5m]))'
q 'count by (instance) (http_requests_total)'
q 'topk(2, sum by (route, code) (rate(http_requests_total[5m])))'
q 'max by (route) (rate(http_requests_total[5m]))'
> sum(rate(http_requests_total[5m]))
{} => 15.4
> sum by (instance) (rate(http_requests_total[5m]))
{instance="api-1"} => 7.4
{instance="api-2"} => 8
> sum without (instance, job) (rate(http_requests_total[5m]))
{code="201", route="/links"} => 2
{code="302", route="/{code}"} => 12
{code="404", route="/{code}"} => 0.8
{code="500", route="/{code}"} => 0.6
> count by (instance) (http_requests_total)
{instance="api-1"} => 3
{instance="api-2"} => 4
> topk(2, sum by (route, code) (rate(http_requests_total[5m])))
{code="201", route="/links"} => 2
{code="302", route="/{code}"} => 12
> max by (route) (rate(http_requests_total[5m]))
{route="/links"} => 1
{route="/{code}"} => 6

Reading it:

  • The service handles 15.4 requests/s in total; api-2 does 8 of them, because it also serves the 500s.
  • sum without (instance, job) keeps route and code: the fleet's traffic per route and status, whatever the number of pods. Prefer without in recording rules and shared dashboards: if someone adds a useful label (say region), without keeps it, while by silently drops it.
  • topk keeps every label of the winning series, so you can see which ones are busiest (here: redirects and link creation; the lines are sorted by label here, not by value). In a graph, topk is evaluated at every step, so the set of series can change along the time axis.
  • count_values("label", expr) turns values into a label: how many series have each value. For "how many pods run each version", count by (version) (linkly_build_info) is the clearer query.

Aggregation over time vs across series

Aggregation operators work across series at one instant. To aggregate one series over time, use the *_over_time functions on a range vector: max_over_time(linkly_queue_items[1h]), avg_over_time(…), quantile_over_time(0.95, …). They come up in Useful Functions. Combining both is common:

promql
# the busiest minute of the last day for the whole service, using a subquery
max_over_time( sum(rate(http_requests_total{job="linkly"}[1m]))[1d:1m] )

[1d:1m] is a subquery: evaluate the inner expression every minute for a day, producing a range vector that max_over_time can read. Subqueries are expensive; a recording rule is usually the better answer for anything a dashboard runs often.

The order of operations

The rule from the previous lesson, generalised: rate, then aggregate; ratio of sums, not sum of ratios:

promql
# error ratio of the whole service: right
sum(rate(http_requests_total{code=~"5.."}[5m])) / sum(rate(http_requests_total[5m]))

# wrong: the average of per-instance ratios; an idle pod that failed 1 of 2 requests
# weighs as much as a busy pod that served 10,000
avg(
  sum by (instance) (rate(http_requests_total{code=~"5.."}[5m]))
    / sum by (instance) (rate(http_requests_total[5m]))
)

Test yourself

Which query gives requests per second per route across all pods?

  1. rate(http_requests_total[5m])
  2. sum by (route) (rate(http_requests_total[5m]))
  3. sum(http_requests_total) by (route)
  4. rate(sum by (route) (http_requests_total)[5m])

Why prefer sum without (instance) over sum by (route, code) in shared recording rules?

  1. It's faster
  2. It keeps labels added later (such as region) instead of silently dropping them
  3. by is deprecated
  4. They return different values

What is the difference between max(x) and max_over_time(x[1h])?

  1. None
  2. max() is across series at one instant; max_over_time is over time for each series
  3. max_over_time is across series
  4. max() needs a range vector