https://github.com/facetoe/promval
PromQL validator written in Python
https://github.com/facetoe/promval
Last synced: about 1 month ago
JSON representation
PromQL validator written in Python
- Host: GitHub
- URL: https://github.com/facetoe/promval
- Owner: facetoe
- License: mit
- Created: 2022-01-22T13:57:29.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-06-13T01:38:07.000Z (over 2 years ago)
- Last Synced: 2025-07-28T07:43:31.994Z (4 months ago)
- Language: Python
- Size: 119 KB
- Stars: 4
- Watchers: 2
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- fucking-static-analysis - promval
- static-analysis - promval
README
# promval
`promval` is a PromQL validator written in Python.
It can be used to validate PromQL expressions are written as expected.
### Examples
#### Validate that we are grouping by certain metrics in by clauses
Here we validate that we are grouping by "job" and "host":
```python
promql = """
sum by (job) (
rate(http_requests_total[5m])
)
"""
validator = ByGroupValidator(expected={"job", "host"})
validator.validate(promql)
```
This fails with:
```
promval.error.ValidationError: 'line: 2:12 'by (job)' - missing required metric names {'host'}
```
#### Validate that labels containing a certain value are specified in by clause
Here we ensure that if we have a label with a particular value, it is specified in the by clause:
```python
expr = """
max by (require, not_required)(foo_metric{required=~'important', something='else'})
"""
validator = ByLabelValueValidator(label_value="important")
validator.validate(expr)
```
This fails with:
```
'line: 2:4 'by (require, not_required)' - expected label name 'required' in by clause
```
### Extending promval
It is easy to write custom validators, simply subclass `promval.validators.Validator` and implement a validating visitor. See examples in `promval.validators` for more information.