PromQL: Basics & selectors

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Every PromQL expression evaluates to one of a small set of value types, and almost every query in this section’s remaining pages starts from the same two: an instant vector and a range vector. This page covers that distinction, the label matchers used to select which series a query touches, the offset and @ modifiers that shift a query in time, subqueries for computing a range-vector function over another range, and the HTTP query API that the expression browser — and every external client — issues these queries against. See Querying basics for the full language reference this page draws from.

The queries on this page run against the orders-api service of this section’s running example (see Instrumenting Applications for its source and Getting Started for the Docker setup), scraped every five seconds. Results were captured from that live server, so the numbers differ from run to run but the shapes and behaviors are exactly what you will see.

The four PromQL value types

Every expression has a type, and most PromQL errors are type errors in disguise:

Type What it is

Instant vector

A set of series, each with one sample at the evaluation timestamp. Most expressions return this.

Range vector

A set of series, each with a list of samples over a time window. Only functions and subqueries consume it.

Scalar

A single floating-point number, such as time() or a literal 4.

String

A literal string; currently used only as a function argument.

flowchart LR IV["Instant vector
http_requests_total"] -- "range selector [5m]" --> RV["Range vector
http_requests_total[5m]"] RV -- "rate, increase, avg_over_time ..." --> IV2["Instant vector
one value per series"] IV2 -- "sum, avg, topk" --> IV3["Fewer series"] IV2 -- "subquery [30m:1m]" --> RV SC["Scalar
time()"] -- "vector()" --> IV2 IV3 -- "scalar()" --> SC

The loop between instant and range vector is the heart of the language: a range selector or subquery produces a range vector, and a function such as rate() turns it back into an instant vector that can be aggregated, compared and graphed.

Instant vectors vs. range vectors

A bare metric name, optionally narrowed with label matchers, is an instant vector: one sample per matching series, all as of the same evaluation timestamp.

http_requests_total{job="orders-api", status="500"}

Appending a range selector — a duration in square brackets — turns the same expression into a range vector: not one sample per series but every sample in that trailing time window, still one list per series.

http_requests_total{job="orders-api", status="500"}[5m]

A range vector cannot be graphed or returned as-is by an instant query; it exists to be fed into a function that reduces it back to an instant vector, most commonly rate() or increase() over a Counter:

rate(http_requests_total{job="orders-api", status="500"}[5m])

[5m] here means "the last 5 minutes of samples up to the query’s evaluation time," not a fixed 5-minute calendar bucket — a query re-run a second later slides the whole window forward by a second. As a rule of thumb, pick a range at least four times the scrape interval so rate() always has at least two samples to work with; see PromQL: Functions for rate(), increase(), and the rest of the function library that range vectors feed into.

The four-times rule is easy to confirm. With the five-second scrape interval used here, a five-second range holds at most one or two samples, and rate() needs two, so the same query returns data with [1m] and nothing with [5s]:

rate(http_requests_total[5s])
{"status":"success","data":{"resultType":"vector","result":[]}}

Staleness and the lookback window

An instant vector selector returns, for each series, the most recent sample at or before the evaluation time — but only if it is recent enough. Two rules decide this:

  • A series whose last sample is more than five minutes old (the default lookback delta) is not returned.

  • When a scrape fails or a target disappears, Prometheus writes an explicit staleness marker for every series the target exposed, and a selector stops returning them immediately instead of waiting five minutes.

The example service was stopped for about fourteen seconds to observe both. The synthetic up series is written even for a failed scrape, while the application’s own series become stale at once:

up{job="orders-api"}                       # 0   (still present, now with value 0)
orders_in_progress                         # empty: the series is stale
last_over_time(orders_in_progress[5m])     # 1   (the last sample is still stored)
absent(orders_in_progress)                 # 1   (the series is missing)

The stored samples are not deleted; last_over_time() over a range still reads them. Only the instant selector hides them. This is also why dashboards show gaps instead of a flat line when a target dies.

Misusing functions on the wrong type

PromQL does not know a metric’s type, only its values, so a type mistake usually evaluates without error and returns a meaningless number. orders_in_progress is a Gauge, yet:

rate(orders_in_progress[1m])

returned 0.145 — the per-second slope of a number that goes up and down, which tells nothing about anything. Apply rate()/increase() only to Counters (and to the _sum, _count and _bucket series of Histograms and Summaries); see PromQL: Functions.

Label matchers

Label matchers narrow an instant or range vector to the series whose label values satisfy every matcher, AND-ed together. Four operators are available:

Operator Meaning

=

Equals the given string, exactly.

!=

Does not equal the given string.

=~

Matches the given RE2 regular expression.

!~

Does not match the given RE2 regular expression.

http_requests_total{job="orders-api", status="500"}
http_requests_total{job!="orders-api"}
http_requests_total{status=~"4..|5.."}
http_requests_total{handler!~"/health|/metrics"}

Two behaviors surprise newcomers, and both can be checked against the example service. A matcher on a label that the series does not have behaves as if the label were present with an empty value, so http_requests_total{nolabel=""} selects all three series, and http_requests_total{nolabel!=""} selects none. That is how {env=""} selects "series without an env label". And a regex that looks like a prefix selects nothing, because of the anchoring described next: http_requests_total{status=~"5"} returned an empty vector, since no status is exactly 5; the intended query is status=~"5..".

A regex match is always implicitly anchored (^(?:...)$), so status=~"5.." matches only three-character values starting with 5 — not any value merely containing that substring. A bare metric name such as http_requests_total is itself sugar for a __name__="http_requests_total" matcher, which is why {__name__=~"http_requests_.*"} works as a matcher on the metric name itself when no literal name is written before the braces. At least one matcher in a selector must not match the empty string ({handler=~".*"} alone is rejected) so a query can never silently select every series in the TSDB. The server answers vector selector must contain at least one non-empty matcher with HTTP status 400. See Time series selectors for the complete matcher semantics, and PromQL: Operators & Aggregation for combining several selectors together with arithmetic, comparison, and set operators.

The offset modifier

offset shifts a selector’s evaluation window into the past by a fixed duration, without changing the query’s own evaluation timestamp. It is the straightforward way to compare "now" against "same time last week":

rate(http_requests_total[5m]) / rate(http_requests_total[5m] offset 1w)

offset applies to the selector it is written on, so in a compound expression each range or instant vector can carry its own, independent offset. It composes with the @ modifier below, and it can also follow a subquery or a parenthesized expression ((expr)[30s:] offset 1m). A comparison with an earlier period on the example service is just sum(rate(http_requests_total[1m])) / sum(rate(http_requests_total[1m] offset 5m)), which returned 0.96 in the lab: the request rate was about 4% lower than five minutes earlier.

The @ modifier

@ pins a selector’s evaluation to a fixed Unix timestamp instead of the query’s own evaluation time, which offset (a relative shift) cannot do on its own:

http_requests_total @ 1609746000
rate(http_requests_total[5m] @ 1609746000)

Against the example service, http_requests_total{handler="/orders",status="500"} @ 1794317702 evaluated two minutes later returned the value that was current at that timestamp (153), the same as http_requests_total{handler="/orders",status="500"} offset 2m, while the unmodified selector had moved on to 269. Pinning with @ is what makes a dashboard panel such as "increase since the start of the selected time range" stable while the user pans it.

@ also accepts start() and end() inside a range-vector function evaluated by /api/v1/query_range (see below), pinning every step of the range to the query’s own start/end parameter rather than to that step’s own evaluation time — useful for a query that should compare every point in a range against one fixed baseline. @, offset, and a range selector can all appear on the same vector selector, in any order, and combine rather than override each other. See the @ modifier for the full grammar, including the start()/end() forms.

Subqueries

A subquery evaluates a range-vector function repeatedly over an inner range, at a given step, producing a range vector out of what would otherwise be an instant-vector expression. The syntax adds a second, inner resolution after the outer range: [<range>:<resolution>].

max_over_time(rate(http_requests_total[5m])[30m:1m])

Read from the inside out: rate(http_requests_total[5m]) is evaluated as if standalone, then re-evaluated every 1m across the trailing 30m window, and max_over_time() reduces that series of per-minute rate values down to one number — the highest 5-minute request rate seen in the last half hour. On the example service max_over_time(sum(rate(http_requests_total[30s]))[5m:30s]) returned 15.08 — the busiest 30-second window in the last five minutes, across all handlers. Omitting the resolution ([30m:]) defaults it to the server’s global evaluation interval. Reach for a subquery specifically when you need a range-vector function (max_over_time(), increase(), deriv(), and similar) applied over another function’s output rather than over raw samples — rate() itself never needs one, since it already accepts a plain range selector directly. Subqueries are comparatively expensive: each inner step re-runs the whole inner expression, so prefer a recording rule that pre-computes the inner expression on a schedule when the same subquery is evaluated repeatedly (for example, from a dashboard). See Subqueries for the full syntax and defaulting rules.

The HTTP query API

The expression browser, Grafana, and every other PromQL client all go through the same JSON HTTP API rather than a special protocol. Two endpoints matter for reading data: /api/v1/query for a single evaluation at one instant, and /api/v1/query_range for a series of evaluations across a time window.

curl "http://localhost:9090/api/v1/query" \
  --data-urlencode 'query=rate(http_requests_total{job="orders-api"}[5m])' \
  --data-urlencode 'time=2025-09-06T10:00:00Z'
# https://prometheus.io/docs/prometheus/latest/querying/api/#instant-queries

query is the only required parameter; time (RFC 3339 or a Unix timestamp) defaults to the server’s own current time when omitted. /api/v1/query_range instead requires start, end, and step, and re-runs the query once per step across that window:

curl "http://localhost:9090/api/v1/query_range" \
  --data-urlencode 'query=rate(http_requests_total{job="orders-api"}[5m])' \
  --data-urlencode 'start=2025-09-06T09:00:00Z' \
  --data-urlencode 'end=2025-09-06T10:00:00Z' \
  --data-urlencode 'step=30s'
# https://prometheus.io/docs/prometheus/latest/querying/api/#range-queries

A real instant-query response from the example service (query=up{job="orders-api"}) looks like this:

{"status":"success","data":{"resultType":"vector","result":[
  {"metric":{"__name__":"up","env":"lab","instance":"host.docker.internal:28101","job":"orders-api"},
   "value":[1794317661.162,"1"]}]}}

and a range query for sum(rate(http_requests_total[1m])) with step=20s returns one series with one [timestamp, value] pair per step:

{"status":"success","data":{"resultType":"matrix","result":[
  {"metric":{},"values":[[1794317601,"12.298565000000002"],[1794317621,"13.108375906768723"],
                         [1794317641,"12.962929294765742"],[1794317661,"13.308848930019455"]]}]}}

An empty metric object is what aggregation leaves behind when no by clause names a label to keep. Syntax and type errors come back with HTTP status 400 and a JSON body, for example for query=foo(:

{"status":"error","errorType":"bad_data","error":"invalid parameter \"query\": 1:5: parse error: unclosed left parenthesis"}

Both return {"status":"success","data":{"resultType": "...", "result": [...]}}; resultType is vector for an instant query’s result and matrix for a range query’s, each sample serialized as [<unix_timestamp>, "<value>"] (the value is a string so large or special floats such as NaN round-trip exactly). step on /api/v1/query_range is independent of any range selector written inside query itself — it only controls how often the whole expression is re-evaluated between start and end, so a step coarser than an inner [5m] skips evaluation points rather than widening the range. The full parameter list — including timeout, response codes for partial results, and the metadata endpoints (/api/v1/series, /api/v1/labels, /api/v1/label/<name>/values) — is at HTTP API.

Exploring what exists: the metadata endpoints

Before writing a query it is often necessary to find out which series and labels exist. Three endpoints answer that without evaluating PromQL:

curl -s localhost:9090/api/v1/series --data-urlencode 'match[]=http_requests_total'
curl -s localhost:9090/api/v1/labels
curl -s localhost:9090/api/v1/label/handler/values
curl -s localhost:9090/api/v1/targets

/api/v1/series returns the label sets of every series matching a selector (shown in Data Model & Metric Types), /api/v1/labels the names of all labels, and /api/v1/label/<name>/values the values of one label. On the lab server the handler label returned the example service’s /orders and /health together with the server’s own handler names, because handler is also a label of Prometheus’s prometheus_http_requests_total. /api/v1/targets lists every scrape target with its health and last error — during the stop-the-service experiment above it reported the orders-api target as down with a dial tcp connection error, the same information as Status > Targets in the web UI.

Continue with PromQL: Operators & Aggregation for combining vectors with arithmetic, comparison, and by/without aggregation, and PromQL: Functions for the function library — rate(), histogram_quantile(), and the rest — that range vectors and subqueries feed into.