Sub topic 9
Selecting series with PromQL
This chapter explains how a PromQL selector chooses time series from Prometheus. It assumes you have a running Prometheus server that scrapes at least one node_exporter target. It also assumes you know that a series is a metric name plus a set of labels, as covered in the data model chapter. You can run every query below in the expression browser at http://localhost:9090/graph.
What a selector does
PromQL is the query language of Prometheus. Every query starts with a selector, which picks series from storage. Functions such as rate() and aggregations such as avg by work on whatever the selector returns. If the selector picks the wrong series, everything built on it is wrong too.
The simplest selector is a bare metric name:
node_load1This returns one sample for every series named node_load1. With two servers you get two rows, each with its own instance and job labels. The result type is an instant vector: a set of series, each with a single value at one point in time.
The diagram shows how a selector becomes either kind of vector.
Label matchers
Curly braces after the name hold label matchers. A series is returned only if it satisfies all of them.
| Matcher | Meaning | Example | Column |
|---|---|---|---|
| `=` | Label equals the string | `job="node"` | |
| `!=` | Label does not equal the string | `mode!="idle"` | |
| `=~` | Label matches the regular expression | `instance=~"server[12].*"` | |
| `!~` | Label does not match the regular expression | `fstype!~"tmpfs\ | overlay"` |
Some examples against node_exporter data:
# Only the idle CPU time series from the node job
node_cpu_seconds_total{job="node", mode="idle"}
# Every mode except idle
node_cpu_seconds_total{mode!="idle"}
# Only the busy-related modes
node_cpu_seconds_total{mode=~"user|system|iowait"}
# Real filesystems only
node_filesystem_avail_bytes{fstype!~"tmpfs|overlay"}Regular expressions are anchored
Prometheus uses RE2 syntax and anchors every regex at both ends. The pattern server1 therefore matches only the value server1, not server1.example.com. To match a prefix, write server1.*.
node_load1{instance=~"server1.*"}A common mistake is instance=~"server1" when the real label is server1.example.com:9100. The query then returns nothing and gives no error.
Missing labels equal the empty string
Prometheus treats a missing label as a label with an empty value. So {foo=""} matches series that have no foo label. Likewise {foo!=""} matches only series that do have it. You can use this to filter on whether a label exists.
A selector must match something non-empty
Prometheus rejects a selector whose matchers could all match the empty string. This query is invalid:
{job=~".*"}The error says the vector selector must contain at least one non-empty matcher. Adding a metric name or a matcher such as job="node" fixes it.
The metric name is a label
Internally the metric name is stored in the label __name__. node_load1 is shorthand for {__name__="node_load1"}. This lets you match names with a regex:
{__name__=~"node_load(1|5|15)"}Use this sparingly. A selector that does not fix the name must scan many more series. It is useful for exploration, for example to find all series from one job:
{job="node", __name__=~"node_memory_Mem.*"}Range vectors
Appending a duration in square brackets turns the selector into a range vector. It returns, for each series, all samples in the window ending at the evaluation time.
node_cpu_seconds_total{mode="idle"}[5m]With a 15-second scrape interval, each series returns about 20 samples. Range vectors cannot be graphed directly. They exist to feed functions such as rate(), which the next chapter covers. If you type one into the Graph tab, Prometheus says it cannot plot a range vector. Use the Table tab to inspect it.
Common duration units are s, m, h, d, w, and y. Units can be combined, as in 1h30m.
Offset and the @ modifier
An offset modifier shifts the evaluation time back. It goes right after the selector, before any function wraps it:
# Load average one hour ago
node_load1 offset 1h
# Compare current load with the same time yesterday
node_load1 - node_load1 offset 1dThe @ modifier pins evaluation to a Unix timestamp:
node_load1 @ 1700000000Both are useful when comparing periods or investigating an incident at a known time.
Staleness and the lookback window
An instant vector does not require a sample at exactly the evaluation time. Prometheus looks back up to 5 minutes, the default lookback delta, and uses the most recent sample it finds. If a target disappears, Prometheus writes a special stale marker. The series then drops out of results at once instead of lingering for 5 minutes.
Two practical consequences follow:
- A series from a target that was killed abruptly, without a clean scrape failure, can still appear for up to 5 minutes.
- If the scrape interval is longer than the lookback, series flicker in and out of results. Keep scrape intervals well under 5 minutes.
Worked example: finding the right series
Suppose a panel for disk usage on server1 is empty. Work from broad to narrow:
- Check the target is alive with
up{job="node", instance=~"server1.*"}. - Check the metric exists with
node_filesystem_avail_bytes{instance=~"server1.*"}. - Look at the label values on the rows that come back, especially
mountpointandfstype. - Add matchers one at a time, such as
fstype!~"tmpfs|overlay", and watch which series disappear.
Adding matchers one at a time shows which one removes the series you expected to keep. Most empty panels come from one of three causes: a regex that does not account for anchoring, a label value with a different spelling than assumed, or a matcher on a label that the series does not carry.
Quick reference of common errors
| Symptom | Likely cause |
|---|---|
| Query returns nothing, no error | Regex anchored, label value misspelled, or the label does not exist on that metric |
| "vector selector must contain at least one non-empty matcher" | Every matcher can match the empty string |
| "can't graph range vector" | The selector has a `[5m]` suffix and no function around it |
| Old series still shown after a target died | Lookback window of 5 minutes |
| Parse error near `{` | Label values must use double quotes, and the matcher list must be inside one pair of braces |
Exercise
A server exposes node_filesystem_avail_bytes with labels instance, mountpoint, and fstype. Write a selector that returns only series for instances starting with web, excluding the filesystem types tmpfs and overlay. Then explain why instance=~"web" would not work.
Show answer
Use node_filesystem_avail_bytes{instance=~"web.*", fstype!~"tmpfs|overlay"}. Regex matchers are fully anchored, so instance=~"web" matches only the exact value web. Real instance values such as web1.example.com:9100 are longer, so nothing matches. The trailing .* makes the pattern match any value that starts with web.