PromQL: Functions

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

PromQL: Basics & Selectors introduces range vectors and instant vectors, and PromQL: Operators & Aggregation covers sum by (...) and friends — both are assumed below. This page works through the function calls that turn those building blocks into the queries actually used in dashboards and alerts: rate(), irate() and increase() for Counters; histogram_quantile() for turning a classic Histogram’s _bucket series into a percentile; predict_linear() for forecasting when a Gauge will cross a threshold; label_replace()/label_join() for reshaping labels with regular expressions; and the time/date functions. The full function reference is Functions, and every example below can be adapted from the worked queries at Query examples.

The worked results on this page come from the orders-api service of this section’s running example (source in Instrumenting Applications), scraped every five seconds by a Prometheus 3.5 server in Docker. Values vary between runs; the arithmetic shown is real.

Counter functions: rate, irate, increase

A Counter only ever goes up (or resets to zero on restart, as covered in Data Model & Metric Types), so its raw value is rarely useful by itself — what dashboards and alerts want is how fast it is increasing. rate(), irate() and increase() all take a range vector, never an instant vector: a single scraped sample has no rate, so each of these needs a [5m]-style range selector to compare samples across a time window.

# Per-second average rate of increase over the last 5 minutes.
rate(http_requests_total{job="api"}[5m])
# https://prometheus.io/docs/prometheus/latest/querying/functions/

rate() looks at the first and last samples in the range, extrapolates slightly to the edges of the window, and divides by the elapsed seconds — the result is smoothed across the whole range, which is what makes it the right choice for graphs and for alerting expressions, where a single noisy scrape should not flap a rule. irate() instead uses only the two most recent samples in the range and ignores the rest, so it reacts instantly to a spike but is far noisier; use it only for interactive, high-resolution graphing of fast-moving counters, never in a recording or alerting rule. increase() is rate() multiplied by the number of seconds in the range — the extrapolated total increase over the window, not a per-second figure:

# Extrapolated request count over the last hour, not a rate.
increase(http_requests_total{job="api"}[1h])

What the three functions compute, with real numbers

Take the failed-request counter of the example service and evaluate all three over a 30-second window. Six samples fell inside the window (five seconds apart); the raw values were:

92, 95, 102, 107, 114, 120      (first sample 26 s before the evaluation time, last one 1 s before it)
flowchart LR A["Range vector [30s]
6 samples, 92 ... 120"] --> B["rate: (120 - 92) over the
observed span, extrapolated
to the window edges, per second"] A --> C["increase: the same result
without dividing by 30"] A --> D["irate: only the last two
samples, (120 - 114) / 5 s"] B --> E["1.12 per second"] C --> F["33.6 requests"] D --> G["1.20 per second"]

increase() returned 33.6, which is the observed increase of 28 stretched by 30 / 25 because the samples cover only 25 of the 30 seconds of the window; that is also why increase() of an integer counter usually returns a fractional number. rate() is that value divided by 30 (1.12). irate() looked at the last two samples only ((120 - 114) / 5, about 1.20), so it moves as soon as one scrape differs. The three numbers are close here because the traffic is steady; with bursty traffic irate() swings widely, which is why it belongs only in interactive graphs.

All three detect a counter reset (the value drops between consecutive samples, typically a process restart) and correct for it automatically by treating the drop as if the counter had continued from where it left off, rather than reporting a spurious negative rate. This correction only works because the input is a genuine Counter; calling rate() on a Gauge produces a meaningless number. During the lab the example service was restarted, so the series dropped from 1065 to 47 between two scrapes (see Data Model & Metric Types for the raw samples); resets(http_requests_total{handler="/orders",status="200"}[10m]) counted the one reset, and rate() over a window containing it stayed positive and plausible. Because rate()/irate()/increase() consume a range vector and return an instant vector, wrap the result in sum by (...) (...) to aggregate across instances — summing first and taking the rate of the sum is a different (and usually wrong) query, since it would apply a single reset-correction across already-summed, cross-instance data:

# Correct: rate per series first, then sum across instances.
sum by (job) (rate(http_requests_total[5m]))

histogram_quantile: latency percentiles from a classic Histogram

A classic Histogram (see Data Model & Metric Types) exposes cumulative _bucket{le="..."} Counter series alongside _sum and _count. histogram_quantile(φ, b) estimates the φ-quantile (0 ≤ φ ≤ 1; 0.99 for p99) by linear interpolation within the bucket where the cumulative count crosses φ of the total, given a vector b whose series carry an le label:

# p99 request latency over the last 5 minutes, aggregated across all instances of a job.
histogram_quantile(0.99,
  sum by (le, job) (rate(http_request_duration_seconds_bucket[5m]))
)
# https://prometheus.io/docs/prometheus/latest/querying/functions/

Read this from the inside out:

  1. rate(..._bucket[5m]) converts each cumulative bucket Counter into a per-second rate, one series per le/instance/pod combination.

  2. sum by (le, job) (...) adds those rates across instances, collapsing everything down to one series per le value per job — le must be kept in the by (...) clause, since histogram_quantile() needs it to reconstruct the bucket boundaries.

  3. histogram_quantile(0.99, ...) walks the resulting le buckets for each job and interpolates the 0.99 quantile within whichever bucket the running count crosses that threshold.

Here is the whole pipeline worked by hand against the example service’s /orders handler, whose simulated latencies are 20, 40, 80, 150, 300 or 600 ms. After sum by (le) (rate(..._bucket[1m])) the per-bucket rates, in requests per second, were:

le Cumulative rate

0.05

3.24

0.1

4.80

0.25

6.45

0.5

8.18

1.0

9.98

2.5

9.98

+Inf

9.98

For phi = 0.99 the target rank is 0.99 * 9.98 = 9.88. That falls in the (0.5, 1.0] bucket, which holds 9.98 - 8.18 = 1.80 requests per second, of which the quantile needs 9.88 - 8.18 = 1.70. Linear interpolation inside the bucket gives 0.5 + 0.5 * 1.70 / 1.80 = 0.972, and the server returned 0.9723. The slowest request the service can produce takes 0.6 s, so the true p99 is about 0.6 s: the estimate is 60% too high because the bucket from 0.5 s to 1 s is so wide. The median came out as 0.0996 and the 0.9 quantile as 0.7227, the latter also pushed up by the same wide bucket. This is the practical meaning of "the quantile is only as accurate as the buckets": put bucket boundaries around the latencies you alert on (for example 0.5, 0.6 and 0.75 if 600 ms is the objective), not at powers of ten.

The mean needs no buckets at all, because every Histogram also has a _sum and a _count:

sum by (handler) (rate(http_request_duration_seconds_sum[1m])) / sum by (handler) (rate(http_request_duration_seconds_count[1m]))

returned 0.216 s for /orders and a few microseconds for /health. Averages hide tail latency, which is why SLO alerts use quantiles or, better, the fraction of requests below a threshold, le="0.5" bucket over le="+Inf" bucket.

The estimate is only as accurate as the bucket boundaries chosen at instrumentation time — a p99 that falls between two widely spaced le values is a rough interpolation, not an exact figure, which is why bucket boundary selection matters and is covered alongside instrumentation in Instrumenting Applications. Native histograms let histogram_quantile() operate on a single series instead of a _bucket family and reduce this interpolation error; see Data Model & Metric Types for the trade-off. This is the query pattern Recording & Alerting Rules precomputes into a recording rule, since a histogram_quantile(rate(...)) chain evaluated on every dashboard refresh (or every alert evaluation) is one of the more expensive query shapes to run live.

predict_linear: capacity forecasting

predict_linear(v range-vector, t scalar) fits a simple linear regression to the samples in the range vector and extrapolates t seconds into the future, returning the predicted value. It is built for slow, roughly linear Gauges — disk usage, queue backlog — where "will this cross a threshold soon" matters more than the exact current value:

# Will free disk space go negative within the next 4 hours, based on the last 6h trend?
predict_linear(node_filesystem_avail_bytes{mountpoint="/"}[6h], 4 * 3600) < 0
# https://prometheus.io/docs/prometheus/latest/querying/functions/

The example service has no growing resource, so the same query shape can be tried on the memory of the Prometheus server itself, which was warming up during the lab run:

process_resident_memory_bytes{job="prometheus"}                       # 105,123,840  (about 100 MiB)
deriv(process_resident_memory_bytes{job="prometheus"}[5m])            # 21,154 bytes per second
predict_linear(process_resident_memory_bytes{job="prometheus"}[5m], 3600)   # 181,718,107

deriv() is the fitted slope in units per second, delta() the plain difference between the first and last sample (6.6 MB over five minutes), and predict_linear() the fitted line extended one hour ahead (about 173 MiB). Both deriv() and predict_linear() are for Gauges; applying them to a Counter would fit a line through a monotonic series and report a growth that rate() already describes better.

The range ([6h] above) controls how much history the regression is fit against — long enough to smooth out noise, short enough to still reflect the current trend rather than a stale one from days ago. predict_linear() assumes a straight-line trend; a workload with strong daily seasonality (a disk that fills on business days and drains overnight) is a poor fit and will over- or under-predict depending on where in the cycle the range window lands. This expression is a common alerting-rule body in Recording & Alerting Rules — "page before it actually runs out" rather than "page once it already has."

label_replace and label_join: reshaping labels with regexes

Both functions return the input vector with one label added or overwritten, without touching sample values — useful for normalizing labels that differ across jobs before an aggregation or a join-style vector match (see PromQL: Operators & Aggregation).

label_replace(v, dst, replacement, src, regex) matches regex (a fully anchored RE2 pattern) against the src label’s value and writes the result of substituting capture groups ($1, $2, …​) into replacement, storing it in dst:

# Pull the numeric instance id out of a pod name like "api-7" into its own label.
label_replace(
  up{job="api"},
  "instance_id", "$1",
  "pod", "api-(.*)"
)
# https://prometheus.io/docs/prometheus/latest/querying/functions/

On the lab server label_replace(up, "host", "$1", "instance", "(.*):.*") split the instance label of each target and returned, among others, {host="host.docker.internal", instance="host.docker.internal:28101", job="orders-api"} and {host="localhost", instance="localhost:9090", job="prometheus"}; and label_join(up, "target", "/", "job", "env") produced target="orders-api/lab" for the example service and target="prometheus/" for the server (the missing env label joined as an empty string).

Every series where pod matches api-(.*) gains an instance_id label holding whatever the capture group matched ("7" for pod="api-7"); a series that does not match regex at all is passed through with dst left unset. This is the standard way to align label names that differ only in spelling between two metrics before matching them together with on(...)/ignoring(...), and it is exactly what Service Discovery & Relabeling does at scrape time with relabel_configs — label_replace() is the PromQL-query-time equivalent of that same regex-capture-and-rewrite idiom, applied per query instead of once at scrape.

label_join(v, dst, separator, src_label_1, src_label_2, ...) concatenates one or more existing labels into dst with separator between them — no regex, just a join:

# Build a single "namespace/pod" identity label out of two existing labels.
label_join(kube_pod_info, "pod_identity", "/", "namespace", "pod")

Both functions are pure-vector transforms with no aggregation, so they compose directly with sum by (...), on(...) joins, or another label_replace() call in the same expression.

Other functions you will use constantly

The functions above are the core, but a handful of others appear in almost every real dashboard or alert. All results are from the example service.

Function Use Result in the lab

avg_over_time, max_over_time, min_over_time, quantile_over_time

Aggregate a Gauge’s samples over a window; the argument is a range vector.

max_over_time(orders_in_progress[1m]) 3, min_over_time(...) 0, avg_over_time(...) 2.33

last_over_time

The latest sample in the range, even if the series is stale now.

1 after the service stopped

absent, absent_over_time

Return 1 (with the selector’s equality labels) when nothing matches; the standard "metric disappeared" alert.

absent(http_requests_total{job="nope"}) returned {job="nope"} 1

changes, resets

Number of value changes, or counter resets, in the range.

changes(orders_in_progress[1m]) 7; resets(...) 1

clamp_min, clamp_max, clamp

Bound each value into a range, for example to keep a ratio panel readable.

clamp_max(sum by (handler) (rate(http_requests_total[1m])), 5) capped /orders at 5

round

Round to the nearest multiple.

round(..., 0.5) gave 9.5 for /orders and 3.5 for /health

sort, sort_desc

Order an instant vector; only the final result of an instant query keeps the order.

sort_desc(sum by (handler, status) (rate(http_requests_total[1m]))) listed /orders 200 first

vector, scalar

Convert between a scalar and a one-element vector.

vector(1) returns {} 1

Two rules of thumb tie these together. First, a function that takes a range vector (rate, increase, deriv, *_over_time, resets, changes) needs a [range], and its result is an instant vector. Second, functions never aggregate across series unless their name says so (sum, avg, topk); rate() returns one value per input series, so wrap it in sum by (...) to get a fleet total, as in sum(increase(http_requests_total[5m])), which returned 3780 requests for the five-minute window.

Time and date functions

A small family of functions ignores its (optional) vector argument entirely and instead returns the evaluation timestamp itself, broken down however is useful — most often to gate a rule to a particular window or day:

time()                              # evaluation time as a Unix timestamp, seconds
day_of_week(vector(time()))         # 0 (Sunday) .. 6 (Saturday), in UTC
day_of_month(vector(time()))        # 1..31
hour(vector(time()))                # 0..23, UTC
minute(vector(time()))              # 0..59
month(vector(time()))               # 1..12
year(vector(time()))                # e.g. 2026
# https://prometheus.io/docs/prometheus/latest/querying/functions/

The lab server evaluated hour() as 13 and day_of_week() as 2 (Tuesday) at the moment of the query, and time() returned a scalar — note the response type, "resultType":"scalar" with a timestamp pair rather than a vector:

{"status":"success","data":{"resultType":"scalar","result":[1794317858.953,"1794317858.953"]}}

vector(time()) turns the scalar time() into a one-sample instant vector so the other functions (which expect a vector argument, defaulting to the current time when omitted) have something to operate on; calling hour() with no argument is equivalent and more common in practice. A frequent use is silencing a noisy rule outside business hours directly in the expression:

# Only fire during weekday business hours (UTC): Mon-Fri, 08:00-18:00.
(
  rate(http_requests_total{status=~"5.."}[5m]) > 0.05
)
and on() (day_of_week() >= 1 and day_of_week() <= 5)
and on() (hour() >= 8 and hour() < 18)

These functions are all evaluated in UTC; converting to a local timezone for a display label (rather than a threshold) is normally left to Grafana, covered in Grafana: Dashboards & Visualization, rather than done inside PromQL.

Where these land in real rules

Every function above shows up, more or less verbatim, once queries stop being typed ad hoc into the Prometheus expression browser and get promoted to recording rules (precomputing an expensive histogram_quantile(sum(rate(...)))) or alerting rules (a predict_linear() or counter-reset-aware rate() threshold). See Recording & Alerting Rules for that promotion, and Prometheus & Monitoring for the rest of this section.