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 |
required |
op
|
Operator
|
Matching operator; the factory methods ( |
required |
value
|
STRING_TYPE
|
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 |
required |
value
|
STRING_TYPE
|
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 |
required |
value
|
STRING_TYPE
|
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 |
required |
value
|
STRING_TYPE
|
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 |
required |
value
|
STRING_TYPE
|
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 |
required |
labels
|
list[Label]
|
Label matchers; |
required |
offset
|
SCALAR_TYPE | None
|
Time shift; raw numbers are seconds ( |
None
|
at
|
AT_TYPE | None
|
Evaluation time: a Unix timestamp, |
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 ( |
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
|
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, |
required |
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 |
required |
labels
|
list[Label]
|
Label matchers; |
required |
range
|
SCALAR_TYPE
|
Window length; raw numbers are seconds ( |
required |
resolution
|
SCALAR_TYPE | None
|
Optional step, rendered as |
None
|
offset
|
SCALAR_TYPE | None
|
Time shift; raw numbers are seconds. |
None
|
at
|
AT_TYPE | None
|
Evaluation time: a Unix timestamp, |
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 ( |
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
|
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, |
required |
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 ( |
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
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
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 ( |
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
|
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, |
required |
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.