Learn Labs
PromQL

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

sum(rate(http_requests_total[5m]))one number: total req/ssum by (route) (rate(http_requests_total[5m]))one series per routesum without (instance, method) (rate(http_requests_total[5m]))drop these labels, keep the restavg by (instance) (instance:node_cpu_utilisation:rate5m)meanmax by (job) (scrape_duration_seconds)worst target per jobcount by (job) (up == 1)how many targets are upcount(count by (route) (http_requests_total))distinct values of a labeltopk(5, sum by (route) (rate(http_requests_total[5m])))5 busiest routesbottomk(3, probe_duration_seconds)3 fastest probesquantile(0.9, rate(http_requests_total[5m]))90th percentile *across series*count_values("version", app_build_info)how many instances per valuestddev(rate(http_requests_total[5m]))spread across series

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.

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 filtered

Comparisons 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:

up == 0 and on (job) count by (job) (up) > 1and: keep left if a match exists on the rightrate(a[5m]) or vector(0)or: left, plus right where left has no matchup unless on (instance) probe_success == 1unless: left where right has NO match

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) b

One-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 same instance.
  • 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 the version label 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.

//5xx rate{route, status}total rate{route}no resultlabels differsum by (route){route}sum by (route){route}ratio per routeone series each

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.

On this page