Alerting: Update state history service to filter states transitions (#58863)

* rename the method to better reflect its behavior
* make historian filter transition on itself
* call historian with all changes
This commit is contained in:
Yuri Tseretyan
2022-12-06 12:33:15 -05:00
committed by GitHub
parent 18d09cd3fe
commit a85adeed96
6 changed files with 120 additions and 25 deletions

View File

@@ -181,7 +181,10 @@ func (st *Manager) ProcessEvalResults(ctx context.Context, evaluatedAt time.Time
st.saveAlertStates(ctx, logger, states...)
st.logStateTransitions(ctx, alertRule, states, staleStates)
allChanges := append(states, staleStates...)
if st.historian != nil {
st.historian.RecordStatesAsync(ctx, alertRule, allChanges)
}
nextStates := make([]*State, 0, len(states))
for _, s := range states {
@@ -316,26 +319,6 @@ func (st *Manager) saveAlertStates(ctx context.Context, logger log.Logger, state
}
}
func (st *Manager) logStateTransitions(ctx context.Context, alertRule *ngModels.AlertRule, newStates, staleStates []StateTransition) {
if st.historian == nil {
return
}
changedStates := make([]StateTransition, 0, len(staleStates))
for _, s := range newStates {
if s.changed() {
changedStates = append(changedStates, s)
}
}
// TODO refactor further. Let historian decide what to log. Current logic removes states `Normal (reason-X) -> Normal (reason-Y)`
for _, t := range staleStates {
if t.PreviousState == eval.Alerting {
changedStates = append(changedStates, t)
}
}
st.historian.RecordStates(ctx, alertRule, changedStates)
}
func (st *Manager) deleteAlertStates(ctx context.Context, logger log.Logger, states []StateTransition) {
if st.instanceStore == nil || len(states) == 0 {
return