Binary Operators & Vector Matching
Ratios (error rate, cache hit rate, memory used vs limit) are the heart of most alerts. They need binary operators between two vectors, and the rules PromQL uses to pair series up: vector matching.
Operators
| Kind | Operators |
|---|---|
| arithmetic | + - * / % ^ |
| comparison | == != > < >= <= (filter by default; add bool to get 0/1) |
| set | and, or, unless |
trigonometric atan2 |
rarely needed |
Between a vector and a scalar, the operator applies to every series: linkly_cache_bytes / 1024 / 1024. Between two vectors, each series on the left is matched with the series on the right that has exactly the same labels (the metric name is ignored). Series without a partner disappear from the result.
The data
Two instances; requests by code; a cache hit/miss counter; and a linkly_build_info series per instance carrying the version:
python3 - > data.om <<'PY'
start = 1767225600
series = {
'http_requests_total{instance="api-1",code="302"}': 6, 'http_requests_total{instance="api-1",code="500"}': 0.06,
'http_requests_total{instance="api-2",code="302"}': 6, 'http_requests_total{instance="api-2",code="500"}': 0.9,
'linkly_cache_requests_total{instance="api-1",result="hit"}': 5.4, 'linkly_cache_requests_total{instance="api-1",result="miss"}': 0.6,
'linkly_cache_requests_total{instance="api-2",result="hit"}': 3, 'linkly_cache_requests_total{instance="api-2",result="miss"}': 3,
}
print("# TYPE http_requests_total counter")
print("# TYPE linkly_cache_requests_total counter")
for name, r in series.items():
for i in range(121):
print(f"{name} {r * 15 * i:g} {start + 15 * i}")
print("# TYPE linkly_build_info gauge")
for inst, ver in (("api-1", "1.4.2"), ("api-2", "1.5.0")):
for i in range(121):
print(f'linkly_build_info{{instance="{inst}",version="{ver}"}} 1 {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
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 'rate(http_requests_total{code="500"}[5m]) / rate(http_requests_total[5m])'
q 'rate(http_requests_total{code="500"}[5m]) / ignoring(code) sum without (code) (rate(http_requests_total[5m]))'
q 'sum by (instance) (rate(linkly_cache_requests_total{result="hit"}[5m])) / sum by (instance) (rate(linkly_cache_requests_total[5m])) < 0.8'
q 'sum by (instance) (rate(linkly_cache_requests_total{result="hit"}[5m])) / sum by (instance) (rate(linkly_cache_requests_total[5m])) < bool 0.8'> rate(http_requests_total{code="500"}[5m]) / rate(http_requests_total[5m])
{code="500", instance="api-1"} => 1
{code="500", instance="api-2"} => 1
> rate(http_requests_total{code="500"}[5m]) / ignoring(code) sum without (code) (rate(http_requests_total[5m]))
{instance="api-1"} => 0.009901
{instance="api-2"} => 0.1304
> sum by (instance) (rate(linkly_cache_requests_total{result="hit"}[5m])) / sum by (instance) (rate(linkly_cache_requests_total[5m])) < 0.8
{instance="api-2"} => 0.5
> sum by (instance) (rate(linkly_cache_requests_total{result="hit"}[5m])) / sum by (instance) (rate(linkly_cache_requests_total[5m])) < bool 0.8
{instance="api-1"} => 0
{instance="api-2"} => 1
- The first query looks right but pairs
{instance="api-1", code="500"}with the series of the same labels on the right: itself. Every 500 series is divided by itself, giving 1. (The 302 series on the right find no partner on the left and vanish.) - The fix: on the right, sum away
code; on the left, tell PromQL to ignorecodewhen matching (ignoring(code)). Now each instance's 500 rate is divided by its total rate: 1% on api-1, 13% on api-2. - A comparison filters: only api-2, whose cache hit ratio is 50%, survives
< 0.8. That's how alert expressions work: the alert fires for every series the expression returns. - With
bool, comparisons return 0 or 1 for every series instead: useful for counting or graphing "is it bad?".
on, ignoring, group_left
| Modifier | Meaning |
|---|---|
on(a, b) |
match using only labels a and b |
ignoring(c) |
match using all labels except c |
group_left(x) |
many-to-one: several left series may match one right series; copy label x from the right |
group_right(x) |
one-to-many, the mirror image |
Without a group_ modifier, matching must be one-to-one, or the query fails with "multiple matches for labels: many-to-one matching must be explicit". The classic many-to-one use is joining info metrics: attach the version from linkly_build_info to every request series.
python3 - > data.om <<'PY'
start = 1767225600
print("# TYPE http_requests_total counter")
for inst, code, r in (("api-1", "302", 6), ("api-1", "500", 0.06), ("api-2", "302", 6), ("api-2", "500", 0.9)):
for i in range(121):
print(f'http_requests_total{{instance="{inst}",code="{code}"}} {r * 15 * i:g} {start + 15 * i}')
print("# TYPE linkly_build_info gauge")
for inst, ver in (("api-1", "1.4.2"), ("api-2", "1.5.0")):
for i in range(121):
print(f'linkly_build_info{{instance="{inst}",version="{ver}"}} 1 {start + 15 * i}')
print("# TYPE up gauge")
for inst, v in (("api-1", 1), ("api-2", 1), ("api-3", 0)):
for i in range(121):
print(f'up{{job="linkly",instance="{inst}"}} {v} {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
q() { echo "> $1"; promtool query instant --time=2026-01-01T00:20:00Z http://127.0.0.1:9090 "$1" 2>&1 |
sed 's/ @\[.*//' | awk '$NF ~ /^[-+.0-9e]+$/ { $NF = sprintf("%.4g", $NF) } { print }' | sort; }
q 'rate(http_requests_total[5m]) * on(instance) linkly_build_info'
q 'rate(http_requests_total[5m]) * on(instance) group_left(version) linkly_build_info'
q 'sum by (version) (rate(http_requests_total{code="500"}[5m]) * on(instance) group_left(version) linkly_build_info)'
q 'up{job="linkly"} == 0'
q 'up{job="linkly"} unless on(instance) linkly_build_info'> rate(http_requests_total[5m]) * on(instance) linkly_build_info
query error: execution: multiple matches for labels: many-to-one matching must be explicit (group_left/group_right)
> rate(http_requests_total[5m]) * on(instance) group_left(version) linkly_build_info
{code="302", instance="api-1", version="1.4.2"} => 6
{code="302", instance="api-2", version="1.5.0"} => 6
{code="500", instance="api-1", version="1.4.2"} => 0.06
{code="500", instance="api-2", version="1.5.0"} => 0.9
> sum by (version) (rate(http_requests_total{code="500"}[5m]) * on(instance) group_left(version) linkly_build_info)
{version="1.4.2"} => 0.06
{version="1.5.0"} => 0.9
> up{job="linkly"} == 0
up{instance="api-3", job="linkly"} => 0
> up{job="linkly"} unless on(instance) linkly_build_info
up{instance="api-3", job="linkly"} => 0
- Without
group_left, the first query fails: two request series per instance match one build-info series. - With
group_left(version), every request series gets theversionlabel (multiplying by 1 leaves the value unchanged). Summing by version shows that the 500s come from 1.5.0: an instant "is the new release worse?" query, which is how canary analysis tools compare versions. unlesskeeps left series that have no match on the right:api-3is a target that is down, so it exposes no build info.andis the opposite (keep left series that do match), andormerges two vectors.
Test yourself
Why does rate(x{code="500"}[5m]) / rate(x[5m]) return 1 for every series?
- rate() is broken
- Each 500 series is matched with the right-hand series that has identical labels: itself
- The division is integer
- Because of extrapolation
What does group_left(version) do in rate(x[5m]) * on(instance) group_left(version) build_info?
- Groups the result by version
- Allows many left series to match one right series and copies
versiononto the result - Drops the version label
- Sorts by version
In an alerting rule, what does expr: error_ratio > 0.05 return when the ratio is 0.02?
- 0
- false
- Nothing (an empty vector), so the alert doesn't fire
- 0.02