Alerting: Move rule evaluation status logic out of prometheus API and into scheduler (#89141)
* Add health fields to rules and an aggregator method to the scheduler * Move health, last error, and last eval time in together to minimize state processing * Wire up a readonly scheduler to prom api * Extract to exported function * Use health in api_prometheus and fix up tests * Rename health struct to status * Fix tests one more time * Several new tests * Handle inactive rules * Push state mapping into state manager * rename to StatusReader * Rectify cyclo complexity rebase * Convert existing package local status implementation to models one * fix tests * undo RuleDefs rename
This commit is contained in:
@@ -556,6 +556,11 @@ func (st *Manager) GetStatesForRuleUID(orgID int64, alertRuleUID string) []*Stat
|
||||
return st.cache.getStatesForRuleUID(orgID, alertRuleUID, st.doNotSaveNormalState)
|
||||
}
|
||||
|
||||
func (st *Manager) GetStatusForRuleUID(orgID int64, alertRuleUID string) ngModels.RuleStatus {
|
||||
states := st.GetStatesForRuleUID(orgID, alertRuleUID)
|
||||
return StatesToRuleStatus(states)
|
||||
}
|
||||
|
||||
func (st *Manager) Put(states []*State) {
|
||||
for _, s := range states {
|
||||
st.cache.set(s)
|
||||
@@ -623,3 +628,34 @@ func (st *Manager) deleteStaleStatesFromCache(ctx context.Context, logger log.Lo
|
||||
func stateIsStale(evaluatedAt time.Time, lastEval time.Time, intervalSeconds int64) bool {
|
||||
return !lastEval.Add(2 * time.Duration(intervalSeconds) * time.Second).After(evaluatedAt)
|
||||
}
|
||||
|
||||
func StatesToRuleStatus(states []*State) ngModels.RuleStatus {
|
||||
status := ngModels.RuleStatus{
|
||||
Health: "ok",
|
||||
LastError: nil,
|
||||
EvaluationTimestamp: time.Time{},
|
||||
}
|
||||
for _, state := range states {
|
||||
if state.LastEvaluationTime.After(status.EvaluationTimestamp) {
|
||||
status.EvaluationTimestamp = state.LastEvaluationTime
|
||||
}
|
||||
|
||||
status.EvaluationDuration = state.EvaluationDuration
|
||||
|
||||
switch state.State {
|
||||
case eval.Normal:
|
||||
case eval.Pending:
|
||||
case eval.Alerting:
|
||||
case eval.Error:
|
||||
status.Health = "error"
|
||||
case eval.NoData:
|
||||
status.Health = "nodata"
|
||||
}
|
||||
|
||||
if state.Error != nil {
|
||||
status.LastError = state.Error
|
||||
status.Health = "error"
|
||||
}
|
||||
}
|
||||
return status
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user