PromQL Basics: Your First Prometheus Queries
Prometheus has been scraping shop-demo every 15 seconds since the last lesson, so it now holds thousands of samples. Then a teammate asks: how many checkouts have failed since the app started? The answer is stored, but the Query page shows only an empty box. This lesson covers PromQL basics: how to pick the series you want, filter them by label, combine them, and read the answer in the Table and Graph tabs.
Instant vectors: a metric name picks series
PromQL, the Prometheus Query Language, is how you ask Prometheus for data. Each query is an expression: you type it on the Query page at http://localhost:9090/query, press Enter, and Prometheus evaluates it at one moment, now by default.
The simplest expression is a metric name. Run http_requests_total and the Table tab lists one row per series, each with its labels and its current value. Here are three of the rows, with illustrative values:
http_requests_total{instance="shop-demo:8000", job="shop-demo", method="GET", route="/products", status="200"} 5214
http_requests_total{instance="shop-demo:8000", job="shop-demo", method="POST", route="/checkout", status="200"} 2106
http_requests_total{instance="shop-demo:8000", job="shop-demo", method="POST", route="/checkout", status="500"} 64
This result is an instant vector: a set of series with one sample each, all at the same timestamp. For each series, Prometheus returns the newest sample at or before the evaluation time, as long as it is less than 5 minutes old, the lookback period. shop-demo writes method, route and status; Prometheus added job and instance, as you saw in What Is Prometheus?
The values are counters: totals that only go up while the app runs and restart from zero when it restarts, as Counters, Gauges and Histograms explained.
Label matchers: slicing the series by label
As Metric Labels and Cardinality explained, each combination of label values is its own series. That makes labels the handles you slice data with. A label matcher, written in curly braces after the name, keeps only the series whose label passes a test. PromQL has four, and the last two test a regex, or regular expression, which is a text pattern:
| Matcher | Keeps series whose label | Example |
|---|---|---|
= | equals the text | {route="/checkout"} |
!= | does not equal it | {route!="/metrics"} |
=~ | matches a regex | {status=~"5.."} |
!~ | does not match a regex | {route!~"/metrics|/health"} |
In a regex, . stands for any character and | means "or". Prometheus regexes are fully anchored, so they must match the whole value. status=~"5" matches only a status of exactly 5, never 500; write "5.." for any 5xx status.
Separate matchers with commas, and a series must pass all of them. http_requests_total{route="/checkout", status="500"} returns only the failed checkouts: your teammate's answer. A matcher also works without a metric name: {route="/checkout"} returns the latency histogram's series too, because they carry the same label. The querying basics page lists every rule.
Aggregation: sum, avg and max with by and without
Often you want fewer, bigger answers: total traffic, not seven counters. An aggregation operator combines many series into fewer. sum adds the values, avg averages them and max keeps the largest. On its own, sum(http_requests_total) returns a single row shown as {}, because no labels are left.
A grouping clause says which labels survive. by (status) keeps only the labels you list, and adds up the series that share them. without (method, route) does the reverse: it drops only the labels you list and keeps the rest, here instance, job and status. Both remove the metric name. After the try-it's test requests for a missing page, sum by (status) (http_requests_total) looks like this, with illustrative values:
{status="200"} 9310
{status="404"} 3
{status="500"} 64
The diagram shows the same collapse with a filter in front. The filter drops the two routes that the lab's own tools call, not shoppers: the Docker health check calls /health, and Prometheus calls /metrics. The sum then adds up whatever is left that shares a status.
Use avg and max where comparing values makes sense, such as scrape_duration_seconds, how long each scrape took. max(scrape_duration_seconds) gives the slowest target's time, and sum(up) counts the targets that are up, because each healthy one contributes 1. The operators reference lists the rest, such as min and count.
Range selectors: instant and range queries
Add a duration in square brackets and the selector changes type. http_requests_total[5m] is a range vector: for each series, every raw sample from the last 5 minutes, about 20 at the lab's 15-second interval. Units include s, m, h and d, and you can combine them, as in 1h30m.
In the Table tab, each row of a range vector lists its samples as a value, @, and a Unix timestamp: seconds since 1 January 1970, UTC. Here is the result of http_requests_total{route="/checkout", status="500"}[1m], laid out as text, with illustrative numbers:
http_requests_total{instance="shop-demo:8000", job="shop-demo", method="POST", route="/checkout", status="500"}
61 @ 1795689915.012
62 @ 1795689930.012
62 @ 1795689945.012
64 @ 1795689960.012
Four samples, 15 seconds apart. The two tabs also run different kinds of query. The Table tab runs an instant query: it evaluates the expression once, at the Evaluation time you pick, now by default. The Graph tab runs a range query: it evaluates the same expression at evenly spaced steps across a time range, the last hour by default, and joins each series' answers into a line.
So a range query is not a range vector. Each step of a graph needs one value per series, so the Graph tab accepts only an instant vector or a scalar, a single plain number. Give it a range vector and an Error executing query box shows:
invalid parameter "query": invalid expression type "range vector" for range query, must be Scalar or instant Vector
A range vector is raw material for functions such as rate(), which turn each window back into one number per series. That is the subject of the next lesson.
Practice queries on shop-demo traffic
Real queries grow in the order of the flowchart: pick a metric, filter it, apply a function, aggregate, then graph. Step 3 needs rate(), so today you skip it and graph the raw counters. They climb steadily; a sudden drop to zero means shop-demo restarted and its totals began again.
Check yourself
http_requests_total{status=~"5"} returns nothing, yet the app has failed checkouts. Why?
Regexes in PromQL are fully anchored, so "5" matches only a status that is exactly 5. Use status=~"5..", which matches any three-character status starting with 5.
You want one line per status but also want to keep job, so you can tell apps apart later. What do you write, and why?
sum by (job, status) (http_requests_total), or sum without (instance, method, route) (http_requests_total). by keeps only the labels it lists, so by (status) alone would drop job.
http_requests_total[5m] works in the Table tab but fails in the Graph tab. Why?
The Graph tab runs a range query, which needs one value per series at each step. A range vector holds many samples per series, so there is nothing single to plot; a function such as rate() turns it into one.