Alerting: Delete protobuf alert rule state on alert rule deletion (#100736)

This commit is contained in:
Alexander Akhmetov 2025-02-14 16:56:14 +01:00 committed by GitHub
parent e343cb5ac9
commit cbae35c28b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 2 deletions

View File

@ -68,6 +68,13 @@ func (st DBstore) DeleteAlertRulesByUID(ctx context.Context, orgID int64, ruleUI
return err
}
logger.Debug("Deleted alert instances", "count", rows)
rows, err = sess.Table("alert_rule_state").Where("org_id = ?", orgID).In("rule_uid", ruleUID).Delete(alertRule{})
if err != nil {
return err
}
logger.Debug("Deleted alert rule state", "count", rows)
return nil
})
}

View File

@ -715,13 +715,22 @@ func TestIntegration_DeleteAlertRulesByUID(t *testing.T) {
sqlStore := db.InitTestDB(t)
cfg := setting.NewCfg()
folderService := setupFolderService(t, sqlStore, cfg, featuremgmt.WithFeatures())
b := &fakeBus{}
logger := log.New("test-dbstore")
store := createTestStore(sqlStore, folderService, logger, cfg.UnifiedAlerting, b)
store := createTestStore(sqlStore, folderService, logger, cfg.UnifiedAlerting, &fakeBus{})
protoInstanceStore := ProtoInstanceDBStore{
SQLStore: sqlStore,
Logger: logger,
FeatureToggles: featuremgmt.WithFeatures(),
}
gen := models.RuleGen
t.Run("should emit event when rules are deleted", func(t *testing.T) {
// Create a new store to pass the custom bus to check the signal
b := &fakeBus{}
logger := log.New("test-dbstore")
store := createTestStore(sqlStore, folderService, logger, cfg.UnifiedAlerting, b)
rule := createRule(t, store, gen)
called := false
b.publishFn = func(ctx context.Context, msg bus.Msg) error {
@ -737,6 +746,40 @@ func TestIntegration_DeleteAlertRulesByUID(t *testing.T) {
require.NoError(t, err)
require.True(t, called)
})
t.Run("should delete alert rule state", func(t *testing.T) {
rule := createRule(t, store, gen)
// Save state for the alert rule
instances := []models.AlertInstance{
{
AlertInstanceKey: models.AlertInstanceKey{
RuleUID: rule.UID,
RuleOrgID: rule.OrgID,
},
},
}
err := protoInstanceStore.SaveAlertInstancesForRule(context.Background(), rule.GetKeyWithGroup(), instances)
require.NoError(t, err)
savedInstances, err := protoInstanceStore.ListAlertInstances(context.Background(), &models.ListAlertInstancesQuery{
RuleUID: rule.UID,
RuleOrgID: rule.OrgID,
})
require.NoError(t, err)
require.Len(t, savedInstances, 1)
// Delete the rule
err = store.DeleteAlertRulesByUID(context.Background(), rule.OrgID, rule.UID)
require.NoError(t, err)
// Now there should be no alert rule state
savedInstances, err = protoInstanceStore.ListAlertInstances(context.Background(), &models.ListAlertInstancesQuery{
RuleUID: rule.UID,
RuleOrgID: rule.OrgID,
})
require.NoError(t, err)
require.Empty(t, savedInstances)
})
}
func TestIntegration_GetNamespaceByUID(t *testing.T) {