Query and Style¶
Style
dataclass
¶
Style(*, empty_braces: bool = True, grouping: GroupingPosition = SUFFIX, compact_matchers: bool = False, paren_space: bool = True)
Rendering options for to_string; the defaults reproduce str(query).
Attributes:
| Name | Type | Description |
|---|---|---|
empty_braces |
bool
|
Render |
grouping |
GroupingPosition
|
Place |
compact_matchers |
bool
|
Render |
paren_space |
bool
|
Render |
Examples:
>>> up = InstantVector("up", [Label.eq("job", "api")])
>>> expr = sum_(rate(up[Duration(m=5)])).by(["job"])
>>> print(expr.to_string(style=Style.prometheus()))
sum by (job) (rate(up{job="api"}[5m]))
Query
¶
Bases: ABC
Base class for all PromQL query components.
to_string
abstractmethod
¶
to_string(indent: str | int | None = None, _indent_level: int = 0, *, style: Style = DEFAULT_STYLE) -> str
Render this expression as a PromQL string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indent
|
str | int | None
|
Enables multi-line output: an |
None
|
_indent_level
|
int
|
Current nesting depth; internal, leave at |
0
|
style
|
Style
|
Formatting options, see |
DEFAULT_STYLE
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
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
|