Skip to main content
PromQL includes many built-in functions for common needs: numeric transformations, time/date extraction, label and metric manipulation, sorting, presence checking, and computing rates for counters. Below we walk through important categories with concise examples and practical tips for queries and alerts.
The image contains a text description of PromQL functions, highlighting features like sorting, math, label transformation, and metric manipulation, with colorful highlights on certain words.
Below we cover math-based functions, date/time helpers, scalar/vector conversions, sorting, presence checks, and counter-rate calculations.

Quick reference: common function categories

Math-based functions

These functions operate on each sample value in an instant vector. Example (instant vector values and results shown inline):

Date and time functions

Prometheus exposes functions to extract parts of the current evaluation time. These are useful for calendar-aware calculations, scheduling logic in queries, or tagging alerts with time components. Key functions:
  • time() — current Unix time in seconds (float).
  • minute(), hour(), day_of_week(), day_of_month(), days_in_month(), month(), year() — return the specified component of the current evaluation timestamp.
Example (if evaluation time is Thursday, September 22, 2022 at 15:07):

Converting between scalars and vectors

  • scalar(v) — Converts an instant vector v containing exactly one sample into a scalar value. If v contains more than one element, the result is NaN.
  • vector(s) — Converts a scalar s into an instant vector containing a single sample (useful for combining scalar thresholds with vector operations).
Use these when you need to mix scalar math or constants with vector expressions.

Sorting

Sort instant vectors by the sample values.
  • sort(v) — ascending order.
  • sort_desc(v) — descending order.
Example:

Presence checks: absent / present (and their _over_time variants)

These functions help detect missing series or whether a series has samples inside a specified range.
  • absent(v) — If v contains any elements, returns an empty vector. If no elements exist, returns a single-element vector with the value 1 and the labels taken from the expression.
  • absent_over_time(v[range]) — Returns 1 for each series that has no samples inside the specified range; returns nothing if at least one sample exists.
  • present_over_time(v[range]) — Returns 1 for each series that has at least one sample in the range; otherwise returns nothing for that series.
Examples:
From the Prometheus docs (behavior illustrations):
Refer to the Prometheus documentation under Querying → Functions for a complete list of functions and their exact semantics.
The image shows a webpage from the Prometheus documentation, specifically detailing the usage of the delta() function in querying. It features a menu on the left, content in the center, and a list of functions on the right.
There are many more functions (delta, deriv, exp, etc.). The names tend to be descriptive; trying examples in the Prometheus console is an effective way to learn them.

Counters and rate calculations

Counters are monotonically increasing metrics (e.g., bytes sent, requests served). Plotting the raw counter typically shows a continuously rising line, which is often not as useful as the rate of change.
The image displays a graph showing a steadily increasing counter metric over time. Accompanying text explains that such plots show expected increases over time.
Prometheus provides two primary functions to convert counters to per-second rates:
  • rate(v[range]) — computes the average per-second rate of increase across the provided time range. It uses the first and last sample of the range (accounting for counter resets).
  • irate(v[range]) — computes an instant rate using only the last two samples in the range (i.e., slope between the most recent samples).
The image contains a line graph illustrating fluctuations over time and text discussing the rate of change of a counter metric using rate() and irate() functions.
How rate() works (conceptual):
  • rate(http_errors[1m]) divides the series into overlapping 1-minute windows for each evaluation.
  • If the scrape interval is 15s, each 1-minute window typically contains 4 samples.
  • For each window, rate() computes (last_sample - first_sample) / window_seconds, yielding a per-second average across the window.
Numeric illustration (one 1-minute window):
How irate() differs:
  • irate(http_errors[1m]) still evaluates over the 1-minute range, but uses the last two samples within that window.
  • Rate is (last - second_last) / time_difference_between_these_two_samples (often equal to the scrape interval, e.g., 15s).
Example usage:
Practical differences and guidance:
The image is a screenshot comparing "rate" and "irate", which explains that "rate" looks at the first and last data points within a range and is effectively an average rate over the range.
Tips when using rate() / irate():
  • Ensure the chosen range contains enough samples. With a 15s scrape interval, 1m yields ~4 samples; more samples improve stability.
  • When aggregating across series, compute the rate first, then aggregate. This preserves correct counter-reset handling per series.
Correct pattern:

Example: network transmit bytes

A raw counter such as node_network_transmit_bytes_total shows cumulative bytes transmitted per interface. The web UI helps explore series and labels.
The image shows the Prometheus web interface with a query being typed in the search bar, displaying autocomplete suggestions for network device statistics.
Viewing the raw counter over time shows a steady increase:
The image shows a Prometheus dashboard displaying a graph of network transmission bytes over time. A hover-over tooltip provides details of the network interface and the specific timestamped data point.
To convert the counter into throughput (bytes per second), compute the per-second rate:
This query returns the average bytes-per-second for each interface over the selected window—more meaningful for bandwidth and throughput alerts.

Summary and references

  • PromQL offers many functions across categories: math, time/date, sorting, presence checks, conversions, and counter-rate computations.
  • Use rate() for stable averages (preferred for alerting), and irate() for instant slopes (preferred for responsive graphs).
  • Always compute rates per series before aggregation to properly handle resets.
  • For a complete list and exact semantics, see the Prometheus docs: https://prometheus.io/docs/prometheus/latest/querying/functions/
Additional resources:

Watch Video