Aggregation & Joins
sum by, without, topk, binary operators, and vector matching
Raw selectors give you one series per label combination: dozens or thousands of lines. Aggregation collapses them into the handful you actually want to look at, and binary operators combine series into ratios and joins.
Aggregation operators
by (labels) keeps only the listed labels. without (labels) keeps
everything except the listed ones. Prefer without in recording rules
and alerts: it preserves labels you didn't know about, like env or
team added by service discovery, which by would silently drop.
topk is evaluated independently at every step of a graph. On a
range graph, topk(5, ...) can show 15 different series, because a
different 5 were on top at different moments. For "the 5 busiest
routes over the last hour", rank by an hour-long rate in an instant
query, or use Grafana's "Sort by" and "Limit" table transforms.
Arithmetic and comparison
Operators work between a vector and a scalar, applying to every series:
node_memory_MemAvailable_bytes / 1024 / 1024 / 1024 # bytes -> GiB
rate(http_requests_total[5m]) * 60 # per minute
http_requests_in_flight > 10 # FILTER: only series above 10
http_requests_in_flight > bool 10 # 0 or 1 per series, nothing filteredComparisons filter: x > 10 returns only the series where the
condition holds, with their original values. That's the basis of every
alerting rule: an alert fires for each series the expression returns.
Add bool to get 0/1 instead of filtering.
Logical/set operators work on label sets:
or vector(0) is a common trick to make "no errors" show as 0 instead of
an empty panel. The stack-health dashboard uses it for "Targets down."
Vector matching
When both sides of an operator are vectors, Prometheus has to decide which series on the left pairs with which on the right. By default it pairs series with exactly the same label set (ignoring the metric name).
# works: both sides are summed by route, so labels are exactly {route}
sum by (route) (rate(http_requests_total{status=~"5.."}[5m]))
/
sum by (route) (rate(http_requests_total[5m]))
# returns NOTHING: left has {route,status}, right has {route}; no exact matches
rate(http_requests_total{status=~"5.."}[5m]) / sum by (route) (rate(http_requests_total[5m]))When label sets differ, tell Prometheus which labels to match on:
# match only on these labels
a / on (route) b
# match on everything except these labels
a / ignoring (status) bOne-to-many: group_left and group_right
on/ignoring alone requires one-to-one matches. If several series
on the left match one series on the right, you need group_left. This
is the "join" pattern, and its most common use is attaching labels from an
info metric:
# request rate per route, with the app version attached as a label
sum by (instance, route) (rate(http_requests_total{job="demo-app"}[5m]))
* on (instance) group_left (version)
app_build_info{job="demo-app"}Reading it piece by piece:
* on (instance): pair series that share the sameinstance.group_left: the left side may have many series per match (one per route); the right side has exactly one (the info metric).(version): copy theversionlabel from the right side onto the result.- Multiplying by
app_build_info, whose value is always 1, leaves the rates unchanged.
The same pattern attaches node_uname_info (kernel version) to node
metrics, or kube_pod_labels (team, app) to container metrics in
Kubernetes.
group_left fails with "many-to-many matching not allowed" when
the right side has more than one series for the same matching labels.
That happens during a rolling deploy: for a moment two versions are
running, so app_build_info has two series per instance with different
versions. Make sure the on (...) labels truly identify one series
on the "one" side.
Operator precedence
From highest to lowest: ^, then * / % atan2, then + -, then
comparisons (== != <= < >= >), then and unless, then or. When in
doubt, add parentheses. A query someone else can't read during an
incident is a bug.
Lab
# 1. requests per second per route, busiest first (Table tab)
sort_desc(sum by (route) (rate(http_requests_total{job="demo-app"}[5m])))
# 2. error ratio per route (note: by (route) on BOTH sides)
sum by (route) (rate(http_requests_total{job="demo-app", status=~"5.."}[5m]))
/ sum by (route) (rate(http_requests_total{job="demo-app"}[5m]))
# 3. join the version on
sum by (instance) (rate(http_requests_total{job="demo-app"}[5m]))
* on (instance) group_left (version) app_build_info
# 4. which jobs have ANY target down?
count by (job) (up == 0)
# 5. CPU busy percentage, per mode, excluding idle
sum by (mode) (rate(node_cpu_seconds_total{mode!="idle"}[5m]))
/ scalar(count(count by (cpu) (node_cpu_seconds_total)))For query 2, try removing by (route) from the denominator only and
see the result disappear. That's vector matching failing quietly.