eval

package
v11.1.4-modfix Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2024 License: AGPL-3.0 Imports: 21 Imported by: 0

Documentation

Overview

Package eval executes the condition for an alert definition, evaluates the condition results, and returns the alert instance states.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResultGen

func ResultGen(mutators ...ResultMutator) func() Result

Types

type AlertingResultsReader

type AlertingResultsReader interface {
	Read() map[data.Fingerprint]struct{}
}

AlertingResultsReader provides fingerprints of results that are in alerting state. It is used during the evaluation of queries.

type ConditionEvaluator

type ConditionEvaluator interface {
	// EvaluateRaw evaluates the condition and returns raw backend response backend.QueryDataResponse
	EvaluateRaw(ctx context.Context, now time.Time) (resp *backend.QueryDataResponse, err error)
	// Evaluate evaluates the condition and converts the response to Results
	Evaluate(ctx context.Context, now time.Time) (Results, error)
}

type EvaluationContext

type EvaluationContext struct {
	Ctx                   context.Context
	User                  identity.Requester
	AlertingResultsReader AlertingResultsReader
}

EvaluationContext represents the context in which a condition is evaluated.

func NewContext

func NewContext(ctx context.Context, user identity.Requester) EvaluationContext

func NewContextWithPreviousResults

func NewContextWithPreviousResults(ctx context.Context, user identity.Requester, reader AlertingResultsReader) EvaluationContext

type EvaluatorFactory

type EvaluatorFactory interface {
	// Validate validates that the condition is correct. Returns nil if the condition is correct. Otherwise, error that describes the failure
	Validate(ctx EvaluationContext, condition models.Condition) error
	// Create builds an evaluator pipeline ready to evaluate a rule's query
	Create(ctx EvaluationContext, condition models.Condition) (ConditionEvaluator, error)
}

func NewEvaluatorFactory

func NewEvaluatorFactory(
	cfg setting.UnifiedAlertingSettings,
	datasourceCache datasources.CacheService,
	expressionService *expr.Service,
	pluginsStore pluginstore.Store,
) EvaluatorFactory

type ExecutionResults

type ExecutionResults struct {
	// Condition contains the results of the condition
	Condition data.Frames

	// Results contains the results of all queries, reduce and math expressions
	Results map[string]data.Frames

	// Errors contains a map of RefIDs that returned an error
	Errors map[string]error

	// NoData contains the DatasourceUID for RefIDs that returned no data.
	NoData map[string]string

	Error error
}

ExecutionResults contains the unevaluated results from executing a condition.

type FakeLoadedMetricsReader

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

func (FakeLoadedMetricsReader) Read

func (f FakeLoadedMetricsReader) Read() map[data.Fingerprint]struct{}

type NumberValueCapture

type NumberValueCapture struct {
	Var    string // RefID
	Labels data.Labels

	Value *float64
}

type Result

type Result struct {
	Instance data.Labels
	State    State // Enum

	// Error message for Error state. should be nil if State != Error.
	Error error

	// Results contains the results of all queries, reduce and math expressions
	Results map[string]data.Frames

	// Values contains the labels and values for all Threshold, Reduce and Math expressions,
	// and all conditions of a Classic Condition that are firing. Threshold, Reduce and Math
	// expressions are indexed by their Ref ID, while conditions in a Classic Condition are
	// indexed by their Ref ID and the index of the condition. For example, B0, B1, etc.
	Values map[string]NumberValueCapture

	EvaluatedAt        time.Time
	EvaluationDuration time.Duration
	// EvaluationString is a string representation of evaluation data such
	// as EvalMatches (from "classic condition"), and in the future from operations
	// like SSE "math".
	EvaluationString string
}

Result contains the evaluated State of an alert instance identified by its labels.

func NewResultFromError

func NewResultFromError(err error, evaluatedAt time.Time, duration time.Duration) Result

type ResultMutator

type ResultMutator func(r *Result)

func WithError

func WithError(err error) ResultMutator

func WithEvaluatedAt

func WithEvaluatedAt(time time.Time) ResultMutator

func WithLabels

func WithLabels(labels data.Labels) ResultMutator

func WithState

func WithState(state State) ResultMutator

type Results

type Results []Result

Results is a slice of evaluated alert instances states.

func EvaluateAlert

func EvaluateAlert(queryResponse *backend.QueryDataResponse, condition models.Condition, now time.Time) Results

EvaluateAlert takes the results of an executed query and evaluates it as an alert rule, returning alert states that the query produces.

func GenerateResults

func GenerateResults(count int, generator func() Result) Results

func (Results) AsDataFrame

func (evalResults Results) AsDataFrame() data.Frame

AsDataFrame forms the EvalResults in Frame suitable for displaying in the table panel of the front end. It displays one row per alert instance, with a column for each label and one for the alerting state.

func (Results) Error

func (evalResults Results) Error() error

Error returns the aggregated `error` of all results of which state is `Error`.

func (Results) HasErrors

func (evalResults Results) HasErrors() bool

HasErrors returns true when Results contains at least one element with error

func (Results) HasNonRetryableErrors

func (evalResults Results) HasNonRetryableErrors() bool

HasNonRetryableErrors returns true if we have at least 1 result with: 1. A `State` of `Error` 2. The `Error` attribute is not nil 3. The `Error` type is of `&invalidEvalResultFormatError` or `ErrSeriesMustBeWide` Our thinking with this approach, is that we don't want to retry errors that have relation with invalid alert definition format.

func (Results) IsError

func (evalResults Results) IsError() bool

HasErrors returns true when Results contains at least one element and all elements are errors

func (Results) IsNoData

func (evalResults Results) IsNoData() bool

IsNoData returns true when all items are NoData or Results is empty

type State

type State int

State is an enum of the evaluation State for an alert instance.

const (
	// Normal is the eval state for an alert instance condition
	// that evaluated to false.
	Normal State = iota

	// Alerting is the eval state for an alert instance condition
	// that evaluated to true (Alerting).
	Alerting

	// Pending is the eval state for an alert instance condition
	// that evaluated to true (Alerting) but has not yet met
	// the For duration defined in AlertRule.
	Pending

	// NoData is the eval state for an alert rule condition
	// that evaluated to NoData.
	NoData

	// Error is the eval state for an alert rule condition
	// that evaluated to Error.
	Error
)

func ParseStateString

func ParseStateString(repr string) (State, error)

func RandomState

func RandomState() State

func (State) IsValid

func (s State) IsValid() bool

func (State) String

func (s State) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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