Functions¶
Function
¶
Function(name: str, args: list[OPERAND_TYPE])
Bases: Query
A PromQL function call, serialised as name(arg1, arg2, ...).
Every function helper (rate, abs_, ...)
returns one. Construct it directly only for a function that has no helper;
raw numbers in args are wrapped in Float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
PromQL function name, e.g. |
required |
args
|
list[OPERAND_TYPE]
|
Arguments in order; raw numbers are wrapped in |
required |
subquery
¶
subquery(range: SCALAR_TYPE, resolution: SCALAR_TYPE | None = None, *, offset: SCALAR_TYPE | None = None, at: AT_TYPE | None = None) -> Subquery
Evaluate this expression as a subquery: <expr>[range:resolution].
See Subquery; slice syntax expr[range:resolution]
is a shorthand for this method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
range
|
SCALAR_TYPE
|
Window length; raw numbers are seconds ( |
required |
resolution
|
SCALAR_TYPE | None
|
Step between evaluations; |
None
|
offset
|
SCALAR_TYPE | None
|
Time shift; raw numbers are seconds. |
None
|
at
|
AT_TYPE | None
|
Evaluation time: a Unix timestamp, |
None
|
to_string
¶
to_string(indent: str | int | None = None, _indent_level: int = 0, *, style: Style = DEFAULT_STYLE) -> str
Render this expression as a PromQL string.
abs_
¶
Return abs(v): the absolute value of each sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
ceil
¶
Return ceil(v): each sample rounded up to the nearest integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
floor
¶
Return floor(v): each sample rounded down to the nearest integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
sqrt
¶
Return sqrt(v): the square root of each sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
sgn
¶
Return sgn(v): the sign of each sample.
Returns 1.0 for positive values, -1.0 for negative, and 0.0 for zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
exp
¶
Return exp(v): e raised to the power of each sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
ln
¶
Return ln(v): the natural logarithm of each sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
log2
¶
Return log2(v): the base-2 logarithm of each sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
log10
¶
Return log10(v): the base-10 logarithm of each sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
acos
¶
Return acos(v): the arccosine of each sample, in radians.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
acosh
¶
Return acosh(v): the inverse hyperbolic cosine of each sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
asin
¶
Return asin(v): the arcsine of each sample, in radians.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
asinh
¶
Return asinh(v): the inverse hyperbolic sine of each sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
atan
¶
Return atan(v): the arctangent of each sample, in radians.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
atanh
¶
Return atanh(v): the inverse hyperbolic tangent of each sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
cos
¶
Return cos(v): the cosine of each sample, taken as radians.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
cosh
¶
Return cosh(v): the hyperbolic cosine of each sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
sin
¶
Return sin(v): the sine of each sample, taken as radians.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
sinh
¶
Return sinh(v): the hyperbolic sine of each sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
tan
¶
Return tan(v): the tangent of each sample, taken as radians.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
tanh
¶
Return tanh(v): the hyperbolic tangent of each sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
deg
¶
Return deg(v): each sample converted from radians to degrees.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
rad
¶
Return rad(v): each sample converted from degrees to radians.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to transform. |
required |
timestamp
¶
Return timestamp(v): the Unix timestamp of each sample, in seconds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector whose sample timestamps are returned. |
required |
sort
¶
Return sort(v): the elements sorted by sample value, ascending.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to sort. |
required |
sort_desc
¶
Return sort_desc(v): the elements sorted by sample value, descending.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to sort. |
required |
absent
¶
Return absent(v): a single element with value 1 if v is empty.
Returns an empty vector when v is non-empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to check, usually a selector. |
required |
scalar
¶
Return scalar(v): the value of a single-element v as a scalar.
Returns NaN if v does not contain exactly one element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector with a single element. |
required |
histogram_count
¶
Return histogram_count(v): the observation count of each native histogram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector of native histograms. |
required |
histogram_sum
¶
Return histogram_sum(v): the sum of observations of each native histogram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector of native histograms. |
required |
histogram_avg
¶
Return histogram_avg(v): the average observation of each native histogram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector of native histograms. |
required |
histogram_stddev
¶
Return histogram_stddev(v): the estimated standard deviation of each native histogram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector of native histograms. |
required |
histogram_stdvar
¶
Return histogram_stdvar(v): the estimated variance of each native histogram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector of native histograms. |
required |
avg_over_time
¶
Return avg_over_time(v): the average value of each series over the range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector: a |
required |
min_over_time
¶
Return min_over_time(v): the smallest value of each series in the range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector: a |
required |
max_over_time
¶
Return max_over_time(v): the largest value of each series in the range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector: a |
required |
sum_over_time
¶
Return sum_over_time(v): the sum of each series' values in the range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector: a |
required |
count_over_time
¶
Return count_over_time(v): the number of samples of each series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector: a |
required |
stddev_over_time
¶
Return stddev_over_time(v): the population standard deviation of each series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector: a |
required |
stdvar_over_time
¶
Return stdvar_over_time(v): the population variance of each series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector: a |
required |
last_over_time
¶
Return last_over_time(v): the most recent value of each series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector: a |
required |
present_over_time
¶
Return present_over_time(v): 1 for each series with any sample in the range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector: a |
required |
absent_over_time
¶
Return absent_over_time(v): a single element with value 1 if v has no samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector: a |
required |
first_over_time
¶
Return first_over_time(v): the oldest value of each series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector: a |
required |
rate
¶
Return rate(v): the per-second average rate of increase of a counter.
Extrapolated to cover the full interval. Use on monotonically increasing counters; automatically handles counter resets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector of a counter: a |
required |
irate
¶
Return irate(v): the per-second rate from the last two samples of a counter.
More responsive than rate but more sensitive to spikes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector of a counter: a |
required |
increase
¶
Return increase(v): the increase of a counter over the range.
Extrapolated to cover the full interval; handles counter resets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector of a counter: a |
required |
delta
¶
Return delta(v): the difference between the first and last gauge value.
Extrapolated to cover the full interval. Use on gauges, not counters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector of a gauge: a |
required |
idelta
¶
Return idelta(v): the difference between the last two gauge values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector of a gauge: a |
required |
deriv
¶
Return deriv(v): the per-second derivative of a gauge.
Estimated with simple linear regression over the range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector of a gauge: a |
required |
resets
¶
Return resets(v): the number of counter resets in the range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector of a counter: a |
required |
changes
¶
Return changes(v): the number of times each value changed in the range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector of a gauge: a |
required |
vector
¶
vector(s: SCALAR_TYPE) -> Function
Return vector(s): scalar s as a single-element vector without labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
SCALAR_TYPE
|
Scalar to convert; a raw number is wrapped in |
required |
round_
¶
round_(v: Query, to_nearest: SCALAR_TYPE | None = None) -> Function
Return round(v, to_nearest): each sample rounded to a multiple of to_nearest.
Without to_nearest, samples are rounded to the nearest integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to round. |
required |
to_nearest
|
SCALAR_TYPE | None
|
Round to a multiple of this number instead of to an integer. |
None
|
Examples:
>>> temperature = InstantVector("temperature_celsius", [])
>>> print(round_(temperature))
round(temperature_celsius{})
>>> print(round_(temperature, 0.5))
round(temperature_celsius{}, 0.5)
clamp_max
¶
clamp_max(v: Query, max: SCALAR_TYPE) -> Function
Return clamp_max(v, max): each sample capped at max.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to clamp. |
required |
max
|
SCALAR_TYPE
|
Upper bound. |
required |
Examples:
>>> print(clamp_max(InstantVector("error_ratio", []), 1))
clamp_max(error_ratio{}, 1)
clamp_min
¶
clamp_min(v: Query, min: SCALAR_TYPE) -> Function
Return clamp_min(v, min): each sample raised to at least min.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to clamp. |
required |
min
|
SCALAR_TYPE
|
Lower bound. |
required |
Examples:
>>> print(clamp_min(InstantVector("free_bytes", []), 0))
clamp_min(free_bytes{}, 0)
clamp
¶
clamp(v: Query, min: SCALAR_TYPE, max: SCALAR_TYPE) -> Function
Return clamp(v, min, max): each sample limited to [min, max].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to clamp. |
required |
min
|
SCALAR_TYPE
|
Lower bound. |
required |
max
|
SCALAR_TYPE
|
Upper bound. |
required |
Examples:
>>> print(clamp(InstantVector("error_ratio", []), 0, 1))
clamp(error_ratio{}, 0, 1)
quantile_over_time
¶
quantile_over_time(phi: SCALAR_TYPE, v: Query) -> Function
Return quantile_over_time(phi, v): the phi-quantile of each series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phi
|
SCALAR_TYPE
|
Quantile to compute, from |
required |
v
|
Query
|
Range vector: a |
required |
Examples:
>>> latency = InstantVector("latency_seconds", [])
>>> print(quantile_over_time(0.95, latency[Duration(h=1)]))
quantile_over_time(0.95, latency_seconds{}[1h])
histogram_quantile
¶
histogram_quantile(phi: SCALAR_TYPE, b: Query) -> Function
Return histogram_quantile(phi, b): the phi-quantile estimated from b.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phi
|
SCALAR_TYPE
|
Quantile to compute, from |
required |
b
|
Query
|
Native histograms, or classic histogram buckets with an |
required |
Examples:
>>> buckets = InstantVector("http_request_duration_seconds_bucket", [])
>>> print(histogram_quantile(0.95, sum_(rate(buckets[Duration(m=5)])).by(["le"])))
histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket{}[5m])) by (le))
histogram_fraction
¶
histogram_fraction(lower: SCALAR_TYPE, upper: SCALAR_TYPE, b: Query) -> Function
Return histogram_fraction(lower, upper, b): the fraction in a value range.
Estimates the fraction of observations in b between lower and upper; bounds that fall
between bucket boundaries are interpolated as in
histogram_quantile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lower
|
SCALAR_TYPE
|
Lower bound of the observed values; may be |
required |
upper
|
SCALAR_TYPE
|
Upper bound of the observed values; may be |
required |
b
|
Query
|
Native histograms, or classic histogram buckets with an |
required |
Examples:
Fraction of requests in the last hour that took 200ms or less:
>>> duration = InstantVector("http_request_duration_seconds", [])
>>> print(histogram_fraction(0, 0.2, rate(duration[Duration(h=1)])))
histogram_fraction(0, 0.2, rate(http_request_duration_seconds{}[1h]))
predict_linear
¶
predict_linear(v: Query, t: SCALAR_TYPE) -> Function
Return predict_linear(v, t): the value of a gauge predicted t seconds ahead.
Uses simple linear regression over the range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector of a gauge: a |
required |
t
|
SCALAR_TYPE
|
Number of seconds after the evaluation time to predict the value for. |
required |
Examples:
>>> free = InstantVector("disk_free_bytes", [])
>>> print(predict_linear(free[Duration(h=6)], 4 * 3600))
predict_linear(disk_free_bytes{}[6h], 14400)
double_exponential_smoothing
¶
double_exponential_smoothing(v: Query, sf: SCALAR_TYPE, tf: SCALAR_TYPE) -> Function
Return double_exponential_smoothing(v, sf, tf): a smoothed gauge value.
Uses Holt's double exponential smoothing over the range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Range vector of a gauge: a |
required |
sf
|
SCALAR_TYPE
|
Smoothing factor, between |
required |
tf
|
SCALAR_TYPE
|
Trend factor, between |
required |
Note
Experimental — may change in future Prometheus releases.
Examples:
>>> temperature = InstantVector("temperature_celsius", [])
>>> print(double_exponential_smoothing(temperature[Duration(h=1)], 0.3, 0.1))
double_exponential_smoothing(temperature_celsius{}[1h], 0.3, 0.1)
day_of_month
¶
Return day_of_month(v): the day of the month (1-31) of each timestamp, in UTC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query | None
|
Instant vector of Unix timestamps; |
None
|
day_of_week
¶
Return day_of_week(v): the day of the week (0 = Sunday … 6 = Saturday), in UTC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query | None
|
Instant vector of Unix timestamps; |
None
|
day_of_year
¶
Return day_of_year(v): the day of the year (1-366) of each timestamp, in UTC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query | None
|
Instant vector of Unix timestamps; |
None
|
days_in_month
¶
Return days_in_month(v): the number of days in the month (28-31), in UTC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query | None
|
Instant vector of Unix timestamps; |
None
|
hour
¶
Return hour(v): the hour of the day (0-23) of each timestamp, in UTC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query | None
|
Instant vector of Unix timestamps; |
None
|
Examples:
>>> print(hour())
hour()
>>> print(hour(timestamp(InstantVector("up", []))))
hour(timestamp(up{}))
minute
¶
Return minute(v): the minute of the hour (0-59) of each timestamp, in UTC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query | None
|
Instant vector of Unix timestamps; |
None
|
month
¶
Return month(v): the month of the year (1-12) of each timestamp, in UTC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query | None
|
Instant vector of Unix timestamps; |
None
|
year
¶
Return year(v): the year of each timestamp, in UTC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query | None
|
Instant vector of Unix timestamps; |
None
|
label_join
¶
label_join(v: Query, dst_label: STRING_TYPE, separator: STRING_TYPE, *src_labels: STRING_TYPE) -> Function
Return label_join(v, ...): v with src_labels values joined into dst_label.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to relabel. |
required |
dst_label
|
STRING_TYPE
|
Label to write the joined value to. |
required |
separator
|
STRING_TYPE
|
String placed between the source label values. |
required |
*src_labels
|
STRING_TYPE
|
Labels whose values are joined, in order. |
()
|
Examples:
>>> print(label_join(InstantVector("up", []), "address", ":", "host", "port"))
label_join(up{}, "address", ":", "host", "port")
label_replace
¶
label_replace(v: Query, dst_label: STRING_TYPE, replacement: STRING_TYPE, src_label: STRING_TYPE, regex: STRING_TYPE) -> Function
Return label_replace(v, ...): v with dst_label set from a match on src_label.
Capture groups from regex may be referenced in replacement as $1, $2, etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to relabel. |
required |
dst_label
|
STRING_TYPE
|
Label to write the replacement to. |
required |
replacement
|
STRING_TYPE
|
Value to write; may reference capture groups of |
required |
src_label
|
STRING_TYPE
|
Label whose value |
required |
regex
|
STRING_TYPE
|
RE2 regular expression, fully anchored; series it does not match are returned unchanged. |
required |
Examples:
>>> up = InstantVector("up", [])
>>> print(label_replace(up, "host", "$1", "instance", "(.*):.*"))
label_replace(up{}, "host", "$1", "instance", "(.*):.*")
sort_by_label
¶
sort_by_label(v: Query, *labels: STRING_TYPE) -> Function
Return sort_by_label(v, label, ...): the elements sorted by label, ascending.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to sort. |
required |
*labels
|
STRING_TYPE
|
Labels to sort by, in order of priority. |
()
|
Examples:
>>> print(sort_by_label(InstantVector("up", []), "job", "instance"))
sort_by_label(up{}, "job", "instance")
sort_by_label_desc
¶
sort_by_label_desc(v: Query, *labels: STRING_TYPE) -> Function
Return sort_by_label_desc(v, label, ...): the elements sorted by label, descending.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to sort. |
required |
*labels
|
STRING_TYPE
|
Labels to sort by, in order of priority. |
()
|
Examples:
>>> print(sort_by_label_desc(InstantVector("up", []), "job", "instance"))
sort_by_label_desc(up{}, "job", "instance")
info
¶
Return info(v, data_label_selector): v with data labels from info series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Query
|
Instant vector to add data labels to. |
required |
data_label_selector
|
Query | None
|
Braces with label matchers only (an
|
None
|
Note
Experimental — may change in future Prometheus releases. Needs the
--enable-feature=promql-experimental-functions flag.
Examples:
>>> requests = InstantVector("http_requests_total", [])
>>> cluster = InstantVector("", [Label.re("k8s_cluster_name", ".+")])
>>> print(info(requests))
info(http_requests_total{})
>>> print(info(requests, cluster))
info(http_requests_total{}, { k8s_cluster_name =~ ".+" })