From 5836def6c2bcc3b4633d0bef70416a1c19090188 Mon Sep 17 00:00:00 2001 From: Yuriy Tseretyan Date: Thu, 7 Oct 2021 17:30:06 -0400 Subject: [PATCH] Alerting: declare constants for __dashboardUid__ and __panelId__ literals (#39976) --- pkg/services/ngalert/models/alert_rule.go | 4 ++++ .../notifier/channels/template_data.go | 5 +++-- pkg/services/ngalert/state/manager.go | 6 +++--- pkg/services/ngalert/store/alert_rule.go | 20 +++++++++---------- .../sqlstore/migrations/ualert/alert_rule.go | 5 +++-- .../sqlstore/migrations/ualert/ualert.go | 8 +++++--- 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/pkg/services/ngalert/models/alert_rule.go b/pkg/services/ngalert/models/alert_rule.go index 04222397c0a..e8bfc6a8442 100644 --- a/pkg/services/ngalert/models/alert_rule.go +++ b/pkg/services/ngalert/models/alert_rule.go @@ -46,6 +46,10 @@ const ( const ( RuleUIDLabel = "__alert_rule_uid__" NamespaceUIDLabel = "__alert_rule_namespace_uid__" + + // Annotations are actually a set of labels, so technically this is the label name of an annotation. + DashboardUIDAnnotation = "__dashboardUid__" + PanelIDAnnotation = "__panelId__" ) // AlertRule is the model for alert rules in unified alerting. diff --git a/pkg/services/ngalert/notifier/channels/template_data.go b/pkg/services/ngalert/notifier/channels/template_data.go index fca4ed65e05..53cd6f3c194 100644 --- a/pkg/services/ngalert/notifier/channels/template_data.go +++ b/pkg/services/ngalert/notifier/channels/template_data.go @@ -16,6 +16,7 @@ import ( "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/services/ngalert/logging" + ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models" ) type ExtendedAlert struct { @@ -77,11 +78,11 @@ func extendAlert(alert template.Alert, externalURL string, logger log.Logger) *E return extended } externalPath := u.Path - dashboardUid := alert.Annotations["__dashboardUid__"] + dashboardUid := alert.Annotations[ngmodels.DashboardUIDAnnotation] if len(dashboardUid) > 0 { u.Path = path.Join(externalPath, "/d/", dashboardUid) extended.DashboardURL = u.String() - panelId := alert.Annotations["__panelId__"] + panelId := alert.Annotations[ngmodels.PanelIDAnnotation] if len(panelId) > 0 { u.RawQuery = "viewPanel=" + panelId extended.PanelURL = u.String() diff --git a/pkg/services/ngalert/state/manager.go b/pkg/services/ngalert/state/manager.go index 29931ca5789..cce76407be7 100644 --- a/pkg/services/ngalert/state/manager.go +++ b/pkg/services/ngalert/state/manager.go @@ -151,7 +151,7 @@ func (st *Manager) ProcessEvalResults(ctx context.Context, alertRule *ngModels.A return states } -//Set the current state based on evaluation results +// Set the current state based on evaluation results func (st *Manager) setNextState(ctx context.Context, alertRule *ngModels.AlertRule, result eval.Result) *State { currentState := st.getOrCreate(alertRule, result) @@ -235,12 +235,12 @@ func translateInstanceState(state ngModels.InstanceStateType) eval.State { func (st *Manager) createAlertAnnotation(ctx context.Context, new eval.State, alertRule *ngModels.AlertRule, result eval.Result, oldState eval.State) { st.log.Debug("alert state changed creating annotation", "alertRuleUID", alertRule.UID, "newState", new.String()) - dashUid, ok := alertRule.Annotations["__dashboardUid__"] + dashUid, ok := alertRule.Annotations[ngModels.DashboardUIDAnnotation] if !ok { return } - panelUid := alertRule.Annotations["__panelId__"] + panelUid := alertRule.Annotations[ngModels.PanelIDAnnotation] panelId, err := strconv.ParseInt(panelUid, 10, 64) if err != nil { diff --git a/pkg/services/ngalert/store/alert_rule.go b/pkg/services/ngalert/store/alert_rule.go index 28e4259a85a..e4a991d931c 100644 --- a/pkg/services/ngalert/store/alert_rule.go +++ b/pkg/services/ngalert/store/alert_rule.go @@ -535,7 +535,7 @@ func (st DBstore) UpdateRuleGroup(cmd UpdateRuleGroupCmd) error { continue } - new := ngmodels.AlertRule{ + newAlertRule := ngmodels.AlertRule{ OrgID: cmd.OrgID, Title: r.GrafanaManagedAlert.Title, Condition: r.GrafanaManagedAlert.Condition, @@ -549,25 +549,25 @@ func (st DBstore) UpdateRuleGroup(cmd UpdateRuleGroupCmd) error { } if r.ApiRuleNode != nil { - new.For = time.Duration(r.ApiRuleNode.For) - new.Annotations = r.ApiRuleNode.Annotations - new.Labels = r.ApiRuleNode.Labels + newAlertRule.For = time.Duration(r.ApiRuleNode.For) + newAlertRule.Annotations = r.ApiRuleNode.Annotations + newAlertRule.Labels = r.ApiRuleNode.Labels } - if s := new.Annotations["__dashboardUid__"]; s != "" { - new.DashboardUID = &s + if s := newAlertRule.Annotations[ngmodels.DashboardUIDAnnotation]; s != "" { + newAlertRule.DashboardUID = &s } - if s := new.Annotations["__panelId__"]; s != "" { + if s := newAlertRule.Annotations[ngmodels.PanelIDAnnotation]; s != "" { panelID, err := strconv.ParseInt(s, 10, 64) if err != nil { - return fmt.Errorf("the __panelId__ annotation does not contain a valid Panel ID: %w", err) + return fmt.Errorf("the %s annotation does not contain a valid Panel ID: %w", ngmodels.PanelIDAnnotation, err) } - new.PanelID = &panelID + newAlertRule.PanelID = &panelID } upsertRule := UpsertRule{ - New: new, + New: newAlertRule, } if existingGroupRule, ok := existingGroupRulesUIDs[r.GrafanaManagedAlert.UID]; ok { diff --git a/pkg/services/sqlstore/migrations/ualert/alert_rule.go b/pkg/services/sqlstore/migrations/ualert/alert_rule.go index 17deea79c03..b110395ab66 100644 --- a/pkg/services/sqlstore/migrations/ualert/alert_rule.go +++ b/pkg/services/sqlstore/migrations/ualert/alert_rule.go @@ -5,6 +5,7 @@ import ( "fmt" "time" + ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models" "github.com/grafana/grafana/pkg/util" ) @@ -79,8 +80,8 @@ func addMigrationInfo(da *dashAlert) (map[string]string, map[string]string) { } annotations := make(map[string]string, 3) - annotations["__dashboardUid__"] = da.DashboardUID - annotations["__panelId__"] = fmt.Sprintf("%v", da.PanelId) + annotations[ngmodels.DashboardUIDAnnotation] = da.DashboardUID + annotations[ngmodels.PanelIDAnnotation] = fmt.Sprintf("%v", da.PanelId) annotations["__alertId__"] = fmt.Sprintf("%v", da.Id) return lbls, annotations diff --git a/pkg/services/sqlstore/migrations/ualert/ualert.go b/pkg/services/sqlstore/migrations/ualert/ualert.go index c1a248d48f1..6ca453d356a 100644 --- a/pkg/services/sqlstore/migrations/ualert/ualert.go +++ b/pkg/services/sqlstore/migrations/ualert/ualert.go @@ -8,6 +8,8 @@ import ( "strconv" "strings" + ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models" + pb "github.com/prometheus/alertmanager/silence/silencepb" "xorm.io/xorm" @@ -148,13 +150,13 @@ func (m *updateDashboardUIDPanelIDMigration) Exec(sess *xorm.Session, mg *migrat dashboardUID *string panelID *int64 ) - if s, ok := next.Annotations["__dashboardUid__"]; ok { + if s, ok := next.Annotations[ngmodels.DashboardUIDAnnotation]; ok { dashboardUID = &s } - if s, ok := next.Annotations["__panelId__"]; ok { + if s, ok := next.Annotations[ngmodels.PanelIDAnnotation]; ok { i, err := strconv.ParseInt(s, 10, 64) if err != nil { - return fmt.Errorf("the __panelId__ annotation does not contain a valid Panel ID: %w", err) + return fmt.Errorf("the %s annotation does not contain a valid Panel ID: %w", ngmodels.PanelIDAnnotation, err) } panelID = &i }