mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: remove instances from db and cache on rule update (#33722)
* remove instances from db and cache on rule update * fix panic * rename
This commit is contained in:
parent
47af158ddb
commit
e58aca2d20
@ -230,6 +230,7 @@ func (srv RulerSrv) RoutePostNameRulesConfig(c *models.ReqContext, ruleGroupConf
|
||||
return response.Error(http.StatusBadRequest, "rule group name is not valid", nil)
|
||||
}
|
||||
|
||||
var alertRuleUIDs []string
|
||||
for _, r := range ruleGroupConfig.Rules {
|
||||
cond := ngmodels.Condition{
|
||||
Condition: r.GrafanaManagedAlert.Condition,
|
||||
@ -239,6 +240,7 @@ func (srv RulerSrv) RoutePostNameRulesConfig(c *models.ReqContext, ruleGroupConf
|
||||
if err := validateCondition(cond, c.SignedInUser, c.SkipCache, srv.DatasourceCache); err != nil {
|
||||
return response.Error(http.StatusBadRequest, fmt.Sprintf("failed to validate alert rule %s", r.GrafanaManagedAlert.Title), err)
|
||||
}
|
||||
alertRuleUIDs = append(alertRuleUIDs, r.GrafanaManagedAlert.UID)
|
||||
}
|
||||
|
||||
if err := srv.store.UpdateRuleGroup(store.UpdateRuleGroupCmd{
|
||||
@ -254,6 +256,10 @@ func (srv RulerSrv) RoutePostNameRulesConfig(c *models.ReqContext, ruleGroupConf
|
||||
return response.Error(http.StatusInternalServerError, "failed to update rule group", err)
|
||||
}
|
||||
|
||||
for _, uid := range alertRuleUIDs {
|
||||
srv.manager.RemoveByRuleUID(c.OrgId, uid)
|
||||
}
|
||||
|
||||
return response.JSON(http.StatusAccepted, util.DynMap{"message": "rule group updated successfully"})
|
||||
}
|
||||
|
||||
|
@ -40,6 +40,7 @@ type RuleStore interface {
|
||||
DeleteAlertRuleByUID(orgID int64, ruleUID string) error
|
||||
DeleteNamespaceAlertRules(orgID int64, namespaceUID string) ([]string, error)
|
||||
DeleteRuleGroupAlertRules(orgID int64, namespaceUID string, ruleGroup string) ([]string, error)
|
||||
DeleteAlertInstancesByRuleUID(orgID int64, ruleUID string) error
|
||||
GetAlertRuleByUID(*ngmodels.GetAlertRuleByUIDQuery) error
|
||||
GetAlertRulesForScheduling(query *ngmodels.ListAlertRulesQuery) error
|
||||
GetOrgAlertRules(query *ngmodels.ListAlertRulesQuery) error
|
||||
@ -157,6 +158,17 @@ func (st DBstore) DeleteRuleGroupAlertRules(orgID int64, namespaceUID string, ru
|
||||
return ruleUIDs, err
|
||||
}
|
||||
|
||||
// DeleteAlertInstanceByRuleUID is a handler for deleting alert instances by alert rule UID when a rule has been updated
|
||||
func (st DBstore) DeleteAlertInstancesByRuleUID(orgID int64, ruleUID string) error {
|
||||
return st.SQLStore.WithTransactionalDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||
_, err := sess.Exec("DELETE FROM alert_instance WHERE def_org_id = ? AND def_uid = ?", orgID, ruleUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// GetAlertRuleByUID is a handler for retrieving an alert rule from that database by its UID and organisation ID.
|
||||
// It returns ngmodels.ErrAlertRuleNotFound if no alert rule is found for the provided ID.
|
||||
func (st DBstore) GetAlertRuleByUID(query *ngmodels.GetAlertRuleByUIDQuery) error {
|
||||
@ -507,6 +519,15 @@ func (st DBstore) UpdateRuleGroup(cmd UpdateRuleGroupCmd) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// delete instances for rules that will not be removed
|
||||
for _, rule := range existingGroupRules {
|
||||
if _, ok := existingGroupRulesUIDs[rule.UID]; !ok {
|
||||
if err := st.DeleteAlertInstancesByRuleUID(cmd.OrgID, rule.UID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// delete the remaining rules
|
||||
for ruleUID := range existingGroupRulesUIDs {
|
||||
if err := st.DeleteAlertRuleByUID(cmd.OrgID, ruleUID); err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user