Lesson 2 of 9 · 23m
PromQL Fluency: Selectors, Vectors, and Time Windows
Instant vectors, range vectors, matchers, offsets, and the difference between selecting data and calculating meaning.
PromQL is small enough to learn in an afternoon and subtle enough to embarrass you in an incident. The dividing line is whether you understand what kind of value an expression produces. Most broken alerts are not wrong because the arithmetic is exotic; they are wrong because someone compared the wrong shape of data.
Instant versus range vectors
up # instant vector: one current sample per series
up[5m] # range vector: five minutes of samples per seriesAn instant vector is a snapshot at evaluation time. A range vector is raw samples across a lookback window and is mainly useful as input to functions such as rate, increase, avg_over_time, or quantile_over_time.
Grafana time-series panels evaluate the same query at many points across the dashboard range. Alerts evaluate an instant expression on a schedule. Recording rules also evaluate instant expressions — even when those expressions internally consume a range.
Matchers narrow label sets
http_requests_total{job="api", status=~"5..", method!="GET"}
node_cpu_seconds_total{mode!~"idle|iowait"}=exact equality!=inequality=~regex match!~negative regex match
Regex is fully anchored in PromQL: status=~"5" does not mean "contains 5"; write status=~".*5.*" if you truly mean that. Most of the time you should not mean that.
Arithmetic preserves labels — sometimes
node_memory_MemAvailable_bytes / node_memory_MemTotal_bytesThis joins series only when both sides have the same label set. If one side has extra labels, the join silently drops matches or fails to produce what you expect. Use on and ignoring deliberately:
node_memory_MemAvailable_bytes
/ on(instance, job)
node_memory_MemTotal_bytesPromQL vector matching is exact. This is good. It forces you to say which labels define the same entity and which labels you are intentionally discarding.
Offset and rate windows answer different questions
rate(http_requests_total[5m])
http_requests_total offset 1hoffset shifts evaluation into the past. It is useful for week-over-week comparisons and for debugging "did this change after the deploy?" It is not a substitute for a correctly chosen range window.
A five-minute rate window is common because it balances noise and responsiveness. A one-minute window spikes with every scrape hiccup; a one-hour window hides incidents until customers have already noticed.
Absence is data too
up == 0
absent(up{job="payment-worker"})up == 0 sees a target that exists and is failing. absent() is true when no series matches at all. These are not interchangeable: an exporter that disappeared does not return up == 0 for a job Prometheus no longer scrapes.
Field notes
- If a query returns "no data," distinguish missing target, missing metric, wrong matcher, and aggregation empty-set before blaming storage.
- Prefer
{}matchers narrow enough to be meaningful but broad enough to survive label additions. - Never use regex as a substitute for inventory.
job=~".*api.*"is usually a sign service discovery labels are sloppy. - In Grafana, inspect the raw query at the same evaluation time before assuming the panel calculation is wrong.
Was this lesson helpful?