Alerting: Only warm alert state cache if execute_alerts=true. (#78895)

* Alerting: Only warm alert state cache if execute_alerts=true.

If the Grafana instance is not executing alerts, then Warm()-ing the state
manager is wasteful and could lead to misleading rule status queries, as the
status returned will be always based on the state loaded from the database at
startup, and not the most recent evaluation state.

* Move Warm() down to shared conditional.
pull/78492/head
Steve Simpson 2 years ago committed by GitHub
parent 8d7314bb9a
commit 520c927931
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      pkg/services/ngalert/ngalert.go

@ -352,9 +352,7 @@ func (ng *AlertNG) Run(ctx context.Context) error {
if !ng.shouldRun() {
return nil
}
ng.Log.Debug("Starting")
ng.stateManager.Warm(ctx, ng.store)
ng.Log.Debug("Starting", "execute_alerts", ng.Cfg.UnifiedAlerting.ExecuteAlerts)
children, subCtx := errgroup.WithContext(ctx)
@ -366,6 +364,17 @@ func (ng *AlertNG) Run(ctx context.Context) error {
})
if ng.Cfg.UnifiedAlerting.ExecuteAlerts {
// Only Warm() the state manager if we are actually executing alerts.
// Doing so when we are not executing alerts is wasteful and could lead
// to misleading rule status queries, as the status returned will be
// always based on the state loaded from the database at startup, and
// not the most recent evaluation state.
//
// Also note that this runs synchronously to ensure state is loaded
// before rule evaluation begins, hence we use ctx and not subCtx.
//
ng.stateManager.Warm(ctx, ng.store)
children.Go(func() error {
return ng.schedule.Run(subCtx)
})

Loading…
Cancel
Save