From 81710ca1f5e1fd870c19c65ec60aab5d237c811c Mon Sep 17 00:00:00 2001 From: Alexander Akhmetov Date: Tue, 16 Dec 2025 18:09:32 +0100 Subject: [PATCH] Alerting: Fix saving result_fingerprint in async alert state persister (#115005) --- pkg/services/ngalert/models/testing.go | 6 ++++ .../ngalert/store/instance_database.go | 7 ++-- .../ngalert/store/instance_database_test.go | 36 +++++++++++-------- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/pkg/services/ngalert/models/testing.go b/pkg/services/ngalert/models/testing.go index 886f13d029a..9542c35fce8 100644 --- a/pkg/services/ngalert/models/testing.go +++ b/pkg/services/ngalert/models/testing.go @@ -973,6 +973,12 @@ func (a AlertInstanceMutators) WithAnnotations(annotations InstanceAnnotations) } } +func (a AlertInstanceMutators) WithResultFingerprint(fp string) AlertInstanceMutator { + return func(i *AlertInstance) { + i.ResultFingerprint = fp + } +} + type Mutator[T any] func(*T) // CopyNotificationSettings creates a deep copy of NotificationSettings. diff --git a/pkg/services/ngalert/store/instance_database.go b/pkg/services/ngalert/store/instance_database.go index a9d57d1ab8f..cc385ef4c63 100644 --- a/pkg/services/ngalert/store/instance_database.go +++ b/pkg/services/ngalert/store/instance_database.go @@ -364,10 +364,10 @@ func (st InstanceDBStore) insertInstancesBatch(sess *sqlstore.DBSession, batch [ query := strings.Builder{} placeholders := make([]string, 0, len(batch)) - args := make([]any, 0, len(batch)*13) + args := make([]any, 0, len(batch)*14) query.WriteString("INSERT INTO alert_instance ") - query.WriteString("(rule_org_id, rule_uid, labels, labels_hash, current_state, current_reason, current_state_since, current_state_end, last_eval_time, fired_at, resolved_at, last_sent_at, annotations) VALUES ") + query.WriteString("(rule_org_id, rule_uid, labels, labels_hash, current_state, current_reason, current_state_since, current_state_end, last_eval_time, fired_at, resolved_at, last_sent_at, result_fingerprint, annotations) VALUES ") for _, instance := range batch { if err := models.ValidateAlertInstance(instance); err != nil { @@ -387,7 +387,7 @@ func (st InstanceDBStore) insertInstancesBatch(sess *sqlstore.DBSession, batch [ continue } - placeholders = append(placeholders, "(?,?,?,?,?,?,?,?,?,?,?,?,?)") + placeholders = append(placeholders, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)") args = append(args, instance.RuleOrgID, instance.RuleUID, @@ -401,6 +401,7 @@ func (st InstanceDBStore) insertInstancesBatch(sess *sqlstore.DBSession, batch [ nullableTimeToUnix(instance.FiredAt), nullableTimeToUnix(instance.ResolvedAt), nullableTimeToUnix(instance.LastSentAt), + instance.ResultFingerprint, annotationsJSON, ) } diff --git a/pkg/services/ngalert/store/instance_database_test.go b/pkg/services/ngalert/store/instance_database_test.go index 3cf264d4a36..4f833d0fd17 100644 --- a/pkg/services/ngalert/store/instance_database_test.go +++ b/pkg/services/ngalert/store/instance_database_test.go @@ -8,6 +8,8 @@ import ( "time" "github.com/golang/snappy" + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/grafana/grafana/pkg/util/testutil" "github.com/stretchr/testify/require" "google.golang.org/protobuf/proto" @@ -391,27 +393,33 @@ func TestIntegrationFullSync(t *testing.T) { t.Run("Should save all instances when batch size is bigger than 1", func(t *testing.T) { batchSize = 2 - newRuleUID := "y" - err := ng.InstanceStore.FullSync(ctx, append(instances, *models.AlertInstanceGen(models.InstanceMuts.WithOrgID(orgID), models.InstanceMuts.WithRuleUID(newRuleUID))), batchSize, nil) + testInstances := []models.AlertInstance{ + *models.AlertInstanceGen(models.InstanceMuts.WithOrgID(orgID), models.InstanceMuts.WithRuleUID("batch1"), models.InstanceMuts.WithResultFingerprint("fp0")), + *models.AlertInstanceGen(models.InstanceMuts.WithOrgID(orgID), models.InstanceMuts.WithRuleUID("batch2"), models.InstanceMuts.WithResultFingerprint("fp1")), + *models.AlertInstanceGen(models.InstanceMuts.WithOrgID(orgID), models.InstanceMuts.WithRuleUID("batch3"), models.InstanceMuts.WithResultFingerprint("fp2")), + } + + err := ng.InstanceStore.FullSync(ctx, testInstances, batchSize, nil) require.NoError(t, err) res, err := ng.InstanceStore.ListAlertInstances(ctx, &models.ListAlertInstancesQuery{ RuleOrgID: orgID, }) require.NoError(t, err) - require.Len(t, res, len(instances)+1) - for _, ruleUID := range append(ruleUIDs, newRuleUID) { - found := false - for _, instance := range res { - if instance.RuleUID == ruleUID { - found = true - continue - } - } - if !found { - t.Errorf("Instance with RuleUID '%s' not found", ruleUID) - } + + savedInstances := make([]models.AlertInstance, len(res)) + for i, r := range res { + savedInstances[i] = *r } + + opts := []cmp.Option{ + cmpopts.EquateApproxTime(time.Second), // we don't get the same precision back from the DB + cmpopts.EquateEmpty(), + cmpopts.SortSlices(func(a, b models.AlertInstance) bool { + return a.RuleUID < b.RuleUID + }), + } + require.Empty(t, cmp.Diff(testInstances, savedInstances, opts...)) }) t.Run("Should not fail when the instances are empty", func(t *testing.T) {