Alerting: declare constants for __dashboardUid__ and __panelId__ literals (#39976)

This commit is contained in:
Yuriy Tseretyan 2021-10-07 17:30:06 -04:00 committed by GitHub
parent c8651c46d0
commit 5836def6c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 20 deletions

View File

@ -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.

View File

@ -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()

View File

@ -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 {

View File

@ -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 {

View File

@ -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

View File

@ -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
}