Selectors & Vectors
Instant vectors, range vectors, matchers, and staleness
PromQL is a functional language: every expression evaluates to a value, and you build bigger expressions by wrapping smaller ones. There are only four value types, and two of them do almost all the work.
| Type | What it is | Example |
|---|---|---|
| Instant vector | A set of series, each with one sample at the evaluation time | http_requests_total |
| Range vector | A set of series, each with all samples in a time window | http_requests_total[5m] |
| Scalar | A single number | 0.005, time() |
| String | Rarely used | "hello" |
Graphs can only draw instant vectors. Range vectors are inputs to
functions like rate() that turn them back into instant vectors.
Instant vector selectors
Regexes are fully anchored: status=~"5" matches only the string
5, not 500. Use 5.. or 5.*.
A matcher on a label that doesn't exist compares against the empty
string. {env!="prod"} matches every series without an env label
too. And {foo=""} means "foo is absent." This surprises everyone
once. A selector must also have at least one matcher that can't match
the empty string, so {env=~".*"} alone is rejected.
How "instant" works: lookback
A query is evaluated at a specific time t. Scrapes don't happen at exactly t, so for each series Prometheus takes the most recent sample at or before t, looking back at most 5 minutes (the lookback delta). If there's no sample in that window, or the newest one is a stale marker, the series is absent from the result.
A range query (what a graph does) is just an instant query repeated at
every step across the time range. A 1-hour graph at 15s step runs 240
instant evaluations.
Range vector selectors
Add a duration in brackets to get every raw sample in the window ending at t:
http_requests_total{route="/api/orders"}[1m]
# result (one series, 12 samples at a 5s scrape interval):
http_requests_total{...route="/api/orders",status="200"}
18233 @1791783000
18291 @1791783005
...
18902 @1791783055Durations: ms, s, m, h, d, w, y, and combinations like
1h30m. The Prometheus UI's Table tab can show a range vector; the Graph
tab can't, because there's no single value per time step to draw.
Functions that take range vectors
Range vectors feed two families of functions:
Counter functions, covered next in Rates & Counters:
rate(http_requests_total[5m])
increase(http_requests_total[1h])_over_time functions, which aggregate each series over time (use
them on gauges):
min_over_time(up[1h]) == 0 is a good "flapping" detector: it catches a
target that went down for a single scrape and came back, which an alert
on up == 0 with for: 2m would never see.
Literals and time
time() # current evaluation time, Unix seconds
time() - process_start_time_seconds # uptime of each process
vector(1) # turn a scalar into a one-series vector
scalar(sum(up)) # turn a single-series vector into a scalarLab
Run each of these in the Prometheus UI and predict the number of result series before pressing Execute:
http_requests_total{job="demo-app"}
http_requests_total{job="demo-app", status=~"5.."}
http_requests_total{job="demo-app", route!="other"}
{job="demo-app", __name__=~".*_bucket"}
up{job=~"blackbox.*"}
count_over_time(up{job="demo-app"}[1m])The last one should be 12: one sample every 5 seconds for 60 seconds.
If it isn't, check the scrape_interval for that job in
config/prometheus.yml.