mirror of
https://github.com/grafana/grafana.git
synced 2025-02-11 08:05:43 -06:00
Alerting: Migrate tags as labels and not annotations (#34990)
Signed-off-by: Ganesh Vernekar <ganeshvern@gmail.com>
This commit is contained in:
parent
05f3161f8e
commit
a23674ef99
@ -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)
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
|
@ -1609,7 +1609,7 @@ var expNotifications = map[string][]string{
|
||||
},
|
||||
"message": "[FIRING:1] OpsGenieAlert ",
|
||||
"source": "Grafana",
|
||||
"tags": []
|
||||
"tags": ["alertname:OpsGenieAlert"]
|
||||
}`,
|
||||
},
|
||||
// Prometheus Alertmanager.
|
||||
|
Loading…
Reference in New Issue
Block a user