Skip to content

Selectors and subqueries

Label

Label(name: str | Variable, op: Operator, value: STRING_TYPE)

A PromQL label matcher used to filter time series.

Label matchers narrow the set of time series returned by a vector selector. Four operators are available:

  • = (Label.eq) — exact string equality
  • != (Label.neq) — string inequality
  • =~ (Label.re) — RE2 regular-expression match (fully anchored)
  • !~ (Label.nre) — negated RE2 regular-expression match

The matcher value may be a String, a raw str or a Variable; the latter two are wrapped in a double-quoted String.

Parameters:

Name Type Description Default
name str | Variable

Label name, or a Variable.

required
op Operator

Matching operator; the factory methods (eq, ...) set it.

required
value STRING_TYPE

Value or RE2 regex to match: a String, or a str or Variable wrapped in a double-quoted String.

required

Examples:

>>> print(Label.eq("env", "prod"))
env = "prod"
>>> print(Label.re("job", String("api.*", "'")))
job =~ 'api.*'
>>> print(Label.eq("env", Variable("env")))
env = "$env"

Operator

Bases: Enum

Enum of PromQL label matching operators.

to_string

to_string(style: Style = DEFAULT_STYLE) -> str

Render this matcher, honouring style.compact_matchers.

eq classmethod

eq(name: str | Variable, value: STRING_TYPE) -> Label

Return a label matcher that requires name = value (exact equality).

Parameters:

Name Type Description Default
name str | Variable

Label name, or a Variable.

required
value STRING_TYPE

Value to compare with: a str, String or Variable.

required

neq classmethod

neq(name: str | Variable, value: STRING_TYPE) -> Label

Return a label matcher that requires name != value (inequality).

Parameters:

Name Type Description Default
name str | Variable

Label name, or a Variable.

required
value STRING_TYPE

Value to compare with: a str, String or Variable.

required

re classmethod

re(name: str | Variable, value: STRING_TYPE) -> Label

Return a label matcher that requires name =~ value (RE2 regex match).

Parameters:

Name Type Description Default
name str | Variable

Label name, or a Variable.

required
value STRING_TYPE

RE2 regex, fully anchored: a str, String or Variable.

required

nre classmethod

nre(name: str | Variable, value: STRING_TYPE) -> Label

Return a label matcher that requires name !~ value (negated RE2 regex match).

Parameters:

Name Type Description Default
name str | Variable

Label name, or a Variable.

required
value STRING_TYPE

RE2 regex, fully anchored: a str, String or Variable.

required

InstantVector

InstantVector(metric: str | Variable, labels: list[Label], *, offset: SCALAR_TYPE | None = None, at: AT_TYPE | None = None)

Bases: TimeModifiers, Query

A PromQL instant vector selector.

Selects a set of time series and a single sample value for each at the query evaluation timestamp. Results can be filtered by label matchers and shifted in time with offset or pinned to an absolute Unix timestamp with @ (also accepts the special values "start()" and "end()").

Indexing builds a RangeVector that keeps the metric, matchers and modifiers (v[Duration(m=5)], or v[range, resolution]), while slicing builds a Subquery (v[Duration(h=1):]).

Parameters:

Name Type Description Default
metric str | Variable

Metric name, or a Variable.

required
labels list[Label]

Label matchers; [] selects every series of the metric.

required
offset SCALAR_TYPE | None

Time shift; raw numbers are seconds (300 → offset 5m).

None
at AT_TYPE | None

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

None

Examples:

>>> print(InstantVector("http_requests_total", [Label.eq("job", String("api"))]))
http_requests_total{ job = "api" }
>>> print(InstantVector("up", [], offset=Duration(m=5)))
up{} offset 5m

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

with_offset

with_offset(offset: SCALAR_TYPE) -> Self

Return a copy of this expression with the given offset.

Parameters:

Name Type Description Default
offset SCALAR_TYPE

Time shift; raw numbers are seconds.

required

with_at

with_at(at: AT_TYPE) -> Self

Return a copy of this expression with the given @ time.

Parameters:

Name Type Description Default
at AT_TYPE

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

required

with_at_start

with_at_start() -> Self

Return a copy of this expression with @ start().

with_at_end

with_at_end() -> Self

Return a copy of this expression with @ end().

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.

RangeVector

RangeVector(metric: str | Variable, labels: list[Label], range: SCALAR_TYPE, resolution: SCALAR_TYPE | None = None, *, offset: SCALAR_TYPE | None = None, at: AT_TYPE | None = None)

Bases: TimeModifiers, Query

A PromQL range vector selector.

Selects a set of time series and a range of sample values for each, extending backwards from the query evaluation timestamp by range. The window is left-open and right-closed.

An optional sub-query resolution step may be provided to control the evaluation frequency. Results can be shifted with offset or pinned to an absolute time with @.

Parameters:

Name Type Description Default
metric str | Variable

Metric name, or a Variable.

required
labels list[Label]

Label matchers; [] selects every series of the metric.

required
range SCALAR_TYPE

Window length; raw numbers are seconds (300 → [5m]).

required
resolution SCALAR_TYPE | None

Optional step, rendered as [range:resolution].

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

Examples:

>>> print(RangeVector("http_requests_total", [], range=Duration(m=5)))
http_requests_total{}[5m]
>>> print(RangeVector("rate_query", [], range=Duration(h=1), resolution=Duration(m=1)))
rate_query{}[1h:1m]

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

with_offset

with_offset(offset: SCALAR_TYPE) -> Self

Return a copy of this expression with the given offset.

Parameters:

Name Type Description Default
offset SCALAR_TYPE

Time shift; raw numbers are seconds.

required

with_at

with_at(at: AT_TYPE) -> Self

Return a copy of this expression with the given @ time.

Parameters:

Name Type Description Default
at AT_TYPE

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

required

with_at_start

with_at_start() -> Self

Return a copy of this expression with @ start().

with_at_end

with_at_end() -> Self

Return a copy of this expression with @ end().

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.

Subquery

Subquery(query: Query, range: SCALAR_TYPE, resolution: SCALAR_TYPE | None = None, *, offset: SCALAR_TYPE | None = None, at: AT_TYPE | None = None)

Bases: TimeModifiers, Query

A PromQL subquery: <expr>[<range>:<resolution>].

Evaluates query (any instant-vector expression) over range at every resolution step, producing a range vector that can be passed to range-vector functions such as max_over_time. Without resolution the global evaluation interval is used ([1h:]). Binary operations and negations are parenthesised. Like selectors, subqueries accept offset and @ modifiers.

Usually built with Query.subquery.

Parameters:

Name Type Description Default
query Query

Any instant-vector expression.

required
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

Raises:

Type Description
TypeError

If query is already a range vector or subquery.

Examples:

>>> http_requests_total = InstantVector("http_requests_total", [])
>>> up = InstantVector("up", [])
>>> down = InstantVector("down", [])
>>> print(rate(http_requests_total[Duration(m=5)]).subquery(Duration(h=1), Duration(m=1)))
rate(http_requests_total{}[5m])[1h:1m]
>>> print(Subquery(up + down, Duration(h=1), offset=Duration(d=1)))
(up{} + down{})[1h:] offset 1d

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

with_offset

with_offset(offset: SCALAR_TYPE) -> Self

Return a copy of this expression with the given offset.

Parameters:

Name Type Description Default
offset SCALAR_TYPE

Time shift; raw numbers are seconds.

required

with_at

with_at(at: AT_TYPE) -> Self

Return a copy of this expression with the given @ time.

Parameters:

Name Type Description Default
at AT_TYPE

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

required

with_at_start

with_at_start() -> Self

Return a copy of this expression with @ start().

with_at_end

with_at_end() -> Self

Return a copy of this expression with @ end().

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.