Documentation
¶
Overview ¶
Package state is a generated GoMock package.
Index ¶
- Constants
- Variables
- func FormatStateAndReason(state eval.State, reason string) string
- func FromAlertsStateToStoppedAlert(firingStates []StateTransition, appURL *url.URL, clock clock.Clock) apimodels.PostableAlerts
- func FromStateTransitionToPostableAlerts(firingStates []StateTransition, stateManager *Manager, appURL *url.URL) apimodels.PostableAlerts
- func GetRuleExtraLabels(l log.Logger, rule *models.AlertRule, folderTitle string, includeFolder bool) map[string]string
- func IsNormalStateWithNoReason(s *State) bool
- func NewEvaluationValues(m map[string]eval.NumberValueCapture) map[string]*float64
- func ParseFormattedState(stateStr string) (eval.State, string, error)
- func StateToPostableAlert(transition StateTransition, appURL *url.URL) *models.PostableAlert
- type AlertInstanceManager
- type AsyncStatePersister
- type Evaluation
- type FakeHistorian
- type FakeInstanceStore
- func (f *FakeInstanceStore) DeleteAlertInstances(ctx context.Context, q ...models.AlertInstanceKey) error
- func (f *FakeInstanceStore) DeleteAlertInstancesByRule(ctx context.Context, key models.AlertRuleKey) error
- func (f *FakeInstanceStore) FetchOrgIds(_ context.Context) ([]int64, error)
- func (f *FakeInstanceStore) FullSync(ctx context.Context, instances []models.AlertInstance) error
- func (f *FakeInstanceStore) ListAlertInstances(_ context.Context, q *models.ListAlertInstancesQuery) ([]*models.AlertInstance, error)
- func (f *FakeInstanceStore) RecordedOps() []any
- func (f *FakeInstanceStore) SaveAlertInstance(_ context.Context, q models.AlertInstance) error
- type FakeInstanceStoreOp
- type FakeRuleReader
- type Historian
- type ImageCapturer
- type InstanceStore
- type Manager
- func (st *Manager) DeleteStateByRuleUID(ctx context.Context, ruleKey ngModels.AlertRuleKey, reason string) []StateTransition
- func (st *Manager) Get(orgID int64, alertRuleUID, stateId string) *State
- func (st *Manager) GetAll(orgID int64) []*State
- func (st *Manager) GetStatesForRuleUID(orgID int64, alertRuleUID string) []*State
- func (st *Manager) ProcessEvalResults(ctx context.Context, evaluatedAt time.Time, alertRule *ngModels.AlertRule, ...) []StateTransition
- func (st *Manager) Put(states []*State)
- func (st *Manager) ResetStateByRuleUID(ctx context.Context, rule *ngModels.AlertRule, reason string) []StateTransition
- func (st *Manager) Run(ctx context.Context) error
- func (st *Manager) Warm(ctx context.Context, rulesReader RuleReader)
- type ManagerCfg
- type MockImageCapturer
- type MockImageCapturerMockRecorder
- type NoopImageService
- type NoopPersister
- type NotAvailableImageService
- type RuleReader
- type State
- func (a *State) Equals(b *State) bool
- func (a *State) GetAlertInstanceKey() (models.AlertInstanceKey, error)
- func (a *State) GetLabels(opts ...models.LabelOption) map[string]string
- func (a *State) GetLastEvaluationValuesForCondition() map[string]float64
- func (a *State) GetRuleKey() models.AlertRuleKey
- func (a *State) Maintain(interval int64, evaluatedAt time.Time)
- func (a *State) NeedsSending(resendDelay time.Duration) bool
- func (a *State) Resolve(reason string, endsAt time.Time)
- func (a *State) SetAlerting(reason string, startsAt, endsAt time.Time)
- func (a *State) SetError(err error, startsAt, endsAt time.Time)
- func (a *State) SetNoData(reason string, startsAt, endsAt time.Time)
- func (a *State) SetNormal(reason string, startsAt, endsAt time.Time)
- func (a *State) SetPending(reason string, startsAt, endsAt time.Time)
- type StatePersister
- type StateTransition
- type SyncStatePersister
Constants ¶
const ( NoDataAlertName = "DatasourceNoData" ErrorAlertName = "DatasourceError" Rulename = "rulename" )
Variables ¶
var (
ResendDelay = 30 * time.Second
)
Functions ¶
func FromAlertsStateToStoppedAlert ¶
func FromAlertsStateToStoppedAlert(firingStates []StateTransition, appURL *url.URL, clock clock.Clock) apimodels.PostableAlerts
FromAlertsStateToStoppedAlert selects only transitions from firing states (states eval.Alerting, eval.NoData, eval.Error) and converts them to models.PostableAlert with EndsAt set to time.Now
func FromStateTransitionToPostableAlerts ¶
func FromStateTransitionToPostableAlerts(firingStates []StateTransition, stateManager *Manager, appURL *url.URL) apimodels.PostableAlerts
func GetRuleExtraLabels ¶
func GetRuleExtraLabels(l log.Logger, rule *models.AlertRule, folderTitle string, includeFolder bool) map[string]string
GetRuleExtraLabels returns a map of built-in labels that should be added to an alert before it is sent to the Alertmanager or its state is cached.
func IsNormalStateWithNoReason ¶
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.
func ParseFormattedState ¶
ParseFormattedState parses a state string in the format "state (reason)" and returns the state and reason separately.
func StateToPostableAlert ¶
func StateToPostableAlert(transition StateTransition, appURL *url.URL) *models.PostableAlert
StateToPostableAlert converts a state to a model that is accepted by Alertmanager. Annotations and Labels are copied from the state. - if state has at least one result, a new label '__value_string__' is added to the label set - the alert's GeneratorURL is constructed to point to the alert detail view - if evaluation state is either NoData or Error, the resulting set of labels is changed:
- original alert name (label: model.AlertNameLabel) is backed up to OriginalAlertName
- label model.AlertNameLabel is overwritten to either NoDataAlertName or ErrorAlertName
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 AsyncStatePersister ¶
type AsyncStatePersister struct {
// contains filtered or unexported fields
}
func (*AsyncStatePersister) Async ¶
func (a *AsyncStatePersister) Async(ctx context.Context, cache *cache)
func (*AsyncStatePersister) Sync ¶
func (a *AsyncStatePersister) Sync(_ context.Context, _ trace.Span, _, _ []StateTransition)
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 {
// 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) FullSync ¶
func (f *FakeInstanceStore) FullSync(ctx context.Context, instances []models.AlertInstance) error
func (*FakeInstanceStore) ListAlertInstances ¶
func (f *FakeInstanceStore) ListAlertInstances(_ context.Context, q *models.ListAlertInstancesQuery) ([]*models.AlertInstance, error)
func (*FakeInstanceStore) RecordedOps ¶
func (f *FakeInstanceStore) RecordedOps() []any
func (*FakeInstanceStore) SaveAlertInstance ¶
func (f *FakeInstanceStore) SaveAlertInstance(_ context.Context, q models.AlertInstance) error
type FakeInstanceStoreOp ¶
type FakeRuleReader ¶
type FakeRuleReader struct{}
func (*FakeRuleReader) ListAlertRules ¶
func (f *FakeRuleReader) ListAlertRules(_ context.Context, q *models.ListAlertRulesQuery) (models.RulesGroup, error)
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) SaveAlertInstance(ctx context.Context, instance models.AlertInstance) error DeleteAlertInstances(ctx context.Context, keys ...models.AlertInstanceKey) error DeleteAlertInstancesByRule(ctx context.Context, key models.AlertRuleKey) error FullSync(ctx context.Context, instances []models.AlertInstance) error }
InstanceStore represents the ability to fetch and write alert instances.
type Manager ¶
func NewManager ¶
func NewManager(cfg ManagerCfg, statePersister StatePersister) *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) GetStatesForRuleUID ¶
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) 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.
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 // MaxStateSaveConcurrency controls the number of goroutines (per rule) that can save alert state in parallel. MaxStateSaveConcurrency int // ApplyNoDataAndErrorToAllStates makes state manager to apply exceptional results (NoData and Error) // to all states when corresponding execution in the rule definition is set to either `Alerting` or `OK` ApplyNoDataAndErrorToAllStates bool RulesPerRuleGroupLimit int64 DisableExecution bool Tracer tracing.Tracer Log log.Logger }
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 ¶
func (m *MockImageCapturer) EXPECT() *MockImageCapturerMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
type MockImageCapturerMockRecorder ¶
type MockImageCapturerMockRecorder struct {
// contains filtered or unexported fields
}
MockImageCapturerMockRecorder is the mock recorder for MockImageCapturer.
type NoopPersister ¶
type NoopPersister struct{}
func (*NoopPersister) Async ¶
func (n *NoopPersister) Async(_ context.Context, _ *cache)
func (*NoopPersister) Sync ¶
func (n *NoopPersister) Sync(_ context.Context, _ trace.Span, _, _ []StateTransition)
type NotAvailableImageService ¶
type NotAvailableImageService struct{}
NotAvailableImageService is a service that returns ErrScreenshotsUnavailable.
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 // ResultFingerprint is a hash of labels of the result before it is processed by ResultFingerprint data.Fingerprint // LatestResult contains the result of the most recent evaluation, if available. LatestResult *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) GetAlertInstanceKey ¶
func (a *State) GetAlertInstanceKey() (models.AlertInstanceKey, error)
func (*State) GetLastEvaluationValuesForCondition ¶
func (*State) GetRuleKey ¶
func (a *State) GetRuleKey() models.AlertRuleKey
func (*State) Resolve ¶
Resolve sets the State to Normal. It updates the StateReason, the end time, and sets Resolved to true.
func (*State) SetAlerting ¶
SetAlerting sets the state to Alerting. It changes both the start and end time.
func (*State) SetNoData ¶
SetNoData sets the state to NoData. It changes both the start and end time.
type StatePersister ¶
type StatePersister interface { Async(ctx context.Context, cache *cache) Sync(ctx context.Context, span trace.Span, states, staleStates []StateTransition) }
func NewAsyncStatePersister ¶
func NewAsyncStatePersister(log log.Logger, ticker *clock.Ticker, cfg ManagerCfg) StatePersister
func NewNoopPersister ¶
func NewNoopPersister() StatePersister
func NewSyncStatePersisiter ¶
func NewSyncStatePersisiter(log log.Logger, cfg ManagerCfg) StatePersister
type StateTransition ¶
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
type SyncStatePersister ¶
type SyncStatePersister struct {
// contains filtered or unexported fields
}
func (*SyncStatePersister) Async ¶
func (a *SyncStatePersister) Async(_ context.Context, _ *cache)
func (*SyncStatePersister) Sync ¶
func (a *SyncStatePersister) Sync(ctx context.Context, span trace.Span, states, staleStates []StateTransition)