https://github.com/cortexproject/promqlsmith
A random PromQL query generator
https://github.com/cortexproject/promqlsmith
fuzz-testing prometheus promql
Last synced: 6 months ago
JSON representation
A random PromQL query generator
- Host: GitHub
- URL: https://github.com/cortexproject/promqlsmith
- Owner: cortexproject
- License: apache-2.0
- Created: 2023-03-07T03:07:33.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-01T16:57:54.000Z (6 months ago)
- Last Synced: 2024-11-01T17:27:16.909Z (6 months ago)
- Topics: fuzz-testing, prometheus, promql
- Language: Go
- Homepage:
- Size: 308 KB
- Stars: 19
- Watchers: 5
- Forks: 4
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PromQLsmith
## Description
A random query generator for PromQL. Its name is inspired by [SQLsmith](https://github.com/anse1/sqlsmith)
## Usage
PromQLsmith is a library that can be used in the test to generate PromQL queries. Example usage can be found under [example](example).
```go
package mainimport (
"fmt"
"math/rand"
"time""github.com/prometheus/prometheus/model/labels"
"github.com/cortexproject/promqlsmith"
)func main() {
seriesSet := []labels.Labels{
labels.FromMap(map[string]string{
labels.MetricName: "http_requests_total",
"job": "prometheus",
"status_code": "200",
}),
labels.FromMap(map[string]string{
labels.MetricName: "http_requests_total",
"job": "prometheus",
"status_code": "400",
}),
}rnd := rand.New(rand.NewSource(time.Now().Unix()))
opts := []promqlsmith.Option{
promqlsmith.WithEnableOffset(true),
promqlsmith.WithEnableAtModifier(true),
}
ps := promqlsmith.New(rnd, seriesSet, opts...)
// Generate a query that can be used in instant query.
q1 := ps.WalkInstantQuery()
// Generate a query that can be used in range query.
q2 := ps.WalkRangeQuery()
fmt.Println(q1.Pretty(0))
fmt.Println(q2.Pretty(2))
}
```