check

package
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package check evaluates PromQL "contracts": lists of expressions that must each return a non-empty result. It is shared by the `promql-cli check` subcommand and the REPL's `.assert` command.

Index

Constants

This section is empty.

Variables

View Source
var Formats = []string{"text", "tap", "junit", "json"}

Formats lists the supported report formats.

Functions

func Write

func Write(w io.Writer, format string, rep Report, color bool) error

Write renders rep in the given format ("text" when empty). color only applies to the text format.

func WriteJSON

func WriteJSON(w io.Writer, rep Report) error

WriteJSON prints the report as a JSON document.

func WriteJUnit

func WriteJUnit(w io.Writer, rep Report) error

WriteJUnit prints the report as a single JUnit <testsuite>.

func WriteResult

func WriteResult(w io.Writer, r Result, color bool)

WriteResult prints one check as a text block: a status line, then, for failures and errors, the expression (when it differs from the name) and why.

func WriteTAP

func WriteTAP(w io.Writer, rep Report)

WriteTAP prints the report as TAP version 13; failures carry a YAML block.

func WriteText

func WriteText(w io.Writer, rep Report, color bool)

WriteText prints every result followed by a one-line summary.

Types

type Check

type Check struct {
	Name string `json:"name"` // preceding comment line, or the expression itself
	Expr string `json:"expr"`
	Line int    `json:"line"` // 1-based line in the contract file; 0 when ad hoc
}

Check is one expression of a contract.

func ParseContract

func ParseContract(r io.Reader) ([]Check, error)

ParseContract reads a contract: one PromQL expression per line, blank lines and '#' comment lines ignored. The '#' comment line immediately preceding an expression (no blank line in between) becomes its name; without one the name is the expression itself.

func ParseContractFile

func ParseContractFile(path string) ([]Check, error)

ParseContractFile is ParseContract on the file at path.

type Report

type Report struct {
	Contract    string
	EvaluatedAt time.Time
	Results     []Result
}

Report is the outcome of running a whole contract.

func (Report) Counts

func (r Report) Counts() (passed, failed, errored int)

Counts returns how many results passed, failed and errored.

func (Report) ExitCode

func (r Report) ExitCode() int

ExitCode maps the results to a process exit code: 2 when any check errored, else 1 when any failed, else 0.

type Result

type Result struct {
	Check
	Status Status `json:"status"`
	Detail string `json:"detail,omitempty"` // why a check failed or errored
}

Result is the outcome of running a Check.

type Runner

type Runner struct {
	Engine    *promql.Engine
	Queryable storage.Queryable
	// Parser must match the engine's parser options (experimental functions).
	// Nil means a parser with experimental functions enabled.
	Parser promparser.Parser
	At     time.Time
	// Timeout bounds each evaluation; zero means 30s.
	Timeout time.Duration
}

Runner evaluates checks against a queryable at a fixed instant.

func (*Runner) Assert

func (r *Runner) Assert(ctx context.Context, expr string) Result

Assert runs an ad hoc expression, named after itself.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, c Check) Result

Run evaluates one check: non-empty result passes, empty fails (with an explanation), parse and evaluation problems are errors.

func (*Runner) RunAll

func (r *Runner) RunAll(ctx context.Context, checks []Check) []Result

RunAll runs every check in order; a failing check never stops the others.

type Status

type Status string

Status is the outcome of a single check.

const (
	// StatusPass means the expression returned a non-empty result.
	StatusPass Status = "pass"
	// StatusFail means the expression evaluated but returned an empty result.
	StatusFail Status = "fail"
	// StatusError means the expression could not be parsed or evaluated.
	StatusError Status = "error"
)

Jump to

Keyboard shortcuts

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