alerting

package
v0.3.11-8 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2024 License: Apache-2.0 Imports: 26 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrFrequencyCannotBeZeroOrLess frequency cannot be below zero
	ErrFrequencyCannotBeZeroOrLess = errors.Error(`"evaluate every" cannot be zero or below`)

	// ErrFrequencyCouldNotBeParsed frequency cannot be parsed
	ErrFrequencyCouldNotBeParsed = errors.Error(`"evaluate every" field could not be parsed`)
)

Functions

func GetConditionFactories

func GetConditionFactories() map[string]ConditionFactory

func NewAlertRuleTester

func NewAlertRuleTester() models.AlertTestRunner

func RegisterCondition

func RegisterCondition(typeName string, factory ConditionFactory)

RegisterCondition adds support for alerting conditions.

func RegisterNotifier

func RegisterNotifier(plug *NotifierPlugin)

Types

type AlertEngine

type AlertEngine struct {
	Scheduler scheduler
	// contains filtered or unexported fields
}

AlertEngine is the background process that schedules alert evaluations and makes sure notifications are sent.

func (*AlertEngine) Init

func (e *AlertEngine) Init() error

Init initalizes the AlertingService.

func (*AlertEngine) IsDisabled

func (e *AlertEngine) IsDisabled() bool

IsDisabled returns true if the alerting service is disabled for this instance.

func (*AlertEngine) Run

func (e *AlertEngine) Run(ctx context.Context) error

Run starts the alerting service background process.

type AlertRuleTester

type AlertRuleTester struct{}

func (AlertRuleTester) DoTest

type Condition

type Condition interface {
	Eval(result *EvalContext) (*ConditionResult, error)
}

Condition is responsible for evaluating an alert condition.

type ConditionFactory

type ConditionFactory func(model *monitor.AlertCondition, index int) (Condition, error)

ConditionFactory is the function signature for creating `Conditions`

type ConditionResult

type ConditionResult struct {
	Firing             bool
	NoDataFound        bool
	Operator           string
	EvalMatches        []*monitor.EvalMatch
	AlertOkEvalMatches []*monitor.EvalMatch
}

ConditionResult is the result of a condition evaluation.

type DefaultEvalHandler

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

DefaultEvalHandler is responsible for evaluating the alert rule.

func NewEvalHandler

func NewEvalHandler() *DefaultEvalHandler

NewEvalHandler is the `DefaultEvalHandler` constructor.

func (*DefaultEvalHandler) Eval

func (e *DefaultEvalHandler) Eval(context *EvalContext)

Eval evaluated the alert rule.

type EvalContext

type EvalContext struct {
	Firing             bool
	IsTestRun          bool
	IsDebug            bool
	EvalMatches        []*monitor.EvalMatch
	AlertOkEvalMatches []*monitor.EvalMatch
	Logs               []*monitor.ResultLogEntry
	Error              error
	ConditionEvals     string
	StartTime          time.Time
	EndTime            time.Time
	Rule               *Rule

	NoDataFound    bool
	PrevAlertState monitor.AlertStateType

	Ctx      context.Context
	UserCred mcclient.TokenCredential
}

EvalContext is the context object for an alert evaluation.

func NewEvalContext

func NewEvalContext(alertCtx context.Context, userCred mcclient.TokenCredential, rule *Rule) *EvalContext

NewEvalContext is the EvalContext constructor.

func (*EvalContext) GetCallbackURLPrefix

func (c *EvalContext) GetCallbackURLPrefix() string

func (*EvalContext) GetDurationMs

func (c *EvalContext) GetDurationMs() float64

GetDurationMs returns the duration of the alert evaluation.

func (*EvalContext) GetEvalMatches

func (c *EvalContext) GetEvalMatches() []monitor.EvalMatch

func (*EvalContext) GetNewState

func (c *EvalContext) GetNewState() monitor.AlertStateType

GetNewState returns the new state from the alert rule evaluation.

func (*EvalContext) GetNotificationTemplateConfig

func (c *EvalContext) GetNotificationTemplateConfig() monitor.NotificationTemplateConfig

func (*EvalContext) GetNotificationTitle

func (c *EvalContext) GetNotificationTitle() string

GetNotificationTitle returns the title of the alert rule including alert state.

func (*EvalContext) GetResourceNameOfMathes

func (c *EvalContext) GetResourceNameOfMathes(matches []monitor.EvalMatch) string

func (*EvalContext) GetRuleTitle

func (c *EvalContext) GetRuleTitle() string

func (*EvalContext) GetStateModel

func (c *EvalContext) GetStateModel() *StateDescription

GetStateModel returns the `StateDescription` based on current state.

func (*EvalContext) ToTestRunResult

func (ctx *EvalContext) ToTestRunResult() *monitor.AlertTestRunOutput

type Job

type Job struct {
	Offset     int64
	OffsetWait bool
	Delay      bool

	Rule *Rule
	// contains filtered or unexported fields
}

Job holds state about when the alert rule should be evaluated.

func (*Job) GetRunning

func (j *Job) GetRunning() bool

GetRunning returns true if the job is running. A lock is taken and released on the Job to ensure atomicity.

func (*Job) SetRunning

func (j *Job) SetRunning(b bool)

SetRunning sets the running property on the Job. A lock is taken and released on the Job to ensure atomicity.

type NotificationConfig

type NotificationConfig notifydrivers.NotificationConfig

type Notifier

type Notifier interface {
	notifydrivers.Notifier

	Notify(evalContext *EvalContext, params jsonutils.JSONObject) error

	// ShouldNotify checks this evaluation should send an alert notification
	ShouldNotify(ctx context.Context, evalContext *EvalContext, notificationState *models.SAlertnotification) bool
}

func InitNotifier

func InitNotifier(config NotificationConfig) (Notifier, error)

InitNotifier construct a new notifier

type NotifierFactory

type NotifierFactory func(config NotificationConfig) (Notifier, error)

NotifierFactory is a signature for creating notifiers

type NotifierPlugin

type NotifierPlugin struct {
	Type               string
	Factory            NotifierFactory
	ValidateCreateData func(cred mcclient.IIdentityProvider, input monitor.NotificationCreateInput) (monitor.NotificationCreateInput, error)
}

type Rule

type Rule struct {
	Id        string
	Frequency int64
	Title     string
	Name      string
	Message   string
	// 使用 TriggeredMessages 存储触发的条件,替代 Message
	TriggeredMessages   []string
	LastStateChange     time.Time
	For                 time.Duration
	NoDataState         monitor.NoDataOption
	ExecutionErrorState monitor.ExecutionErrorOption
	State               monitor.AlertStateType
	Conditions          []Condition
	Notifications       []string
	// AlertRuleTags       []*models.AlertRuleTag
	Level           string
	RuleDescription []*monitor.AlertRecordRule

	StateChanges int

	CustomizeConfig jsonutils.JSONObject
	// 静默期
	SilentPeriod int64
}

Rule is the in-memory version of an alert rule.

func NewRuleFromDBAlert

func NewRuleFromDBAlert(ruleDef *models.SAlert) (*Rule, error)

NewRuleFromDBAlert maps an db version of alert to an in-memory version

type StateDescription

type StateDescription struct {
	//Color string
	Text string
	Data string
}

SateDescription contains visual information about the alert state.

type Ticker

type Ticker struct {
	C chan time.Time
	// contains filtered or unexported fields
}

Ticker is a ticker to power the alerting scheduler. it's like a time.Ticker, except:

  • it doesn't drop ticks for slow receivers, rather, it queues up. so that callers are in control to instrument what's going on.
  • it automatically ticks every second, which is the right thing in our current design
  • it ticks on second marks or very shortly after. this provides a predictable load pattern (this shouldn't cause too much load contention issues because the next steps in the pipeline just process at their own pace)
  • the timestamps are used to mark "last datapoint to query for" and as such, are a configurable amount of seconds in the past
  • because we want to allow:
  • a clean "resume where we left off" and "don't yield ticks we already did"
  • adjusting offset over time to compensate for storage backing up or getting fast and providing lower latency you specify a lastProcessed timestamp as well as an offset at creation, or runtime

func NewTicker

func NewTicker(last time.Time, initialOffset time.Duration, c clock.Clock) *Ticker

NewTicker returns a ticker that ticks on second marks or very shortly after, and never drops ticks

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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