Skip to main content
PromQL includes many built-in functions for common tasks like arithmetic and rounding, label transformation, metric manipulation, sorting, and time-based queries. Below are the most commonly used functions organized by purpose, with examples and best practices.
A dark slide titled "Functions" showing the sentence "PromQL has several different function for a variety of use cases including sorting, math, label transformation, metric manipulation and many more," with the keywords highlighted in colored blocks.

Quick reference

Math functions

Basic math helpers operate on instant vectors and transform each sample value.
  • ceil(v instant-vector) — round up to the nearest integer.
  • floor(v instant-vector) — round down to the nearest integer.
  • abs(v instant-vector) — return the absolute value of each sample.
Example (instant vector -> results):

Date and time functions

Prometheus can return components of the current evaluation time as scalar integers. These are useful for calendar-aware rules or time-based label logic. Example results:

Changing types

A dark-themed slide titled "Changing type" showing the text: "The vector function takes a scalar value and converts it into an instant vector." There’s also a small "© Copyright KodeKloud" notice at the bottom.
  • vector(scalar) — converts a scalar to an instant vector with a single sample and no labels. Useful when a vector input is required by a function.
  • scalar(vector) — converts an instant vector to a scalar. Important: the vector must contain exactly one element; otherwise the result is NaN.
scalar(v) will return NaN if v has zero or multiple elements. Use aggregation (e.g., sum(), avg()) or label filtering to guarantee a single-element vector before calling scalar().

Sorting

  • sort(v instant-vector) — sort samples in ascending order by value.
  • sort_desc(v instant-vector) — sort samples in descending order by value.
Example:

Presence and absence functions

Use these functions to detect whether series exist or are missing in a given time window.
  • absent(v instant-vector) / absent_over_time(v range-vector) — indicate absence.
    • absent_over_time(metric[window]) returns a sample with value 1 for the series if there are no samples for the metric in the specified window. If there is at least one sample in the window, it returns an empty vector.
  • present_over_time(v range-vector) — returns 1 for each series that has at least one sample in the specified range.
Examples:
Reference behaviors from the Prometheus docs:
For the full semantics and edge cases, see the Prometheus docs: Querying → Functions.

Other useful functions

Prometheus provides many additional functions. A few highlights and notable behaviors:
  • delta, deriv, changes, exp and others exist for time-series analysis.
  • clamp(v instant-vector, min scalar, max scalar) — clamp samples to the given min and max.
    • Returns an empty vector if min > max.
    • Samples become NaN if min or max is NaN.
    • Histogram samples are ignored.
  • changes(v range-vector) — returns how many times a series’ value changed within the range.
Numeric edge cases for ceil:
  • ceil(+Inf) = +Inf
  • ceil(±0) = ±0
  • ceil(1.49) = 2.0
  • ceil(1.78) = 2.0
Example of delta:

Histogram helpers (common patterns)

  • Compute a quantile from histogram buckets:
  • Per-second request rate from the histogram _count series:
  • Total increase in requests over a window:

Rate and irate for counters

Raw counters typically always increase; to visualize or alert on throughput we convert them to per-second rates with rate() or irate().
A dark presentation slide titled "Rate" explaining that rate() and irate() give the per-second average rate of change. Below the text is a green line chart showing a fluctuating time-series trend.
How rate() works (conceptually)
  • Expression: rate(metric[window])
  • Prometheus collects all samples in the specified window (e.g., 1m). With a 15s scrape interval, a 1m window usually contains ~4 samples.
  • rate() computes an average per-second rate over the window. For counters it accounts for resets and extrapolates at the window edges when necessary. Conceptually it uses the difference between the extrapolated earliest and latest sample values divided by the time span.
Illustration:
  • Samples in a 1m window: 1.2, 2.3, 3.1, 3.3
  • First = 1.2, Last = 3.3 → delta = 2.1
  • Time window = 60s → rate = 2.1 / 60 ≈ 0.035
Query:
How irate() differs
  • Expression: irate(metric[window])
  • irate() uses only the last two samples in the window to compute an instantaneous rate: (last - second_last) / (time difference between those two samples). With a 15s scrape interval, that difference is typically 15s.
Example:
  • Last two samples: 8.9, 10.3 → delta = 1.4
  • Time difference = 15s → irate = 1.4 / 15 ≈ 0.0933
When to use each
  • rate() — use for stable, averaged rates and alerting rules (recommended for robustness).
  • irate() — use for instantaneous or highly volatile rate visualizations.
Best practices:
  • Ensure the window contains enough samples (e.g., with 15s scrapes use at least 1m or larger windows).
  • Apply rate()/irate() before aggregation (e.g., sum() or avg()), so Prometheus can correctly handle counter resets. Example: sum without(code, handler) (rate(http_requests_total[24h]))

Network example

A counter like node_network_transmit_bytes_total is monotonically increasing. To get per-second throughput, use rate(). Raw instant vector over the last 5 minutes:
Per-second rate (1m window, common with 15s scrapes):
This returns bytes/second per interface and is far more useful for throughput monitoring than plotting the raw counter.

Summary and further reading

Prometheus ships many functions for numerical manipulation, time extraction, type conversion, presence detection, histogram analysis, and counter rates. The functions shown here cover most practical scenarios; consult the official Prometheus documentation for the complete list and edge-case behavior:

Watch Video