From eae88f8ce825432d390841210ee5e7610cd30096 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Mon, 31 May 2021 10:43:10 -0400 Subject: [PATCH] Alerting: Migrate tags as labels and not annotations (#34990) (#34992) Signed-off-by: Ganesh Vernekar (cherry picked from commit a23674ef99c1b82386e8a777ec6934221ff40f4a) Co-authored-by: Ganesh Vernekar <15064823+codesome@users.noreply.github.com> --- .../ngalert/notifier/channels/opsgenie.go | 14 +++++++------ .../notifier/channels/opsgenie_test.go | 11 +++++----- .../sqlstore/migrations/ualert/alert_rule.go | 20 +++++++++---------- .../alerting/api_notification_channel_test.go | 2 +- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/pkg/services/ngalert/notifier/channels/opsgenie.go b/pkg/services/ngalert/notifier/channels/opsgenie.go index d31937b12ad..63313ef2c3c 100644 --- a/pkg/services/ngalert/notifier/channels/opsgenie.go +++ b/pkg/services/ngalert/notifier/channels/opsgenie.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "net/http" + "sort" "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" @@ -169,9 +170,9 @@ func (on *OpsgenieNotifier) buildOpsgenieMessage(ctx context.Context, alerts mod var priority string // In the new alerting system we've moved away from the grafana-tags. Instead, annotations on the rule itself should be used. - annotations := make(map[string]string, len(data.CommonAnnotations)) - for k, v := range data.CommonAnnotations { - annotations[k] = tmpl(v) + lbls := make(map[string]string, len(data.CommonLabels)) + for k, v := range data.CommonLabels { + lbls[k] = tmpl(v) if k == "og_priority" { if ValidPriorities[v] { @@ -187,17 +188,18 @@ func (on *OpsgenieNotifier) buildOpsgenieMessage(ctx context.Context, alerts mod details.Set("url", ruleURL) if on.sendDetails() { - for k, v := range annotations { + for k, v := range lbls { details.Set(k, v) } } - tags := make([]string, 0, len(annotations)) + tags := make([]string, 0, len(lbls)) if on.sendTags() { - for k, v := range annotations { + for k, v := range lbls { tags = append(tags, fmt.Sprintf("%s:%s", k, v)) } } + sort.Strings(tags) if priority != "" && on.OverridePriority { bodyJSON.Set("priority", priority) diff --git a/pkg/services/ngalert/notifier/channels/opsgenie_test.go b/pkg/services/ngalert/notifier/channels/opsgenie_test.go index 18583819f43..6c8f2d1685e 100644 --- a/pkg/services/ngalert/notifier/channels/opsgenie_test.go +++ b/pkg/services/ngalert/notifier/channels/opsgenie_test.go @@ -51,7 +51,7 @@ func TestOpsgenieNotifier(t *testing.T) { }, "message": "[FIRING:1] (val1)", "source": "Grafana", - "tags": ["ann1:annv1"] + "tags": ["alertname:alert1", "lbl1:val1"] }`, }, { @@ -76,7 +76,7 @@ func TestOpsgenieNotifier(t *testing.T) { }, "message": "[FIRING:1] (val1)", "source": "Grafana", - "tags": ["ann1:annv1"] + "tags": ["alertname:alert1", "lbl1:val1"] }`, }, { @@ -97,7 +97,8 @@ func TestOpsgenieNotifier(t *testing.T) { "alias": "6e3538104c14b583da237e9693b76debbc17f0f8058ef20492e5853096cf8733", "description": "[FIRING:1] (val1)\nhttp://localhost/alerting/list\n\n**Firing**\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\n", "details": { - "ann1": "annv1", + "alertname": "alert1", + "lbl1": "val1", "url": "http://localhost/alerting/list" }, "message": "[FIRING:1] (val1)", @@ -128,12 +129,12 @@ func TestOpsgenieNotifier(t *testing.T) { "alias": "6e3538104c14b583da237e9693b76debbc17f0f8058ef20492e5853096cf8733", "description": "[FIRING:2] \nhttp://localhost/alerting/list\n\n**Firing**\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\n\nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval2\n", "details": { - "ann1": "annv1", + "alertname": "alert1", "url": "http://localhost/alerting/list" }, "message": "[FIRING:2] ", "source": "Grafana", - "tags": ["ann1:annv1"] + "tags": ["alertname:alert1"] }`, expInitError: nil, expMsgError: nil, diff --git a/pkg/services/sqlstore/migrations/ualert/alert_rule.go b/pkg/services/sqlstore/migrations/ualert/alert_rule.go index efb43269c2d..5a0728a8e0b 100644 --- a/pkg/services/sqlstore/migrations/ualert/alert_rule.go +++ b/pkg/services/sqlstore/migrations/ualert/alert_rule.go @@ -72,17 +72,18 @@ func (a *alertRule) makeVersion() *alertRuleVersion { } } -func addMigrationInfo(da *dashAlert) map[string]string { - annotations := da.ParsedSettings.AlertRuleTags - if annotations == nil { - annotations = make(map[string]string, 3) +func addMigrationInfo(da *dashAlert) (map[string]string, map[string]string) { + lbls := da.ParsedSettings.AlertRuleTags + if lbls == nil { + lbls = make(map[string]string) } + annotations := make(map[string]string, 3) annotations["__dashboardUid__"] = da.DashboardUID annotations["__panelId__"] = fmt.Sprintf("%v", da.PanelId) annotations["__alertId__"] = fmt.Sprintf("%v", da.Id) - return annotations + return lbls, annotations } func getMigrationString(da dashAlert) string { @@ -90,7 +91,9 @@ func getMigrationString(da dashAlert) string { } func (m *migration) makeAlertRule(cond condition, da dashAlert, folderUID string) (*alertRule, error) { - annotations := addMigrationInfo(&da) + lbls, annotations := addMigrationInfo(&da) + lbls["alertname"] = da.Name + annotations["message"] = da.Message ar := &alertRule{ OrgId: da.OrgId, @@ -105,10 +108,7 @@ func (m *migration) makeAlertRule(cond condition, da dashAlert, folderUID string For: duration(da.For), Updated: time.Now().UTC(), Annotations: annotations, - Labels: map[string]string{ - "alertname": da.Name, - "message": da.Message, - }, + Labels: lbls, } var err error diff --git a/pkg/tests/api/alerting/api_notification_channel_test.go b/pkg/tests/api/alerting/api_notification_channel_test.go index ba9a57d3a38..44d20be02a3 100644 --- a/pkg/tests/api/alerting/api_notification_channel_test.go +++ b/pkg/tests/api/alerting/api_notification_channel_test.go @@ -1609,7 +1609,7 @@ var expNotifications = map[string][]string{ }, "message": "[FIRING:1] OpsGenieAlert ", "source": "Grafana", - "tags": [] + "tags": ["alertname:OpsGenieAlert"] }`, }, // Prometheus Alertmanager.