state

package
v0.0.0-cloud Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2023 License: AGPL-3.0 Imports: 21 Imported by: 0

Documentation

Overview

Package state is a generated GoMock package.

Index

Constants

This section is empty.

Variables

View Source
var (
	ResendDelay           = 30 * time.Second
	MetricsScrapeInterval = 15 * time.Second // TODO: parameterize? // Setting to a reasonable default scrape interval for Prometheus.
)

Functions

func FormatStateAndReason

func FormatStateAndReason(state eval.State, reason string) string

func IsNormalStateWithNoReason

func IsNormalStateWithNoReason(s *State) bool

IsNormalStateWithNoReason returns true if the state is Normal and reason is empty

func NewEvaluationValues

func NewEvaluationValues(m map[string]eval.NumberValueCapture) map[string]*float64

NewEvaluationValues returns the labels and values for each RefID in the capture.

Types

type AlertInstanceManager

type AlertInstanceManager interface {
	GetAll(orgID int64) []*State
	GetStatesForRuleUID(orgID int64, alertRuleUID string) []*State
}

AlertInstanceManager defines the interface for querying the current alert instances.

type Evaluation

type Evaluation struct {
	EvaluationTime  time.Time
	EvaluationState eval.State
	// Values contains the RefID and value of reduce and math expressions.
	// Classic conditions can have different values for the same RefID as they can include multiple conditions.
	// For these, we use the index of the condition in addition RefID as the key e.g. "A0, A1, A2, etc.".
	Values map[string]*float64
	// Condition is the refID specified as the condition in the alerting rule at the time of the evaluation.
	Condition string
}

type FakeHistorian

type FakeHistorian struct {
	StateTransitions []StateTransition
}

func (*FakeHistorian) Record

func (f *FakeHistorian) Record(ctx context.Context, rule history_model.RuleMeta, states []StateTransition) <-chan error

type FakeInstanceStore

type FakeInstanceStore struct {
	RecordedOps []interface{}
	// contains filtered or unexported fields
}

func (*FakeInstanceStore) DeleteAlertInstances

func (f *FakeInstanceStore) DeleteAlertInstances(ctx context.Context, q ...models.AlertInstanceKey) error

func (*FakeInstanceStore) DeleteAlertInstancesByRule

func (f *FakeInstanceStore) DeleteAlertInstancesByRule(ctx context.Context, key models.AlertRuleKey) error

func (*FakeInstanceStore) FetchOrgIds

func (f *FakeInstanceStore) FetchOrgIds(_ context.Context) ([]int64, error)

func (*FakeInstanceStore) ListAlertInstances

func (*FakeInstanceStore) SaveAlertInstances

func (f *FakeInstanceStore) SaveAlertInstances(_ context.Context, q ...models.AlertInstance) error

type FakeInstanceStoreOp

type FakeInstanceStoreOp struct {
	Name string
	Args []interface{}
}

type FakeRuleReader

type FakeRuleReader struct{}

func (*FakeRuleReader) ListAlertRules

type Historian

type Historian interface {
	// RecordStates writes a number of state transitions for a given rule to state history. It returns a channel that
	// is closed when writing the state transitions has completed. If an error has occurred, the channel will contain a
	// non-nil error.
	Record(ctx context.Context, rule history_model.RuleMeta, states []StateTransition) <-chan error
}

Historian maintains an audit log of alert state history.

type ImageCapturer

type ImageCapturer interface {
	NewImage(ctx context.Context, r *models.AlertRule) (*models.Image, error)
}

ImageCapturer captures images.

type InstanceStore

type InstanceStore interface {
	FetchOrgIds(ctx context.Context) ([]int64, error)
	ListAlertInstances(ctx context.Context, cmd *models.ListAlertInstancesQuery) ([]*models.AlertInstance, error)
	SaveAlertInstances(ctx context.Context, cmd ...models.AlertInstance) error
	DeleteAlertInstances(ctx context.Context, keys ...models.AlertInstanceKey) error
	DeleteAlertInstancesByRule(ctx context.Context, key models.AlertRuleKey) error
}

InstanceStore represents the ability to fetch and write alert instances.

type Manager

type Manager struct {
	ResendDelay time.Duration
	// contains filtered or unexported fields
}

func NewManager

func NewManager(cfg ManagerCfg) *Manager

func (*Manager) DeleteStateByRuleUID

func (st *Manager) DeleteStateByRuleUID(ctx context.Context, ruleKey ngModels.AlertRuleKey, reason string) []StateTransition

DeleteStateByRuleUID removes the rule instances from cache and instanceStore. A closed channel is returned to be able to gracefully handle the clear state step in scheduler in case we do not need to use the historian to save state history.

func (*Manager) Get

func (st *Manager) Get(orgID int64, alertRuleUID, stateId string) *State

func (*Manager) GetAll

func (st *Manager) GetAll(orgID int64) []*State

func (*Manager) GetStatesForRuleUID

func (st *Manager) GetStatesForRuleUID(orgID int64, alertRuleUID string) []*State

func (*Manager) ProcessEvalResults

func (st *Manager) ProcessEvalResults(ctx context.Context, evaluatedAt time.Time, alertRule *ngModels.AlertRule, results eval.Results, extraLabels data.Labels) []StateTransition

ProcessEvalResults updates the current states that belong to a rule with the evaluation results. if extraLabels is not empty, those labels will be added to every state. The extraLabels take precedence over rule labels and result labels

func (*Manager) Put

func (st *Manager) Put(states []*State)

func (*Manager) ResetStateByRuleUID

func (st *Manager) ResetStateByRuleUID(ctx context.Context, rule *ngModels.AlertRule, reason string) []StateTransition

ResetStateByRuleUID removes the rule instances from cache and instanceStore and saves state history. If the state history has to be saved, rule must not be nil.

func (*Manager) Run

func (st *Manager) Run(ctx context.Context) error

func (*Manager) Warm

func (st *Manager) Warm(ctx context.Context, rulesReader RuleReader)

type ManagerCfg

type ManagerCfg struct {
	Metrics       *metrics.State
	ExternalURL   *url.URL
	InstanceStore InstanceStore
	Images        ImageCapturer
	Clock         clock.Clock
	Historian     Historian
	// DoNotSaveNormalState controls whether eval.Normal state is persisted to the database and returned by get methods
	DoNotSaveNormalState bool
}

type MockImageCapturer

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

MockImageCapturer is a mock of ImageCapturer interface.

func NewMockImageCapturer

func NewMockImageCapturer(ctrl *gomock.Controller) *MockImageCapturer

NewMockImageCapturer creates a new mock instance.

func (*MockImageCapturer) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockImageCapturer) NewImage

func (m *MockImageCapturer) NewImage(arg0 context.Context, arg1 *models.AlertRule) (*models.Image, error)

NewImage mocks base method.

type MockImageCapturerMockRecorder

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

MockImageCapturerMockRecorder is the mock recorder for MockImageCapturer.

func (*MockImageCapturerMockRecorder) NewImage

func (mr *MockImageCapturerMockRecorder) NewImage(arg0, arg1 interface{}) *gomock.Call

NewImage indicates an expected call of NewImage.

type NoopImageService

type NoopImageService struct{}

NoopImageService is a no-op image service.

func (*NoopImageService) NewImage

type NotAvailableImageService

type NotAvailableImageService struct{}

NotAvailableImageService is a service that returns ErrScreenshotsUnavailable.

func (*NotAvailableImageService) NewImage

type RuleReader

type RuleReader interface {
	ListAlertRules(ctx context.Context, query *models.ListAlertRulesQuery) (models.RulesGroup, error)
}

RuleReader represents the ability to fetch alert rules.

type State

type State struct {
	OrgID        int64
	AlertRuleUID string

	// CacheID is a unique, opaque identifier for the state, and is used to find the state
	// in the state cache. It tends to be derived from the state's labels.
	CacheID string

	// State represents the current state.
	State eval.State

	// StateReason is a textual description to explain why the state has its current state.
	StateReason string

	// Results contains the result of the current and previous evaluations.
	Results []Evaluation

	// Error is set if the current evaluation returned an error. If error is non-nil results
	// can still contain the results of previous evaluations.
	Error error

	// Resolved is set to true if this state is the transitional state between Firing and Normal.
	// All subsequent states will be false until the next transition from Firing to Normal.
	Resolved bool

	// Image contains an optional image for the state. It tends to be included in notifications
	// as a visualization to show why the alert fired.
	Image *models.Image

	// Annotations contains the annotations from the alert rule. If an annotation is templated
	// then the template is first evaluated to derive the final annotation.
	Annotations map[string]string

	// Labels contain the labels from the query and any custom labels from the alert rule.
	// If a label is templated then the template is first evaluated to derive the final label.
	Labels data.Labels

	// Values contains the values of any instant vectors, reduce and math expressions, or classic
	// conditions.
	Values map[string]float64

	StartsAt             time.Time
	EndsAt               time.Time
	LastSentAt           time.Time
	LastEvaluationString string
	LastEvaluationTime   time.Time
	EvaluationDuration   time.Duration
}

func (*State) Equals

func (a *State) Equals(b *State) bool

func (*State) GetAlertInstanceKey

func (a *State) GetAlertInstanceKey() (models.AlertInstanceKey, error)

func (*State) GetLabels

func (a *State) GetLabels(opts ...models.LabelOption) map[string]string

func (*State) GetLastEvaluationValuesForCondition

func (a *State) GetLastEvaluationValuesForCondition() map[string]float64

func (*State) GetRuleKey

func (a *State) GetRuleKey() models.AlertRuleKey

func (*State) Maintain

func (a *State) Maintain(interval int64, evaluatedAt time.Time)

Maintain updates the end time using the most recent evaluation.

func (*State) NeedsSending

func (a *State) NeedsSending(resendDelay time.Duration) bool

func (*State) Resolve

func (a *State) Resolve(reason string, endsAt time.Time)

Resolve sets the State to Normal. It updates the StateReason, the end time, and sets Resolved to true.

func (*State) SetAlerting

func (a *State) SetAlerting(reason string, startsAt, endsAt time.Time)

SetAlerting sets the state to Alerting. It changes both the start and end time.

func (*State) SetError

func (a *State) SetError(err error, startsAt, endsAt time.Time)

SetError sets the state to Error. It changes both the start and end time.

func (*State) SetNoData

func (a *State) SetNoData(reason string, startsAt, endsAt time.Time)

SetNoData sets the state to NoData. It changes both the start and end time.

func (*State) SetNormal

func (a *State) SetNormal(reason string, startsAt, endsAt time.Time)

SetNormal sets the state to Normal. It changes both the start and end time.

func (*State) SetPending

func (a *State) SetPending(reason string, startsAt, endsAt time.Time)

SetPending the state to Pending. It changes both the start and end time.

func (*State) TrimResults

func (a *State) TrimResults(alertRule *models.AlertRule)

type StateTransition

type StateTransition struct {
	*State
	PreviousState       eval.State
	PreviousStateReason string
}

StateTransition describes the transition from one state to another.

func (StateTransition) Changed

func (c StateTransition) Changed() bool

func (StateTransition) Formatted

func (c StateTransition) Formatted() string

func (StateTransition) PreviousFormatted

func (c StateTransition) PreviousFormatted() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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