Sub topic 9
Selecting Series with PromQL
Assume you can read YAML, understand labels such as job and instance, and have access to a Prometheus server containing metrics from node_exporter. PromQL is Prometheus’s query language. It selects time series by metric name and labels, then passes the selected data to functions or aggregations.
Metric selectors
A metric name selects every time series with that name:
upThe up metric has one series for each scrape target. Its value is 1 when the last scrape succeeded and 0 when it failed.
A label matcher narrows the selection:
up{job="node"}This selects only targets whose job label is exactly node. Add more matchers to require all conditions:
node_cpu_seconds_total{job="node", instance="server1.example.com:9100", mode="idle"}Each unique combination of metric name and label values is a separate time series.
Label matcher operators
PromQL provides four common matcher operators:
| Operator | Meaning | Example | Column |
|---|---|---|---|
| `=` | Label equals a value | `job="node"` | |
| `!=` | Label does not equal a value | `mode!="idle"` | |
| `=~` | Label matches a regular expression | `instance=~"server[12].*"` | |
| `!~` | Label does not match a regular expression | `fstype!~"tmpfs | overlay"` |
Regular expressions match the complete label value. For example, instance=~"server[12].*" matches values beginning with server1 or server2.
A selector can use a regular expression to choose several servers:
up{job="node", instance=~"server[12].example.com:9100"}Use != and !~ carefully. A series without the named label can also satisfy a negative matcher. Check the target labels in Prometheus when a query returns unexpected results.
Instant and range selectors
A normal selector returns the latest sample for each matching series. This is called an instant vector:
node_memory_MemAvailable_bytes{instance="server1.example.com:9100"}A range selector adds a time window and returns recent samples:
node_cpu_seconds_total{mode="idle"}[5m]Range vectors are used by functions such as rate, which calculates the average per-second increase of a counter over the selected window:
rate(node_cpu_seconds_total{mode="idle"}[5m])Exercise
Write a selector that returns the up series for targets with job="node" except the target at server2.example.com:9100.
Show answer
up{job="node", instance!="server2.example.com:9100"}