Skip to content

cortexproject/promqlsmith

Folders and files

NameName
Last commit message
Last commit date

Latest commit

cbb5738 · Feb 3, 2025
Jan 28, 2025
Nov 1, 2024
Apr 28, 2023
Nov 1, 2024
Mar 8, 2023
Mar 7, 2023
Apr 28, 2023
Mar 13, 2023
Nov 20, 2024
Nov 20, 2024
Nov 6, 2024
Nov 6, 2024
Nov 6, 2024
Mar 26, 2024
Feb 3, 2025
Jan 28, 2025

Repository files navigation

PromQLsmith

Description

A random query generator for PromQL. Its name is inspired by SQLsmith

Usage

PromQLsmith is a library that can be used in the test to generate PromQL queries. Example usage can be found under example.

package main

import (
	"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))
}