Skip to content

Binary operators

Match

Match(type: Literal['on', 'ignoring'], labels: list[str | Variable])

Vector matching clause that controls which labels are used when pairing series.

When two instant vectors are combined with a binary operator, Prometheus must match each left-hand element to a right-hand element. Match configures this behaviour:

  • on(labels) — match only on the listed labels, ignore all others.
  • ignoring(labels) — exclude the listed labels from matching; use all others.

Parameters:

Name Type Description Default
type Literal['on', 'ignoring']

"on" or "ignoring".

required
labels list[str | Variable]

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

required

Examples:

>>> print(Match.on(["env", "job"]))
on (env, job)
>>> print(Match.ignoring(["instance"]))
ignoring (instance)

on classmethod

on(labels: list[str | Variable]) -> Match

Return an on(labels) match clause.

Parameters:

Name Type Description Default
labels list[str | Variable]

Label names, or Variables to match on.

required

ignoring classmethod

ignoring(labels: list[str | Variable]) -> Match

Return an ignoring(labels) match clause.

Parameters:

Name Type Description Default
labels list[str | Variable]

Label names, or Variables to leave out of matching.

required

to_string

to_string(style: Style = DEFAULT_STYLE) -> str

Render this clause, honouring style.paren_space.

Group

Group(type: Literal['left', 'right'], labels: list[str | Variable])

Group modifier enabling many-to-one or one-to-many vector matching.

By default binary operations require a one-to-one match. When the cardinalities differ, a Group modifier must be added:

  • group_left(labels) — multiple left-side series may match one right-side series.
  • group_right(labels) — multiple right-side series may match one left-side series.

The optional labels list copies those labels from the "one" side into the result.

Parameters:

Name Type Description Default
type Literal['left', 'right']

"left" or "right".

required
labels list[str | Variable]

Label names, or Variables to copy from the "one" side; may be empty.

required

Examples:

>>> print(Group.left(["region"]))
group_left (region)
>>> print(Group.right([]))
group_right ()

left classmethod

left(labels: list[str | Variable]) -> Group

Return a group_left(labels) modifier.

Parameters:

Name Type Description Default
labels list[str | Variable]

Label names, or Variables to copy from the right side; may be empty.

required

right classmethod

right(labels: list[str | Variable]) -> Group

Return a group_right(labels) modifier.

Parameters:

Name Type Description Default
labels list[str | Variable]

Label names, or Variables to copy from the left side; may be empty.

required

to_string

to_string(style: Style = DEFAULT_STYLE) -> str

Render this modifier, honouring style.paren_space.

BinaryOprator

BinaryOprator(op: Operator, left: OPERAND_TYPE, right: OPERAND_TYPE, match: Match | None = None, group: Group | None = None, *, return_bool: bool = False)

Bases: Query

A PromQL binary operation between two query expressions.

Supports arithmetic, comparison, logical/set, and trigonometric operators (see BinaryOprator.Operator). Nested BinaryOprator operands are automatically parenthesised to preserve evaluation order.

Vector matching behaviour can be refined via the fluent methods on, ignoring, group_left, and group_right, each of which returns a new immutable instance. A group modifier without on/ignoring adds ignoring (), since PromQL requires a matching clause before it.

Usually built with a helper such as add or with Python's own operators (a + b, a > 0, a & b).

Parameters:

Name Type Description Default
op Operator

Operator to apply.

required
left OPERAND_TYPE

Left operand: a query, or a raw number wrapped in Float.

required
right OPERAND_TYPE

Right operand: a query, or a raw number wrapped in Float.

required
match Match | None

on/ignoring clause; usually set with on or ignoring.

None
group Group | None

group_left/group_right modifier; usually set with group_left or group_right.

None
return_bool bool

Add the bool modifier to a comparison; usually set with bool.

False
Note

The class name BinaryOprator (missing an 'e') is intentional and preserved for backwards compatibility.

Examples:

>>> http_requests_total = InstantVector("http_requests_total", [])
>>> http_errors_total = InstantVector("http_errors_total", [])
>>> errors = InstantVector("errors", [])
>>> print(add(http_requests_total, http_errors_total).on(["env"]))
http_requests_total{} + on (env) http_errors_total{}
>>> print(gt(errors, Float(0)).bool())
errors{} > bool 0

Raises:

Type Description
ValueError

If return_bool is set on a non-comparison operator, a group modifier is used with and/or/unless, or on/ignoring/ group modifiers are applied to a scalar operand.

Operator

Bases: Enum

Enum of all PromQL binary operators.

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.

bool

bool() -> BinaryOprator

Return a copy of this comparison with the bool modifier.

Instead of filtering, the comparison then returns 0 or 1 for each element. Required when both operands are scalars.

Raises:

Type Description
ValueError

If this is not a comparison operation.

on

on(labels: list[str | Variable]) -> BinaryOprator

Return a copy of this operation with an on(labels) match clause.

Parameters:

Name Type Description Default
labels list[str | Variable]

Label names, or Variables to match on.

required

ignoring

ignoring(labels: list[str | Variable]) -> BinaryOprator

Return a copy of this operation with an ignoring(labels) match clause.

Parameters:

Name Type Description Default
labels list[str | Variable]

Label names, or Variables to leave out of matching.

required

group_left

group_left(labels: list[str | Variable]) -> BinaryOprator

Return a copy of this operation with a group_left(labels) modifier.

Many left-side series may match one right-side series; labels are copied from the right ("one") side.

Parameters:

Name Type Description Default
labels list[str | Variable]

Label names, or Variables to copy from the right side; may be empty.

required

Raises:

Type Description
ValueError

If this is an and/or/unless operation.

group_right

group_right(labels: list[str | Variable]) -> BinaryOprator

Return a copy of this operation with a group_right(labels) modifier.

The mirror image of group_left.

Parameters:

Name Type Description Default
labels list[str | Variable]

Label names, or Variables to copy from the left side; may be empty.

required

Raises:

Type Description
ValueError

If this is an and/or/unless operation.

Negation

Negation(operand: OPERAND_TYPE)

Bases: Query

Unary minus applied to a PromQL expression (-expr).

Binary-operation operands are parenthesised so the sign applies to the whole expression.

Parameters:

Name Type Description Default
operand OPERAND_TYPE

Expression to negate; a raw number is wrapped in Float.

required

Examples:

>>> errors_total = InstantVector("errors_total", [])
>>> a = InstantVector("a", [])
>>> b = InstantVector("b", [])
>>> print(-rate(errors_total[Duration(m=5)]))
-rate(errors_total{}[5m])
>>> print(-(a + b))
-(a{} + b{})

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.

add

add(left: OPERAND_TYPE, right: OPERAND_TYPE) -> BinaryOprator

Return left + right: arithmetic addition.

Parameters:

Name Type Description Default
left OPERAND_TYPE

Left operand: a query, or a raw number wrapped in Float.

required
right OPERAND_TYPE

Right operand: a query, or a raw number wrapped in Float.

required

Examples:

>>> hits = InstantVector("cache_hits_total", [])
>>> misses = InstantVector("cache_misses_total", [])
>>> print(add(hits, misses))
cache_hits_total{} + cache_misses_total{}
>>> print(hits + misses)
cache_hits_total{} + cache_misses_total{}

sub

sub(left: OPERAND_TYPE, right: OPERAND_TYPE) -> BinaryOprator

Return left - right: arithmetic subtraction.

Parameters:

Name Type Description Default
left OPERAND_TYPE

Left operand: a query, or a raw number wrapped in Float.

required
right OPERAND_TYPE

Right operand: a query, or a raw number wrapped in Float.

required

Examples:

>>> total = InstantVector("memory_total_bytes", [])
>>> free = InstantVector("memory_free_bytes", [])
>>> print(sub(total, free))
memory_total_bytes{} - memory_free_bytes{}
>>> print(total - free)
memory_total_bytes{} - memory_free_bytes{}

mul

mul(left: OPERAND_TYPE, right: OPERAND_TYPE) -> BinaryOprator

Return left * right: arithmetic multiplication.

Parameters:

Name Type Description Default
left OPERAND_TYPE

Left operand: a query, or a raw number wrapped in Float.

required
right OPERAND_TYPE

Right operand: a query, or a raw number wrapped in Float.

required

Examples:

>>> ratio = InstantVector("error_ratio", [])
>>> print(mul(ratio, 100))
error_ratio{} * 100
>>> print(ratio * 100)
error_ratio{} * 100

div

div(left: OPERAND_TYPE, right: OPERAND_TYPE) -> BinaryOprator

Return left / right: arithmetic division.

Parameters:

Name Type Description Default
left OPERAND_TYPE

Left operand: a query, or a raw number wrapped in Float.

required
right OPERAND_TYPE

Right operand: a query, or a raw number wrapped in Float.

required

Examples:

>>> errors = InstantVector("http_errors_total", [])
>>> requests = InstantVector("http_requests_total", [])
>>> print(div(errors, requests))
http_errors_total{} / http_requests_total{}
>>> print(errors / requests)
http_errors_total{} / http_requests_total{}

mod

mod(left: OPERAND_TYPE, right: OPERAND_TYPE) -> BinaryOprator

Return left % right: arithmetic modulo.

Parameters:

Name Type Description Default
left OPERAND_TYPE

Left operand: a query, or a raw number wrapped in Float.

required
right OPERAND_TYPE

Right operand: a query, or a raw number wrapped in Float.

required

Examples:

>>> uptime = InstantVector("uptime_seconds", [])
>>> print(mod(uptime, 60))
uptime_seconds{} % 60
>>> print(uptime % 60)
uptime_seconds{} % 60

pow_

pow_(left: OPERAND_TYPE, right: OPERAND_TYPE) -> BinaryOprator

Return left ^ right: exponentiation.

Parameters:

Name Type Description Default
left OPERAND_TYPE

Left operand: a query, or a raw number wrapped in Float.

required
right OPERAND_TYPE

Right operand: a query, or a raw number wrapped in Float.

required

Examples:

>>> growth = InstantVector("growth_factor", [])
>>> print(pow_(growth, 2))
growth_factor{} ^ 2
>>> print(growth**2)
growth_factor{} ^ 2

eq

Return left == right: the equality comparison.

Keeps the elements of left for which the comparison is true; bool returns 0/1 instead.

Parameters:

Name Type Description Default
left OPERAND_TYPE

Left operand: a query, or a raw number wrapped in Float.

required
right OPERAND_TYPE

Right operand: a query, or a raw number wrapped in Float.

required

Examples:

>>> up = InstantVector("up", [])
>>> print(eq(up, 0))
up{} == 0
>>> print(up == 0)
up{} == 0
>>> print(eq(up, 0).bool())
up{} == bool 0

neq

neq(left: OPERAND_TYPE, right: OPERAND_TYPE) -> BinaryOprator

Return left != right: the inequality comparison.

Keeps the elements of left for which the comparison is true; bool returns 0/1 instead.

Parameters:

Name Type Description Default
left OPERAND_TYPE

Left operand: a query, or a raw number wrapped in Float.

required
right OPERAND_TYPE

Right operand: a query, or a raw number wrapped in Float.

required

Examples:

>>> up = InstantVector("up", [])
>>> print(neq(up, 1))
up{} != 1
>>> print(up != 1)
up{} != 1

lt

Return left < right: the less-than comparison.

Keeps the elements of left for which the comparison is true; bool returns 0/1 instead.

Parameters:

Name Type Description Default
left OPERAND_TYPE

Left operand: a query, or a raw number wrapped in Float.

required
right OPERAND_TYPE

Right operand: a query, or a raw number wrapped in Float.

required

Examples:

>>> free = InstantVector("disk_free_ratio", [])
>>> print(lt(free, 0.1))
disk_free_ratio{} < 0.1
>>> print(free < 0.1)
disk_free_ratio{} < 0.1

lte

lte(left: OPERAND_TYPE, right: OPERAND_TYPE) -> BinaryOprator

Return left <= right: the less-than-or-equal comparison.

Keeps the elements of left for which the comparison is true; bool returns 0/1 instead.

Parameters:

Name Type Description Default
left OPERAND_TYPE

Left operand: a query, or a raw number wrapped in Float.

required
right OPERAND_TYPE

Right operand: a query, or a raw number wrapped in Float.

required

Examples:

>>> free = InstantVector("disk_free_ratio", [])
>>> print(lte(free, 0.1))
disk_free_ratio{} <= 0.1
>>> print(free <= 0.1)
disk_free_ratio{} <= 0.1

gt

Return left > right: the greater-than comparison.

Keeps the elements of left for which the comparison is true; bool returns 0/1 instead.

Parameters:

Name Type Description Default
left OPERAND_TYPE

Left operand: a query, or a raw number wrapped in Float.

required
right OPERAND_TYPE

Right operand: a query, or a raw number wrapped in Float.

required

Examples:

>>> latency = InstantVector("latency_seconds", [])
>>> print(gt(latency, 0.5))
latency_seconds{} > 0.5
>>> print(latency > 0.5)
latency_seconds{} > 0.5

gte

gte(left: OPERAND_TYPE, right: OPERAND_TYPE) -> BinaryOprator

Return left >= right: the greater-than-or-equal comparison.

Keeps the elements of left for which the comparison is true; bool returns 0/1 instead.

Parameters:

Name Type Description Default
left OPERAND_TYPE

Left operand: a query, or a raw number wrapped in Float.

required
right OPERAND_TYPE

Right operand: a query, or a raw number wrapped in Float.

required

Examples:

>>> latency = InstantVector("latency_seconds", [])
>>> print(gte(latency, 0.5))
latency_seconds{} >= 0.5
>>> print(latency >= 0.5)
latency_seconds{} >= 0.5

and_

and_(left: OPERAND_TYPE, right: OPERAND_TYPE) -> BinaryOprator

Return left and right: set intersection.

Keeps the elements of left whose label set has a match in right.

Parameters:

Name Type Description Default
left OPERAND_TYPE

Instant vector whose elements are filtered.

required
right OPERAND_TYPE

Instant vector whose label sets select the left elements to keep.

required

Examples:

>>> errors = InstantVector("errors_total", [])
>>> up = InstantVector("up", [])
>>> print(and_(errors, up == 1).on(["instance"]))
errors_total{} and on (instance) (up{} == 1)
>>> print((errors & (up == 1)).on(["instance"]))
errors_total{} and on (instance) (up{} == 1)

or_

or_(left: OPERAND_TYPE, right: OPERAND_TYPE) -> BinaryOprator

Return left or right: set union.

Keeps every element of left, plus the elements of right whose label set has no match in left.

Parameters:

Name Type Description Default
left OPERAND_TYPE

Instant vector whose elements are all kept.

required
right OPERAND_TYPE

Instant vector whose elements are added where left has no matching label set.

required

Examples:

>>> up = InstantVector("up", [])
>>> print(or_(up, vector(0)))
up{} or vector(0)
>>> print(up | vector(0))
up{} or vector(0)

unless

unless(left: OPERAND_TYPE, right: OPERAND_TYPE) -> BinaryOprator

Return left unless right: set complement.

Keeps the elements of left whose label set has no match in right.

Parameters:

Name Type Description Default
left OPERAND_TYPE

Instant vector whose elements are filtered.

required
right OPERAND_TYPE

Instant vector whose label sets select the left elements to drop.

required

Examples:

>>> up = InstantVector("up", [])
>>> maintenance = InstantVector("maintenance_mode", [])
>>> print(unless(up, maintenance).on(["instance"]))
up{} unless on (instance) maintenance_mode{}

atan2

atan2(left: OPERAND_TYPE, right: OPERAND_TYPE) -> BinaryOprator

Return left atan2 right: the two-argument arctangent, in radians.

Applied element-wise, with the same vector matching as the arithmetic operators.

Parameters:

Name Type Description Default
left OPERAND_TYPE

Left operand: a query, or a raw number wrapped in Float.

required
right OPERAND_TYPE

Right operand: a query, or a raw number wrapped in Float.

required

Examples:

>>> y = InstantVector("position_y", [])
>>> x = InstantVector("position_x", [])
>>> print(atan2(y, x))
position_y{} atan2 position_x{}