Course contents · Lesson 5 of 21

PromQL Basics: Your First Prometheus Queries

Prometheus and Alertmanager · Lesson 5 of 21 · 11 min Hands-on

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:

MatcherKeeps series whose labelExample
=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.

Diagram. Top: eight series of http_requests_total by route and status: / 200, /products 200, /report 200, /checkout 200, /checkout 500, other 404 from your test curl, and /health 200 and /metrics 200, which are struck through. Step 1, the filter route!~"/metrics|/health", drops the two internal routes. Step 2, sum by (status), adds series that share a status. Result: three lines, status 200 from four series rising fast, status 500 rising slowly, and status 404 flat after the curl. Lines are illustrative.
Aggregation adds series together: four series with status 200 become one line.

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.

The query sum by (status) (http_requests_total{route="/checkout"}) split into labelled parts: sum is the aggregation operator that adds up the values, by (status) is the grouping clause naming the labels to keep, and inside the parentheses the inner expression selects and filters series: http_requests_total is the metric name and {route="/checkout"} is the label matcher. The result is one series per status with no metric name, such as {status="200"} and {status="500"}.
Read a query from the inside out: the selector picks and filters, then sum by (status) combines.

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.

Flowchart in five steps, each with the query so far. 1, pick the metric: http_requests_total. 2, filter by label: http_requests_total{route="/checkout"}, two series for status 200 and 500. 3, apply a function, dashed because it is the next lesson: rate(http_requests_total{route="/checkout"}[5m]). 4, aggregate: sum by (status) wrapped around step 3, dropping every label except status. 5, graph it: run step 4 in the Graph tab for one line per status.
Each step wraps or extends the one before, so even a long query is a short one grown outwards.

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.