promql-parser 0.4.2__cp313-cp313-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- promql_parser/__init__.py +5 -0
- promql_parser/__init__.pyi +269 -0
- promql_parser/promql_parser.cp313-win_amd64.pyd +0 -0
- promql_parser/py.typed +0 -0
- promql_parser-0.4.2.dist-info/METADATA +39 -0
- promql_parser-0.4.2.dist-info/RECORD +8 -0
- promql_parser-0.4.2.dist-info/WHEEL +4 -0
- promql_parser-0.4.2.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"""PromQL Lexer and Parser
|
|
2
|
+
|
|
3
|
+
The goal of this project is to build a PromQL lexer and parser capable of
|
|
4
|
+
parsing PromQL that conforms with [Prometheus Query](https://prometheus.io/docs/prometheus/latest/querying/basics/).
|
|
5
|
+
|
|
6
|
+
## Example
|
|
7
|
+
|
|
8
|
+
The parser entry point is `promql_parser.parse`, which takes a string slice of PromQL
|
|
9
|
+
and returns the parse result as an AST (`promql_parser.Expr`).
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
import promql_parser
|
|
13
|
+
|
|
14
|
+
promql = 'http_requests_total{environment=~"staging|testing|development",method!="GET"} offset 5m'
|
|
15
|
+
expr = promql_parser.parse(promql)
|
|
16
|
+
print(f"Prettify:\n{expr.prettify()}")
|
|
17
|
+
print(f"AST:\n{expr}")
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
This outputs:
|
|
21
|
+
|
|
22
|
+
```text
|
|
23
|
+
Prettify:
|
|
24
|
+
http_requests_total{environment=~"staging|testing|development",method!="GET"} offset 5m
|
|
25
|
+
|
|
26
|
+
AST:
|
|
27
|
+
VectorSelector(VectorSelector { name: Some("http_requests_total"), matchers: Matchers { matchers: [Matcher { op: Re(staging|testing|development), name: "environment", value: "staging|testing|development" }, Matcher { op: NotEqual, name: "method", value: "GET" }] }, offset: Some(Pos(300s)), at: None })
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## PromQL compliance
|
|
31
|
+
|
|
32
|
+
This library declares compatible with [prometheus v2.45.0](https://github.com/prometheus/prometheus/tree/v2.45.0),
|
|
33
|
+
which isreleased at 2023-06-23. Any revision on PromQL after this commit is not guaranteed.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
from datetime import datetime, timedelta
|
|
37
|
+
from enum import Enum
|
|
38
|
+
from typing import Any, List, Optional, final
|
|
39
|
+
|
|
40
|
+
def parse(input: str) -> Expr:
|
|
41
|
+
"""Parse the given query literal to an AST."""
|
|
42
|
+
...
|
|
43
|
+
|
|
44
|
+
def parse_duration(duration: str) -> timedelta:
|
|
45
|
+
"""Parse a string into a duration.
|
|
46
|
+
|
|
47
|
+
Assumes that a year always has 365d, a week always has 7d,
|
|
48
|
+
and a day always has 24h.
|
|
49
|
+
|
|
50
|
+
Basic usage:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from datetime import timedelta
|
|
54
|
+
import promql_parser
|
|
55
|
+
|
|
56
|
+
assert promql_parser.parse_duration("1h") == timedelta(seconds=3600);
|
|
57
|
+
assert promql_parser.parse_duration("4d") == timedelta(seconds=3600 * 24 * 4)
|
|
58
|
+
assert promql_parser.parse_duration("4d1h") == timedelta(seconds=3600 * 97)
|
|
59
|
+
```
|
|
60
|
+
"""
|
|
61
|
+
...
|
|
62
|
+
|
|
63
|
+
def display_duration(delta: timedelta) -> str:
|
|
64
|
+
"""Display Duration in Prometheus format"""
|
|
65
|
+
...
|
|
66
|
+
|
|
67
|
+
class Expr:
|
|
68
|
+
@staticmethod
|
|
69
|
+
def parse(input: str) -> Any: ...
|
|
70
|
+
def prettify(self) -> str: ...
|
|
71
|
+
|
|
72
|
+
@final
|
|
73
|
+
class AggregateExpr(Expr):
|
|
74
|
+
"""An aggregation operation on a Vector.
|
|
75
|
+
|
|
76
|
+
Attributes:
|
|
77
|
+
op: The used aggregation operation.
|
|
78
|
+
expr: The Vector expression over which is aggregated.
|
|
79
|
+
param: Parameter used by some aggregators.
|
|
80
|
+
modifier: An optional modifier for some operations like sum.
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
op: TokenType
|
|
84
|
+
expr: Expr
|
|
85
|
+
param: Optional[Any]
|
|
86
|
+
modifier: Optional[AggModifier]
|
|
87
|
+
|
|
88
|
+
@final
|
|
89
|
+
class TokenType:
|
|
90
|
+
pass
|
|
91
|
+
|
|
92
|
+
@final
|
|
93
|
+
class AggModifier:
|
|
94
|
+
type: AggModifierType
|
|
95
|
+
labels: List[str]
|
|
96
|
+
|
|
97
|
+
@final
|
|
98
|
+
class AggModifierType(Enum):
|
|
99
|
+
By: Any
|
|
100
|
+
Without: Any
|
|
101
|
+
|
|
102
|
+
@final
|
|
103
|
+
class UnaryExpr(Expr):
|
|
104
|
+
"""An unary operation on another expression."""
|
|
105
|
+
|
|
106
|
+
expr: Expr
|
|
107
|
+
|
|
108
|
+
@final
|
|
109
|
+
class BinaryExpr(Expr):
|
|
110
|
+
"""A binary expression between two child expressions.
|
|
111
|
+
|
|
112
|
+
Attributes:
|
|
113
|
+
op: The operation of the expression.
|
|
114
|
+
lhs: The operands on the left side of the operator.
|
|
115
|
+
rhs: The operands on the right side of the operator.
|
|
116
|
+
modifier: An optional modifier.
|
|
117
|
+
"""
|
|
118
|
+
|
|
119
|
+
op: TokenType
|
|
120
|
+
lhs: Expr
|
|
121
|
+
rhs: Expr
|
|
122
|
+
modifier: Optional[BinModifier]
|
|
123
|
+
|
|
124
|
+
@final
|
|
125
|
+
class BinModifier:
|
|
126
|
+
"""Binary expression modifier
|
|
127
|
+
|
|
128
|
+
Attributes:
|
|
129
|
+
card:
|
|
130
|
+
The matching behavior for the operation if both operands are Vectors.
|
|
131
|
+
If they are not this field is None.
|
|
132
|
+
matching: on/ignoring on labels. Like a + b, no match modified is needed.
|
|
133
|
+
return_bool: If a comparison operator, return 0/1 rather than filtering.
|
|
134
|
+
"""
|
|
135
|
+
|
|
136
|
+
card: VectorMatchCardinality
|
|
137
|
+
matching: Optional[LabelModifier]
|
|
138
|
+
return_bool: bool
|
|
139
|
+
|
|
140
|
+
@final
|
|
141
|
+
class LabelModifier:
|
|
142
|
+
"""LabelModifier acts as
|
|
143
|
+
|
|
144
|
+
# Aggregation Modifier
|
|
145
|
+
|
|
146
|
+
- Exclude means `ignoring`
|
|
147
|
+
- Include means `on`
|
|
148
|
+
|
|
149
|
+
# Vector Match Modifier
|
|
150
|
+
|
|
151
|
+
- Exclude means `without` removes the listed labels from the result vector,
|
|
152
|
+
while all other labels are preserved in the output.
|
|
153
|
+
- Include means `by` does the opposite and drops labels that are not listed in the by clause,
|
|
154
|
+
even if their label values are identical between all elements of the vector.
|
|
155
|
+
|
|
156
|
+
If empty listed labels, meaning no grouping
|
|
157
|
+
"""
|
|
158
|
+
|
|
159
|
+
type: LabelModifierType
|
|
160
|
+
labels: List[str]
|
|
161
|
+
|
|
162
|
+
@final
|
|
163
|
+
class LabelModifierType(Enum):
|
|
164
|
+
Include: Any
|
|
165
|
+
Exclude: Any
|
|
166
|
+
|
|
167
|
+
@final
|
|
168
|
+
class VectorMatchCardinality(Enum):
|
|
169
|
+
"""The label list provided with the group_left or group_right modifier contains
|
|
170
|
+
additional labels from the "one"-side to be included in the result metrics."""
|
|
171
|
+
|
|
172
|
+
OneToOne: Any
|
|
173
|
+
ManyToOne: Any
|
|
174
|
+
OneToMany: Any
|
|
175
|
+
ManyToMany: Any
|
|
176
|
+
|
|
177
|
+
@final
|
|
178
|
+
class ParenExpr(Expr):
|
|
179
|
+
"""Wraps an expression so it cannot be disassembled as a consequence of operator precedence."""
|
|
180
|
+
|
|
181
|
+
expr: Expr
|
|
182
|
+
|
|
183
|
+
@final
|
|
184
|
+
class SubqueryExpr(Expr):
|
|
185
|
+
"""A subquery."""
|
|
186
|
+
|
|
187
|
+
expr: Expr
|
|
188
|
+
offset: Optional[timedelta]
|
|
189
|
+
at: Optional[AtModifier]
|
|
190
|
+
range: Optional[timedelta]
|
|
191
|
+
step: Optional[timedelta]
|
|
192
|
+
|
|
193
|
+
@final
|
|
194
|
+
class AtModifier:
|
|
195
|
+
type: AtModifierType
|
|
196
|
+
at: Optional[datetime]
|
|
197
|
+
|
|
198
|
+
@final
|
|
199
|
+
class AtModifierType(Enum):
|
|
200
|
+
Start: Any
|
|
201
|
+
End: Any
|
|
202
|
+
At: Any
|
|
203
|
+
|
|
204
|
+
@final
|
|
205
|
+
class NumberLiteral(Expr):
|
|
206
|
+
"""A number literal."""
|
|
207
|
+
|
|
208
|
+
val: float
|
|
209
|
+
|
|
210
|
+
@final
|
|
211
|
+
class StringLiteral(Expr):
|
|
212
|
+
"""A string literal."""
|
|
213
|
+
|
|
214
|
+
val: str
|
|
215
|
+
|
|
216
|
+
@final
|
|
217
|
+
class MatchOp(Enum):
|
|
218
|
+
Equal: Any
|
|
219
|
+
NotEqual: Any
|
|
220
|
+
Re: Any
|
|
221
|
+
NotRe: Any
|
|
222
|
+
|
|
223
|
+
@final
|
|
224
|
+
class Matcher:
|
|
225
|
+
op: MatchOp
|
|
226
|
+
name: str
|
|
227
|
+
value: str
|
|
228
|
+
|
|
229
|
+
@final
|
|
230
|
+
class Matchers:
|
|
231
|
+
matchers: List[Matcher]
|
|
232
|
+
or_matchers: List[List[Matcher]]
|
|
233
|
+
|
|
234
|
+
@final
|
|
235
|
+
class VectorSelector(Expr):
|
|
236
|
+
"""A Vector selection."""
|
|
237
|
+
|
|
238
|
+
name: Optional[str]
|
|
239
|
+
matchers: Matchers
|
|
240
|
+
offset: Optional[timedelta]
|
|
241
|
+
at: Optional[AtModifier]
|
|
242
|
+
|
|
243
|
+
@final
|
|
244
|
+
class MatrixSelector(Expr):
|
|
245
|
+
"""A Matrix selection."""
|
|
246
|
+
|
|
247
|
+
vector_selector: VectorSelector
|
|
248
|
+
range: timedelta
|
|
249
|
+
|
|
250
|
+
@final
|
|
251
|
+
class Call(Expr):
|
|
252
|
+
"""A call to a Prometheus function."""
|
|
253
|
+
|
|
254
|
+
func: Function
|
|
255
|
+
args: List[Any]
|
|
256
|
+
|
|
257
|
+
@final
|
|
258
|
+
class ValueType(Enum):
|
|
259
|
+
Vector: Any
|
|
260
|
+
Scalar: Any
|
|
261
|
+
Matrix: Any
|
|
262
|
+
String: Any
|
|
263
|
+
|
|
264
|
+
@final
|
|
265
|
+
class Function:
|
|
266
|
+
name: str
|
|
267
|
+
arg_types: List[ValueType]
|
|
268
|
+
variadic: bool
|
|
269
|
+
return_type: ValueType
|
|
Binary file
|
promql_parser/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: promql-parser
|
|
3
|
+
Version: 0.4.2
|
|
4
|
+
Classifier: Programming Language :: Rust
|
|
5
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
6
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Summary: PromQL Parser for Python
|
|
9
|
+
License: MIT
|
|
10
|
+
Requires-Python: >=3.8
|
|
11
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
12
|
+
Project-URL: Source Code, https://github.com/messense/py-promql-parser
|
|
13
|
+
|
|
14
|
+
# py-promql-parser
|
|
15
|
+
|
|
16
|
+

|
|
17
|
+
[](https://pypi.org/project/promql-parser)
|
|
18
|
+
|
|
19
|
+
[promql-parser](https://github.com/GreptimeTeam/promql-parser) Python binding, a PromQL parser for Python.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pip install promql-parser
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
import promql_parser
|
|
31
|
+
|
|
32
|
+
ast = promql_parser.parse('prometheus_http_requests_total{code="200", job="prometheus"}')
|
|
33
|
+
print(ast)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## License
|
|
37
|
+
|
|
38
|
+
This work is released under the MIT license. A copy of the license is provided in the [LICENSE](./LICENSE) file.
|
|
39
|
+
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
promql_parser-0.4.2.dist-info/METADATA,sha256=NR8Kd1_gdIbfvQOhY1CPs9o9VMMHQwaon49zWZhmWdE,1129
|
|
2
|
+
promql_parser-0.4.2.dist-info/WHEEL,sha256=futlocXTKIczW-hie_n6w--bAT-euhL-NVZjIAlXTFQ,96
|
|
3
|
+
promql_parser-0.4.2.dist-info/licenses/LICENSE,sha256=1oF4gRKKrXP17iJXIPlgRzmm8gKXEiRL_pi3hC1kqYw,1107
|
|
4
|
+
promql_parser/__init__.py,sha256=S_WJxolC7EN5itz4Z99Ml5faJ58UDXvlnVtiSvstGDM,135
|
|
5
|
+
promql_parser/__init__.pyi,sha256=-qwXr0MKSHDOEg3IA-prq9bJlz3jfBvWk4Njti7Sjhk,6530
|
|
6
|
+
promql_parser/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
promql_parser/promql_parser.cp313-win_amd64.pyd,sha256=IZ2xuS-P5sasud53JYyLZPyOABehKMbN_wVPIP4b9ws,2909184
|
|
8
|
+
promql_parser-0.4.2.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present Messense Lv
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
9
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
10
|
+
so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|