Knowledge

PromQL basics: counters, gauges, rate and sum by, with CPU, RAM and disk examples

Updated 2026-11-21 · Present

  • A counter only goes up (and may drop to 0 on restart), so wrap it in rate(); a gauge moves up and down freely, so use it directly.
  • sum by (...) and avg by (...) merge many time series by the labels you choose.
  • Three node_exporter examples: percentage of CPU in use, RAM still available, free disk space.

Introduction

PromQL is the query language of Prometheus. It is not like SQL: you write an expression, and the result is a set of time series. This article assumes you already have Prometheus pulling measurements from node_exporter as in Installing node_exporter and Prometheus on Ubuntu 24.04 as systemd services. Open http://localhost:9090/graph and try each expression below.

Time series and labels

A time series consists of a metric name and labels, for example node_filesystem_avail_bytes{device="/dev/sda1", mountpoint="/"}. The name and labels identify the series; Prometheus stores its values over time.

Writing a bare metric name, like node_load1, returns an instant vector: each series has exactly one value, the most recent sample at query time (provided that sample falls within the lookback period, 5 minutes by default). Adding a duration in square brackets, as in node_cpu_seconds_total[5m], returns a range vector: for each series, a run of values over the last 5 minutes. A range vector cannot be graphed directly; it exists to be fed into functions such as rate().

Label filters go in curly braces:

Example: node_cpu_seconds_total{mode="idle"}.

Counters and gauges

The two kinds of metric you meet most often:

There are also histograms and summaries, used to record distributions (such as response times); we do not need them in this article.

rate: turning a counter into a speed

rate(v[range]) gives the per-second average rate of increase of a counter over that range, and automatically compensates when the counter resets to 0 after a restart. For example, bytes received per second over the network:

rate(node_network_receive_bytes_total[5m])

A few notes from the official documentation: irate() uses only the last two samples and suits graphing fast-moving series; rate() is smoother and so suits alerting; increase() gives the total increase over the range and is essentially rate() multiplied by the number of seconds in the range. Also, always rate() first, aggregate after: if you sum counters and only then apply rate, Prometheus can no longer detect the counter resets.

The range in rate should be several times the scrape interval; with a 15-second interval, [1m] or [5m] is reasonable.

Aggregating with sum by and avg by

A metric often has many series (each CPU core, each disk, each machine). Aggregation operators collapse them:

Example: total bytes received across all network interfaces of each machine:

sum by (instance) (rate(node_network_receive_bytes_total[5m]))

Example 1: how busy is the CPU, in percent

node_cpu_seconds_total has the labels cpu (each core) and mode (idle, user, system...). The rate of increase for the idle mode is the fraction of time that core was idle (0 to 1). Average across each machine's cores, then take the remainder:

100 * (1 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])))

Read it from the inside out: filter on idle mode, compute the rate per core, average per machine, subtract from 1 to get the busy fraction, multiply by 100 for a percentage.

Example 2: RAM still available

Two gauges: node_memory_MemAvailable_bytes (RAM that can be given to new programs) and node_memory_MemTotal_bytes (total RAM). The fraction of RAM still usable:

100 * node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes

Both sides have the same label set, so the division pairs up series with identical labels. Use MemAvailable rather than "free" because Linux uses spare RAM as cache, so low "free" does not necessarily mean a RAM shortage.

Example 3: free disk space

100 * node_filesystem_avail_bytes{fstype!~"tmpfs|overlay"}
    / node_filesystem_size_bytes{fstype!~"tmpfs|overlay"}

The filter fstype!~"tmpfs|overlay" removes virtual filesystems from the result so only real disks remain. To look at one mount point only, add mountpoint="/".

Verification and common errors

Summary

Four ideas let you read most Prometheus queries: a time series is a name plus labels; counters go with rate(), gauges are used directly; sum by/avg by aggregate by label; rate first, aggregate after. The next step is turning these expressions into alerts, as in the next article on alerting with Alertmanager.

References