Alerting: Scheduled recording rules execute their queries (#88309)

* Basic eval flow

* Wiring-up

* fix

* Extend todo

* Start with tests

* Include some relevant tests, skip ones that seem to have timing-based race conditions

* Some tests, touch up linter and todo

* Solve TODO

* Add tracing

* Tests to make sure an eval went through

* Wire up feature toggles

* Update pkg/services/ngalert/schedule/recording_rule.go

Co-authored-by: Steve Simpson <steve.simpson@grafana.com>

* Update pkg/services/ngalert/schedule/recording_rule_test.go

Co-authored-by: Steve Simpson <steve.simpson@grafana.com>

* Update pkg/services/ngalert/schedule/recording_rule_test.go

Co-authored-by: Steve Simpson <steve.simpson@grafana.com>

* Update pkg/services/ngalert/schedule/recording_rule_test.go

Co-authored-by: Steve Simpson <steve.simpson@grafana.com>

---------

Co-authored-by: Steve Simpson <steve.simpson@grafana.com>
This commit is contained in:
Alexander Weaver
2024-05-28 10:59:21 -05:00
committed by GitHub
parent 910553df20
commit b926b6336d
8 changed files with 431 additions and 14 deletions

View File

@@ -11,6 +11,7 @@ import (
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/ngalert/eval"
"github.com/grafana/grafana/pkg/services/ngalert/metrics"
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
@@ -51,6 +52,7 @@ func newRuleFactory(
evalFactory eval.EvaluatorFactory,
ruleProvider ruleProvider,
clock clock.Clock,
featureToggles featuremgmt.FeatureToggles,
met *metrics.Scheduler,
logger log.Logger,
tracer tracing.Tracer,
@@ -59,7 +61,16 @@ func newRuleFactory(
) ruleFactoryFunc {
return func(ctx context.Context, rule *ngmodels.AlertRule) Rule {
if rule.IsRecordingRule() {
return newRecordingRule(ctx, logger)
return newRecordingRule(
ctx,
maxAttempts,
clock,
evalFactory,
featureToggles,
logger,
met,
tracer,
)
}
return newAlertRule(
ctx,
@@ -244,7 +255,7 @@ func (a *alertRule) Run(key ngmodels.AlertRuleKey) error {
for attempt := int64(1); attempt <= a.maxAttempts; attempt++ {
isPaused := ctx.rule.IsPaused
f := ruleWithFolder{ctx.rule, ctx.folderTitle}.Fingerprint()
f := ctx.Fingerprint()
// Do not clean up state if the eval loop has just started.
var needReset bool
if currentFingerprint != 0 && currentFingerprint != f {