Alerting: Add delete reason to DeleteAlertInstancesByRule

alexander-akhmetov/delete-reason
Alexander Akhmetov 9 months ago
parent 228bf711f9
commit db49d89d19
No known key found for this signature in database
GPG Key ID: A5A8947133B1B31B
  1. 14
      pkg/services/ngalert/state/manager.go
  2. 11
      pkg/services/ngalert/state/persist.go
  3. 2
      pkg/services/ngalert/state/testing.go
  4. 3
      pkg/services/ngalert/store/instance_database.go

@ -267,7 +267,19 @@ func (st *Manager) DeleteStateByRuleUID(ctx context.Context, ruleKey ngModels.Al
}
if st.instanceStore != nil {
err := st.instanceStore.DeleteAlertInstancesByRule(ctx, ruleKey)
var deleteReason AlertInstancesDeleteReason
switch reason {
case ngModels.StateReasonRuleDeleted:
deleteReason = AlertInstancesDeleteReasonRuleDeleted
case ngModels.StateReasonUpdated:
deleteReason = AlertInstancesDeleteReasonRuleUpdated
case ngModels.StateReasonPaused:
deleteReason = AlertInstancesDeleteReasonRulePaused
default:
deleteReason = AlertInstancesDeleteReasonUnknown
}
err := st.instanceStore.DeleteAlertInstancesByRule(ctx, ruleKey, deleteReason)
if err != nil {
logger.Error("Failed to delete states that belong to a rule from database", "error", err)
}

@ -7,6 +7,15 @@ import (
history_model "github.com/grafana/grafana/pkg/services/ngalert/state/historian/model"
)
type AlertInstancesDeleteReason string
const (
AlertInstancesDeleteReasonRuleDeleted AlertInstancesDeleteReason = "rule_deleted"
AlertInstancesDeleteReasonRuleUpdated AlertInstancesDeleteReason = "rule_updated"
AlertInstancesDeleteReasonRulePaused AlertInstancesDeleteReason = "rule_paused"
AlertInstancesDeleteReasonUnknown AlertInstancesDeleteReason = "unknown"
)
// InstanceStore represents the ability to fetch and write alert instances.
type InstanceStore interface {
InstanceReader
@ -15,7 +24,7 @@ type InstanceStore interface {
DeleteAlertInstances(ctx context.Context, keys ...models.AlertInstanceKey) error
// SaveAlertInstancesForRule overwrites the state for the given rule.
SaveAlertInstancesForRule(ctx context.Context, key models.AlertRuleKeyWithGroup, instances []models.AlertInstance) error
DeleteAlertInstancesByRule(ctx context.Context, key models.AlertRuleKeyWithGroup) error
DeleteAlertInstancesByRule(ctx context.Context, key models.AlertRuleKeyWithGroup, reason AlertInstancesDeleteReason) error
FullSync(ctx context.Context, instances []models.AlertInstance) error
}

@ -60,7 +60,7 @@ func (f *FakeInstanceStore) SaveAlertInstancesForRule(ctx context.Context, key m
return nil
}
func (f *FakeInstanceStore) DeleteAlertInstancesByRule(ctx context.Context, key models.AlertRuleKeyWithGroup) error {
func (f *FakeInstanceStore) DeleteAlertInstancesByRule(ctx context.Context, key models.AlertRuleKeyWithGroup, reason AlertInstancesDeleteReason) error {
return nil
}

@ -11,6 +11,7 @@ import (
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/services/ngalert/state"
"github.com/grafana/grafana/pkg/services/sqlstore"
)
@ -217,7 +218,7 @@ func (st DBstore) SaveAlertInstancesForRule(ctx context.Context, key models.Aler
return errors.New("method SaveAlertInstancesForRule is not implemented for instance database store")
}
func (st DBstore) DeleteAlertInstancesByRule(ctx context.Context, key models.AlertRuleKeyWithGroup) error {
func (st DBstore) DeleteAlertInstancesByRule(ctx context.Context, key models.AlertRuleKeyWithGroup, _ state.AlertInstancesDeleteReason) error {
return st.SQLStore.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
_, err := sess.Exec("DELETE FROM alert_instance WHERE rule_org_id = ? AND rule_uid = ?", key.OrgID, key.UID)
return err

Loading…
Cancel
Save