PromQL Basics: Selectors & Vectors
PromQL is Prometheus's query language. Grafana panels, alert rules and recording rules are all PromQL expressions, so it is the most important skill in this course. This section runs every query against a real Prometheus holding 30 minutes of linkly traffic.
The data
The sessions in this section start with the same few lines (the lab recipe): generate 30 minutes of counters for two linkly instances, backfill them, start Prometheus. api-2 also returns some 500s:
python3 - > data.om <<'PY'
start = 1767225600 # 2026-01-01T00:00:00Z
per_sec = { # (instance, route, code): requests per second
("api-1", "/{code}", "302"): 6, ("api-1", "/{code}", "404"): 0.4, ("api-1", "/links", "201"): 1,
("api-2", "/{code}", "302"): 6, ("api-2", "/{code}", "404"): 0.4, ("api-2", "/links", "201"): 1,
("api-2", "/{code}", "500"): 0.6,
}
print("# TYPE http_requests_total counter")
for (inst, route, code), r in per_sec.items():
for i in range(121): # 30 minutes, one sample every 15 s
print(f'http_requests_total{{job="linkly",instance="{inst}",route="{route}",code="{code}"}} {r * 15 * i:g} {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" | sort; }
q 'http_requests_total{instance="api-2", route="/{code}"}'
q 'http_requests_total{code=~"4..|5.."}'
q 'http_requests_total{route!="/links", code!~"3.."}'> http_requests_total{instance="api-2", route="/{code}"}
http_requests_total{code="302", instance="api-2", job="linkly", route="/{code}"} => 7200 @[1767226800]
http_requests_total{code="404", instance="api-2", job="linkly", route="/{code}"} => 480 @[1767226800]
http_requests_total{code="500", instance="api-2", job="linkly", route="/{code}"} => 720 @[1767226800]
> http_requests_total{code=~"4..|5.."}
http_requests_total{code="404", instance="api-1", job="linkly", route="/{code}"} => 480 @[1767226800]
http_requests_total{code="404", instance="api-2", job="linkly", route="/{code}"} => 480 @[1767226800]
http_requests_total{code="500", instance="api-2", job="linkly", route="/{code}"} => 720 @[1767226800]
> http_requests_total{route!="/links", code!~"3.."}
http_requests_total{code="404", instance="api-1", job="linkly", route="/{code}"} => 480 @[1767226800]
http_requests_total{code="404", instance="api-2", job="linkly", route="/{code}"} => 480 @[1767226800]
http_requests_total{code="500", instance="api-2", job="linkly", route="/{code}"} => 720 @[1767226800]
Selectors and matchers
A selector is a metric name with optional label matchers in braces. Every returned series must satisfy all matchers:
| Matcher | Meaning |
|---|---|
code="500" |
equal |
code!="500" |
not equal (also matches series without the label) |
code=~"5.." |
regex match, fully anchored (RE2 syntax) |
| `code!~"2.. | 3.."` |
The metric name is itself a label, __name__, so {__name__=~"http_.*", job="linkly"} selects every http_* metric. A selector must contain at least one matcher that doesn't match the empty string: {code=~".*"} alone is rejected because it would match every series in the database.
Instant vectors and range vectors
The queries above return an instant vector: one value per series at the evaluation time (00:20:00). Add a duration in brackets and you get a range vector: all the raw samples of each series in that window, ending at the evaluation time:
python3 - > data.om <<'PY'
start = 1767225600
print("# TYPE http_requests_total counter")
for i in range(121):
print(f'http_requests_total{{job="linkly",instance="api-2",route="/{{code}}",code="500"}} {0.6 * 15 * i:g} {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"; }
q 'http_requests_total[1m]'
q 'http_requests_total offset 10m'
q 'http_requests_total @ 1767226200'> http_requests_total[1m]
http_requests_total{code="500", instance="api-2", job="linkly", route="/{code}"} =>
693 @[1767226755]
702 @[1767226770]
711 @[1767226785]
720 @[1767226800]
> http_requests_total offset 10m
http_requests_total{code="500", instance="api-2", job="linkly", route="/{code}"} => 360 @[1767226800]
> http_requests_total @ 1767226200
http_requests_total{code="500", instance="api-2", job="linkly", route="/{code}"} => 360 @[1767226800]
[1m]returned the four samples scraped in the minute up to 00:20:00 (the window's start is excluded, its end included), eachvalue @timestamp. You can't graph a range vector directly; you pass it to a function such asrate(), which turns it back into an instant vector (next lesson).offset 10mshifts the window back in time: 360 is the value at 00:10:00, reported at the evaluation time (@[1767226800], 00:20:00). Great for "compared to last week":… offset 1w.@ 1767226200pins the evaluation to an absolute Unix time (00:10:00), whatever time the query runs at.@ start()and@ end()refer to the range of a graph.
Durations are written ms, s, m, h, d, w, y, and can be combined: 1h30m.
Range queries
Graphs use a range query: the same expression evaluated at every step between start and end. With promtool:
python3 - > data.om <<'PY'
start = 1767225600
print("# TYPE linkly_links_stored gauge")
for i in range(121):
print(f'linkly_links_stored{{job="linkly",instance="api-1"}} {1300 + 2 * i} {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
promtool query range --start=2026-01-01T00:00:00Z --end=2026-01-01T00:20:00Z --step=5m \
http://127.0.0.1:9090 'linkly_links_stored'
echo "--- after the data ends:"
promtool query instant --time=2026-01-01T00:34:00Z http://127.0.0.1:9090 'linkly_links_stored'
promtool query instant --time=2026-01-01T00:36:00Z http://127.0.0.1:9090 'linkly_links_stored'linkly_links_stored{instance="api-1", job="linkly"} =>
1300 @[1767225600]
1340 @[1767225900]
1380 @[1767226200]
1420 @[1767226500]
1460 @[1767226800]
--- after the data ends:
linkly_links_stored{instance="api-1", job="linkly"} => 1540 @[1767227640]
The last two queries show the lookback delta: an instant query returns the most recent sample up to 5 minutes old. The data ends at 00:30:00, so at 00:34 you still get its last value, and at 00:36 nothing. That's also why a target that stops being scraped disappears from graphs after 5 minutes (Prometheus additionally marks its series stale right away when a scrape fails or the target vanishes).
Test yourself
What does http_requests_total{code=~"5.."}[5m] return?
- An instant vector of 5xx counters
- A range vector: every raw sample of each 5xx series from the last 5 minutes
- The 5xx rate
- An error
Does code=~"50" match a series with code="500"?
- Yes, it's a substring
- No, PromQL regexes are fully anchored
- Only with
!~ - Only in Grafana
How do you compare current traffic with the same moment one week ago?
http_requests_total - 1w- Use
offset 1won the second selector @ 1w[1w]