cases

package
v0.0.0-...-12cbdf9 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2022 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ResendDelay = time.Minute

	// MaxRTT is the max request time for alert-generator sending the alert or making GET requests to the API.
	MaxRTT = 5 * time.Second
)

Variables

View Source
var AllCasesMap = map[string]TestCase{
	"PendingAndFiringAndResolved":       PendingAndFiringAndResolved(),
	"PendingAndResolved_AlwaysInactive": PendingAndResolved_AlwaysInactive(),
	"ZeroFor_SmallFor":                  ZeroFor_SmallFor(),
	"NewAlerts_OrderCheck":              NewAlerts_OrderCheck(),
}

AllCasesMap contains all the usable test cases in this package. It is recommended to keep the name of rule group same as the corresponding function calls for easy debugging.

Functions

This section is empty.

Types

type ExpectedAlert

type ExpectedAlert struct {
	// OrderingID is the number used to sort the slice of expected alerts for a given label set of an alert.
	OrderingID int

	// TimeTolerance is the tolerance to be considered when
	// comparing the time of the alert receiving and alert payload fields.
	// This is usually the group interval.
	// TODO: have some additional tolerance on the http request delay on top of group interval.
	TimeTolerance time.Duration

	// This alert should come at Ts time.
	Ts time.Time

	// NextState is the time when the next state of this alert is expected.
	// time.Time{} if the state won't change from here.
	// This is used to check if this alert can be ignored.
	NextState time.Time

	// If it is a Resolved alert, Resolved must be set to true.
	Resolved bool

	// Resend is true if this alert was a resend of the earlier alert with same labels.
	Resend bool

	// ResolvedTime is the time when the alert becomes Resolved. time.Unix(0,0) if never Resolved.
	// This is also the EndsAt of the alert when the alert is Resolved.
	ResolvedTime time.Time

	// EndsAtDelta is the duration w.r.t. the alert reception time when the EndsAt must be set.
	// This is only for pending and firing alerts.
	// It is usually 4*resendDelay or 4*groupInterval, whichever is higher.
	EndsAtDelta time.Duration

	// This is the expected alert.
	Alert *notifier.Alert
}

ExpectedAlert describes the characteristics of a receiving alert. The alert is considered as "may or may not come" (hence no error if not received) in these scenarios:

  1. (Ts + TimeTolerance) crosses the ResolvedTime time when Resolved is false. Because it can get resolved during the tolerance period.
  2. (Ts + TimeTolerance) crosses ResolvedTime+15m when Resolved is true.

func (*ExpectedAlert) CanBeIgnored

func (ea *ExpectedAlert) CanBeIgnored() bool

CanBeIgnored tells if the alert can be ignored. It can be ignored in the following cases: 1. It is a firing alert but it gets into "inactive" state within the tolerance time. 2. It is a resolved alert but it was resolved more than 15m ago. 3. The alert goes into the next state within the tolerance time.

func (*ExpectedAlert) Matches

func (ea *ExpectedAlert) Matches(now time.Time, a notifier.Alert) (err error)

Matches tells if the given alert satisfies the expected alert description.

func (*ExpectedAlert) ShouldBeIgnored

func (ea *ExpectedAlert) ShouldBeIgnored() bool

ShouldBeIgnored tells if the alert should be ignored. It is ignored in the following cases: 1. It is a resolved alert and Ts has crossed ResolvedTime+15m. 2. Ts has crossed the next state time.

type TestCase

type TestCase interface {
	// Describe returns the test case's rule group name and description.
	// NOTE: The group name must be unique across all the test cases.
	Describe() (groupName string, description string)

	// RuleGroup returns the alerting rule group that this test case is testing.
	// NOTE: All the rules in the group must include a label `rulegroup="<groupName>"`
	//       which should also be attached to the resultant alerts.
	RuleGroup() (rulefmt.RuleGroup, error)

	// SamplesToRemoteWrite gives all the samples that needs to be remote-written at their
	// respective timestamps. The last sample indicates the end of this test case hence appropriate
	// additional samples must be considered beforehand.
	//
	// All the timestamps returned here are in milliseconds and starts at 0,
	// which is the time when the test suite would start the test.
	// The test suite is responsible for translating these 0 based
	// timestamp to the relevant timestamps for the current time.
	//
	// The samples must be delivered to the remote storage after the timestamp specified on the samples
	// and must be delivered within 10 seconds of that timestamp.
	SamplesToRemoteWrite() []prompb.TimeSeries

	// Init tells the test case the actual timestamp for the 0 time.
	Init(zeroTime int64)

	// TestUntil returns a unix timestamp upto which the test must be running on this TestCase.
	// This must be called after Init() and the returned timestamp refers to absolute unix
	// timestamp (i.e. not relative like the SamplesToRemoteWrite())
	TestUntil() int64

	// CheckAlerts returns nil if the alerts provided are as expected at the given timestamp.
	// Returns an error otherwise describing what is the problem.
	// This must be checked with a min interval of the rule group's interval from RuleGroup().
	CheckAlerts(ts int64, alerts []v1.Alert) error

	// CheckRuleGroup returns nil if the rule group provided is as expected at the given timestamp.
	// Returns an error otherwise describing what is the problem.
	// This must be checked with a min interval of the rule group's interval from RuleGroup().
	CheckRuleGroup(ts int64, rg *v1.RuleGroup) error

	// CheckMetrics returns nil if at give timestamp the metrics contain the expected metrics.
	// Returns an error otherwise describing what is the problem.
	// This must be checked with a min interval of the rule group's interval from RuleGroup().
	CheckMetrics(ts int64, metrics []promql.Sample) error

	// ExpectedAlerts returns all the expected alerts that must be received for this test case.
	// This must be called only after Init().
	ExpectedAlerts() []ExpectedAlert
}

TestCase defines a single test case for the alert generator.

func AllCases

func AllCases() []TestCase

func NewAlerts_OrderCheck

func NewAlerts_OrderCheck() TestCase

NewAlerts_OrderCheck tests the following cases: * Rule that produces new alerts that go from pending->firing->inactive while already having active alerts. * A rule group having rules which are dependent on the ALERTS series from the rules above it in the same group. * Expansion of template in annotations only use the labels from the query result as source data even if those labels get overridden by the rules. They do not use the rules' additional labels.

func PendingAndFiringAndResolved

func PendingAndFiringAndResolved() TestCase

PendingAndFiringAndResolved tests the following cases: * Alert that goes from pending->firing->inactive. * pending alerts having changing annotation values (checked via API). * firing and inactive alerts being sent when they first went into those states. * firing alert being re-sent at expected intervals when the alert is active with changing annotation contents. * inactive alert being re-sent at expected intervals up to a certain time and not after that. * Alert that becomes active after having fired already and gone into inactive state where 'for' duration is non zero where inactive alert was still being sent.

func PendingAndResolved_AlwaysInactive

func PendingAndResolved_AlwaysInactive() TestCase

PendingAndResolved_AlwaysInactive tests the following cases: * Alert that goes from pending->inactive. * Rule that never becomes active (i.e. alerts in pending or firing). * Alert goes into inactive when there is no more data in pending.

func ZeroFor_SmallFor

func ZeroFor_SmallFor() TestCase

ZeroFor_SmallFor tests the following cases:

  • Alert that goes directly to firing state (skipping the pending state) because of zero for duration.
  • When the for duration is non-zero and less than the evaluation interval, firing alert must be sent after the second evaluation of the rule and not before.
  • Alert that becomes active after having fired already and gone into inactive state where for duration is zero and the inactive alert was not being sent anymore.
  • Alert goes into inactive when there is no more data when in firing.

Jump to

Keyboard shortcuts

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