Alerting: Update saveAlertStates in state manager to not return results (#58279)

This commit is contained in:
Yuri Tseretyan 2022-11-07 09:09:19 -05:00 committed by GitHub
parent e5d4d00c1f
commit f9c88e72ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -179,10 +179,8 @@ func (st *Manager) ProcessEvalResults(ctx context.Context, evaluatedAt time.Time
processedResults[s.State.CacheID] = s.State processedResults[s.State.CacheID] = s.State
} }
resolvedStates := st.staleResultsHandler(ctx, evaluatedAt, alertRule, processedResults, logger) resolvedStates := st.staleResultsHandler(ctx, evaluatedAt, alertRule, processedResults, logger)
if len(states) > 0 && st.instanceStore != nil {
logger.Debug("Saving new states to the database", "count", len(states)) st.saveAlertStates(ctx, logger, states...)
_, _ = st.saveAlertStates(ctx, states...)
}
changedStates := make([]StateTransition, 0, len(states)) changedStates := make([]StateTransition, 0, len(states))
for _, s := range states { for _, s := range states {
@ -284,29 +282,19 @@ func (st *Manager) Put(states []*State) {
} }
// TODO: Is the `State` type necessary? Should it embed the instance? // TODO: Is the `State` type necessary? Should it embed the instance?
func (st *Manager) saveAlertStates(ctx context.Context, states ...StateTransition) (saved, failed int) { func (st *Manager) saveAlertStates(ctx context.Context, logger log.Logger, states ...StateTransition) {
logger := st.log.FromContext(ctx)
if st.instanceStore == nil { if st.instanceStore == nil {
return 0, 0 return
} }
logger.Debug("Saving alert states", "count", len(states)) logger.Debug("Saving alert states", "count", len(states))
instances := make([]ngModels.AlertInstance, 0, len(states)) instances := make([]ngModels.AlertInstance, 0, len(states))
type debugInfo struct {
OrgID int64
Uid string
State string
Labels string
}
debug := make([]debugInfo, 0)
for _, s := range states { for _, s := range states {
labels := ngModels.InstanceLabels(s.Labels) labels := ngModels.InstanceLabels(s.Labels)
_, hash, err := labels.StringAndHash() _, hash, err := labels.StringAndHash()
if err != nil { if err != nil {
debug = append(debug, debugInfo{s.OrgID, s.AlertRuleUID, s.State.State.String(), s.Labels.String()}) logger.Error("Failed to create a key for alert state to save it to database. The state will be ignored ", "cacheID", s.CacheID, "error", err)
logger.Error("Failed to save alert instance with invalid labels", "error", err)
continue continue
} }
fields := ngModels.AlertInstance{ fields := ngModels.AlertInstance{
@ -326,14 +314,16 @@ func (st *Manager) saveAlertStates(ctx context.Context, states ...StateTransitio
} }
if err := st.instanceStore.SaveAlertInstances(ctx, instances...); err != nil { if err := st.instanceStore.SaveAlertInstances(ctx, instances...); err != nil {
type debugInfo struct {
State string
Labels string
}
debug := make([]debugInfo, 0)
for _, inst := range instances { for _, inst := range instances {
debug = append(debug, debugInfo{inst.RuleOrgID, inst.RuleUID, string(inst.CurrentState), data.Labels(inst.Labels).String()}) debug = append(debug, debugInfo{string(inst.CurrentState), data.Labels(inst.Labels).String()})
} }
logger.Error("Failed to save alert states", "states", debug, "error", err) logger.Error("Failed to save alert states", "states", debug, "error", err)
return 0, len(debug)
} }
return len(instances), len(debug)
} }
// TODO: why wouldn't you allow other types like NoData or Error? // TODO: why wouldn't you allow other types like NoData or Error?