Skip to content

Aggregations

Grouping

Grouping(type: Literal['by', 'without'], labels: list[str | Variable])

Aggregation grouping clause that controls which label dimensions appear in results.

  • by(labels) — keep only the listed labels in the output.
  • without(labels) — drop the listed labels; preserve all others.

Parameters:

Name Type Description Default
type Literal['by', 'without']

"by" or "without".

required
labels list[str | Variable]

Label names, or Variables; [] renders an empty list, e.g. by ().

required

Examples:

>>> print(Grouping.by(["job", "env"]))
by (job, env)
>>> print(Grouping.without(["instance"]))
without (instance)

by classmethod

by(labels: list[str | Variable]) -> Grouping

Return a by(labels) grouping clause.

Parameters:

Name Type Description Default
labels list[str | Variable]

Label names, or Variables to keep.

required

without classmethod

without(labels: list[str | Variable]) -> Grouping

Return a without(labels) grouping clause.

Parameters:

Name Type Description Default
labels list[str | Variable]

Label names, or Variables to drop.

required

to_string

to_string(style: Style = DEFAULT_STYLE) -> str

Render this clause, honouring style.paren_space.

Aggregation

Aggregation(operator: str, params: list[OPERAND_TYPE], *, grouping: Grouping | None = None)

Bases: Query

A PromQL aggregation expression that collapses multiple time series into fewer results.

Usually built with a helper such as sum_ or topk; by and without return a copy with a grouping clause.

Parameters:

Name Type Description Default
operator str

Aggregation operator name, e.g. "sum".

required
params list[OPERAND_TYPE]

Arguments in order: the parameter (k, phi, ...) first, if the operator takes one, then the vector. Raw numbers are wrapped in Float.

required
grouping Grouping | None

by/without clause; usually set with by or without.

None

Examples:

>>> http_requests_total = InstantVector("http_requests_total", [])
>>> expr = sum_(rate(http_requests_total[Duration(m=5)])).by(["job"])
>>> print(expr)
sum(rate(http_requests_total{}[5m])) by (job)
>>> print(expr.to_string(style=Style(grouping=Style.GroupingPosition.PREFIX)))
sum by (job) (rate(http_requests_total{}[5m]))
>>> print(topk(3, http_requests_total).without(["instance"]))
topk(3, http_requests_total{}) without (instance)

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 (3600 → [1h:]).

required
resolution SCALAR_TYPE | None

Step between evaluations; None uses the global evaluation interval.

None
offset SCALAR_TYPE | None

Time shift; raw numbers are seconds.

None
at AT_TYPE | None

Evaluation time: a Unix timestamp, "start()" or "end()".

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.

by

by(labels: list[str | Variable]) -> Aggregation

Return a copy of this aggregation with a by(labels) grouping clause.

Parameters:

Name Type Description Default
labels list[str | Variable]

Label names, or Variables to keep.

required

without

without(labels: list[str | Variable]) -> Aggregation

Return a copy of this aggregation with a without(labels) grouping clause.

Parameters:

Name Type Description Default
labels list[str | Variable]

Label names, or Variables to drop.

required

sum_

sum_(query: Query) -> Aggregation

Return sum(query): the sum of the sample values in each group.

Parameters:

Name Type Description Default
query Query

Instant vector to aggregate.

required

Examples:

>>> requests = InstantVector("http_requests_total", [])
>>> print(sum_(requests))
sum(http_requests_total{})
>>> print(sum_(requests).by(["job"]))
sum(http_requests_total{}) by (job)

avg

avg(query: Query) -> Aggregation

Return avg(query): the average of the sample values in each group.

Parameters:

Name Type Description Default
query Query

Instant vector to aggregate.

required

Examples:

>>> print(avg(InstantVector("cpu_usage", [])).without(["cpu"]))
avg(cpu_usage{}) without (cpu)

min_

min_(query: Query) -> Aggregation

Return min(query): the smallest sample value in each group.

Parameters:

Name Type Description Default
query Query

Instant vector to aggregate.

required

Examples:

>>> print(min_(InstantVector("disk_free_bytes", [])).by(["instance"]))
min(disk_free_bytes{}) by (instance)

max_

max_(query: Query) -> Aggregation

Return max(query): the largest sample value in each group.

Parameters:

Name Type Description Default
query Query

Instant vector to aggregate.

required

Examples:

>>> print(max_(InstantVector("memory_used_bytes", [])).by(["instance"]))
max(memory_used_bytes{}) by (instance)

count

count(query: Query) -> Aggregation

Return count(query): the number of series in each group.

Parameters:

Name Type Description Default
query Query

Instant vector to aggregate.

required

Examples:

>>> print(count(InstantVector("up", [])).by(["job"]))
count(up{}) by (job)

group

group(query: Query) -> Aggregation

Return group(query): 1 for each group that has at least one series.

Parameters:

Name Type Description Default
query Query

Instant vector to aggregate.

required

Examples:

>>> print(group(InstantVector("up", [])).by(["job"]))
group(up{}) by (job)

stddev

stddev(query: Query) -> Aggregation

Return stddev(query): the population standard deviation in each group.

Parameters:

Name Type Description Default
query Query

Instant vector to aggregate.

required

Examples:

>>> print(stddev(InstantVector("cpu_usage", [])).by(["instance"]))
stddev(cpu_usage{}) by (instance)

stdvar

stdvar(query: Query) -> Aggregation

Return stdvar(query): the population variance in each group.

Parameters:

Name Type Description Default
query Query

Instant vector to aggregate.

required

Examples:

>>> print(stdvar(InstantVector("cpu_usage", [])).by(["instance"]))
stdvar(cpu_usage{}) by (instance)

topk

topk(k: SCALAR_TYPE, query: Query) -> Aggregation

Return topk(k, query): the k largest series in each group.

Parameters:

Name Type Description Default
k SCALAR_TYPE

Number of series to keep per group.

required
query Query

Instant vector to aggregate.

required

Examples:

>>> print(topk(5, InstantVector("http_requests_total", [])).by(["job"]))
topk(5, http_requests_total{}) by (job)

bottomk

bottomk(k: SCALAR_TYPE, query: Query) -> Aggregation

Return bottomk(k, query): the k smallest series in each group.

Parameters:

Name Type Description Default
k SCALAR_TYPE

Number of series to keep per group.

required
query Query

Instant vector to aggregate.

required

Examples:

>>> print(bottomk(3, InstantVector("disk_free_bytes", [])))
bottomk(3, disk_free_bytes{})

count_values

count_values(label: STRING_TYPE, query: Query) -> Aggregation

Return count_values(label, query): the number of series per distinct value.

Each output series carries that sample value in a new label named label, and its value is the number of input series that had it.

Parameters:

Name Type Description Default
label STRING_TYPE

Name of the output label that holds each distinct sample value.

required
query Query

Instant vector to aggregate.

required

Examples:

>>> print(count_values("version", InstantVector("build_info", [])))
count_values("version", build_info{})

quantile

quantile(phi: SCALAR_TYPE, query: Query) -> Aggregation

Return quantile(phi, query): the phi-quantile of the values in each group.

Parameters:

Name Type Description Default
phi SCALAR_TYPE

Quantile to compute, from 0 to 1 (0.95 is the 95th percentile).

required
query Query

Instant vector to aggregate.

required

Examples:

>>> print(quantile(0.95, InstantVector("latency_seconds", [])).by(["job"]))
quantile(0.95, latency_seconds{}) by (job)

limitk

limitk(k: SCALAR_TYPE, query: Query) -> Aggregation

Return limitk(k, query): at most k pseudo-random series per group.

Parameters:

Name Type Description Default
k SCALAR_TYPE

Maximum number of series to keep per group.

required
query Query

Instant vector to aggregate.

required

Examples:

>>> print(limitk(10, InstantVector("up", [])).by(["job"]))
limitk(10, up{}) by (job)

limit_ratio

limit_ratio(r: SCALAR_TYPE, query: Query) -> Aggregation

Return limit_ratio(r, query): a pseudo-random ratio r of the samples.

Parameters:

Name Type Description Default
r SCALAR_TYPE

Ratio of samples to keep, from -1 to 1. The selection is deterministic; a negative r inverts its order, so limit_ratio(-0.9, v) returns exactly the samples that limit_ratio(0.1, v) leaves out.

required
query Query

Instant vector to aggregate.

required

Examples:

>>> print(limit_ratio(0.1, InstantVector("up", [])))
limit_ratio(0.1, up{})