parser

package
v0.85.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = Options{
	Names:    model.UTF8Validation,
	Schema:   PrometheusSchema,
	IsStrict: false,
}
View Source
var ErrRuleCommentOnFile = errors.New("this comment is only valid when attached to a rule")
View Source
var PromQLParser = promParser.NewParser(promParser.Options{
	EnableExperimentalFunctions:  true,
	ExperimentalDurationExpr:     true,
	EnableExtendedRangeSelectors: true,
	EnableBinopFillModifiers:     true,
})

PromQLParser is a shared, goroutine-safe PromQL parser with all feature flags enabled. Each method creates its own internal parser state, so it is safe to call concurrently.

Functions

This section is empty.

Types

type AlertingRule

type AlertingRule struct {
	For           *YamlDuration
	KeepFiringFor *YamlDuration
	Labels        *YamlMap
	Annotations   *YamlMap
	Alert         YamlNode
	Expr          PromQLExpr
}

func (*AlertingRule) IsIdentical added in v0.50.0

func (ar *AlertingRule) IsIdentical(b *AlertingRule) bool

type ContentReader added in v0.72.0

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

func (*ContentReader) Read added in v0.72.0

func (r *ContentReader) Read(b []byte) (got int, err error)

type File added in v0.72.0

type File struct {
	Diagnostics []diags.Diagnostic
	Comments    []comments.Comment
	Groups      []Group
	Error       ParseError
	TotalLines  int
	IsRelaxed   bool
	IsIgnored   bool
}

type Group added in v0.72.0

type Group struct {
	Labels      *YamlMap
	Interval    *YamlDuration
	QueryOffset *YamlDuration
	Limit       *YamlInt
	Name        YamlNode
	Rules       []Rule
	Error       ParseError
}

type Options added in v0.80.0

type Options struct {
	Names    model.ValidationScheme
	Schema   Schema
	IsStrict bool
}

func (Options) WithStrict added in v0.80.0

func (o Options) WithStrict(strict bool) Options

type ParseError

type ParseError struct {
	Err         error
	Details     string
	Diagnostics []diags.Diagnostic
	Line        int
}

Use insread of StrictError.

func (ParseError) Error added in v0.61.0

func (pe ParseError) Error() string

type Parser

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

func NewParser

func NewParser(opts Options) Parser

func (Parser) Parse

func (p Parser) Parse(src io.Reader) (f File)

type PromQLExpr

type PromQLExpr struct {
	Value *YamlNode
	// contains filtered or unexported fields
}

func (*PromQLExpr) IsIdentical added in v0.50.0

func (pqle *PromQLExpr) IsIdentical(b PromQLExpr) bool

func (*PromQLExpr) Query

func (pn *PromQLExpr) Query() *PromQLNode

func (*PromQLExpr) Source added in v0.78.0

func (pn *PromQLExpr) Source() []*source.Source

func (*PromQLExpr) SyntaxError

func (pn *PromQLExpr) SyntaxError() error

type PromQLNode

type PromQLNode struct {
	Parent   *PromQLNode
	Expr     promParser.Node
	Children []*PromQLNode
}

PromQLNode is used to turn the parsed PromQL query expression into a tree. This allows us to walk the tree up & down and look for either parents or children of specific type. Which is useful if you, for example, want to check if all vector selectors are wrapped inside function calls etc.

func DecodeExpr added in v0.2.0

func DecodeExpr(expr string) (*PromQLNode, error)

func WalkDownExpr added in v0.56.0

func WalkDownExpr[T promParser.Node](node *PromQLNode) (nodes []*PromQLNode)

WalkDownExpr works just like WalkUpExpr but it walks the tree down, visiting all children. It also starts by checking the node passed to it before walking down the tree.

func WalkUpExpr added in v0.56.0

func WalkUpExpr[T promParser.Node](node *PromQLNode) (nodes []*PromQLNode)

WalkUpExpr allows to iterate a promQLNode node looking for parents of specific type. Prometheus parser returns interfaces which makes it more difficult to figure out what kind of node we're dealing with, hence this helper takes a type parameter it tries to cast. It starts by checking the node passed to it and then walks up by visiting all parent nodes.

func WalkUpParent added in v0.56.0

func WalkUpParent[T promParser.Node](node *PromQLNode) (nodes []*PromQLNode)

WalkUpParent works like WalkUpExpr but checks the parent (if present) instead of the node itself. It returns the nodes where the parent is of given type.

type RecordingRule

type RecordingRule struct {
	Labels *YamlMap
	Record YamlNode
	Expr   PromQLExpr
}

func (*RecordingRule) IsIdentical added in v0.50.0

func (rr *RecordingRule) IsIdentical(b *RecordingRule) bool

type Rule

type Rule struct {
	AlertingRule  *AlertingRule
	RecordingRule *RecordingRule
	Comments      []comments.Comment
	Error         ParseError
	Lines         diags.LineRange
}

func (Rule) Expr

func (r Rule) Expr() *PromQLExpr

func (Rule) IsIdentical added in v0.50.0

func (r Rule) IsIdentical(b Rule) bool

func (Rule) IsSame added in v0.27.0

func (r Rule) IsSame(nr Rule) bool

func (Rule) LastKey added in v0.71.0

func (r Rule) LastKey() (node *YamlNode)

func (Rule) Name added in v0.40.1

func (r Rule) Name() string

func (Rule) NameNode added in v0.71.1

func (r Rule) NameNode() YamlNode

func (Rule) Type added in v0.40.1

func (r Rule) Type() RuleType

type RuleType added in v0.40.1

type RuleType string
const (
	AlertingRuleType  RuleType = "alerting"
	RecordingRuleType RuleType = "recording"
	InvalidRuleType   RuleType = "invalid"
)

type Schema added in v0.69.0

type Schema uint8
const (
	PrometheusSchema Schema = iota
	ThanosSchema
)

type YamlDuration added in v0.80.0

type YamlDuration struct {
	ParseError error
	Raw        string
	Pos        diags.PositionRanges
	Value      time.Duration
}

func (*YamlDuration) IsIdentical added in v0.80.0

func (yd *YamlDuration) IsIdentical(b *YamlDuration) bool

type YamlInt added in v0.80.0

type YamlInt struct {
	ParseError error
	Raw        string
	Pos        diags.PositionRanges
	Value      int
}

type YamlKeyValue

type YamlKeyValue struct {
	Key   *YamlNode
	Value *YamlNode
}

type YamlMap

type YamlMap struct {
	Key   *YamlNode
	Items []*YamlKeyValue
}

func MergeMaps added in v0.72.0

func MergeMaps(a, b *YamlMap) *YamlMap

func (YamlMap) GetValue

func (ym YamlMap) GetValue(key string) *YamlNode

func (*YamlMap) IsIdentical added in v0.50.0

func (ym *YamlMap) IsIdentical(b *YamlMap) bool

func (YamlMap) Lines

func (ym YamlMap) Lines() (lr diags.LineRange)

type YamlNode

type YamlNode struct {
	Value string
	Pos   diags.PositionRanges
}

func (*YamlNode) IsIdentical added in v0.54.0

func (yn *YamlNode) IsIdentical(b *YamlNode) bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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