promqlsmith

package module
v0.0.0-...-226ab9c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2025 License: Apache-2.0 Imports: 10 Imported by: 1

README

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))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExprType

type ExprType int
const (
	VectorSelector ExprType = iota
	MatrixSelector
	AggregateExpr
	BinaryExpr
	SubQueryExpr
	CallExpr
	NumberLiteral
	UnaryExpr
)

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option specifies options when generating queries.

func WithAtModifierMaxTimestamp

func WithAtModifierMaxTimestamp(atModifierMaxTimestamp int64) Option

func WithEnableAtModifier

func WithEnableAtModifier(enableAtModifier bool) Option

func WithEnableExperimentalPromQLFunctions

func WithEnableExperimentalPromQLFunctions(enableExperimentalPromQLFunctions bool) Option

func WithEnableOffset

func WithEnableOffset(enableOffset bool) Option

func WithEnableVectorMatching

func WithEnableVectorMatching(enableVectorMatching bool) Option

func WithEnabledAggrs

func WithEnabledAggrs(enabledAggrs []parser.ItemType) Option

func WithEnabledBinOps

func WithEnabledBinOps(enabledBinops []parser.ItemType) Option

func WithEnabledExprs

func WithEnabledExprs(enabledExprs []ExprType) Option

func WithEnabledFunctions

func WithEnabledFunctions(enabledFunctions []*parser.Function) Option

func WithEnforceLabelMatchers

func WithEnforceLabelMatchers(matchers []*labels.Matcher) Option

func WithMaxDepth

func WithMaxDepth(depth int) Option

WithMaxDepth sets the maximum depth for generated query expressions

type PromQLSmith

type PromQLSmith struct {
	// contains filtered or unexported fields
}

func New

func New(rnd *rand.Rand, seriesSet []labels.Labels, opts ...Option) *PromQLSmith

New creates a PromQLsmith instance.

func (*PromQLSmith) Walk

func (s *PromQLSmith) Walk(valueTypes ...parser.ValueType) parser.Expr

Walk will walk the ast tree using one of the randomly generated expr type.

func (*PromQLSmith) WalkInstantQuery

func (s *PromQLSmith) WalkInstantQuery() parser.Expr

WalkInstantQuery walks the ast and generate an expression that can be used in instant query. Instant query also supports string literal, but we skip it here.

func (*PromQLSmith) WalkRangeQuery

func (s *PromQLSmith) WalkRangeQuery() parser.Expr

WalkRangeQuery walks the ast and generate an expression that can be used in range query.

func (*PromQLSmith) WalkSelectors

func (s *PromQLSmith) WalkSelectors() []*labels.Matcher

WalkSelectors generates random label matchers based on the input series labels.

Directories

Path Synopsis
example
demo Module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL