diff --git a/pkg/services/ngalert/notifier/channels/default_template.go b/pkg/services/ngalert/notifier/channels/default_template.go index da9f7570414..6ee1602539b 100644 --- a/pkg/services/ngalert/notifier/channels/default_template.go +++ b/pkg/services/ngalert/notifier/channels/default_template.go @@ -12,25 +12,24 @@ import ( const DefaultTemplateString = ` {{ define "__subject" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .GroupLabels.SortedPairs.Values | join " " }} {{ if gt (len .CommonLabels) (len .GroupLabels) }}({{ with .CommonLabels.Remove .GroupLabels.Names }}{{ .Values | join " " }}{{ end }}){{ end }}{{ end }} -{{ define "__text_alert_list" }}{{ range . }}Labels: +{{ define "__text_alert_list" }}{{ range . }} +Labels: {{ range .Labels.SortedPairs }} - {{ .Name }} = {{ .Value }} {{ end }}Annotations: {{ range .Annotations.SortedPairs }} - {{ .Name }} = {{ .Value }} -{{ end }}Source: {{ .GeneratorURL }} -{{ end }}{{ end }} +{{ end }}{{ if gt (len .GeneratorURL) 0 }}Source: {{ .GeneratorURL }} +{{ end }}{{ if gt (len .SilenceURL) 0 }}Silence: {{ .SilenceURL }} +{{ end }}{{ if gt (len .DashboardURL) 0 }}Dashboard: {{ .DashboardURL }} +{{ end }}{{ if gt (len .PanelURL) 0 }}Panel: {{ .PanelURL }} +{{ end }}{{ end }}{{ end }} {{ define "default.title" }}{{ template "__subject" . }}{{ end }} -{{ define "default.message" }}{{ if gt (len .Alerts.Firing) 0 }} -**Firing** -{{ template "__text_alert_list" .Alerts.Firing }} +{{ define "default.message" }}{{ if gt (len .Alerts.Firing) 0 }}**Firing** +{{ template "__text_alert_list" .Alerts.Firing }}{{ if gt (len .Alerts.Resolved) 0 }} -{{ end }} -{{ if gt (len .Alerts.Resolved) 0 }} -**Resolved** -{{ template "__text_alert_list" .Alerts.Resolved }} -{{ end }} -{{ end }} +{{ end }}{{ end }}{{ if gt (len .Alerts.Resolved) 0 }}**Resolved** +{{ template "__text_alert_list" .Alerts.Resolved }}{{ end }}{{ end }} ` func templateForTests(t *testing.T) *template.Template { diff --git a/pkg/services/ngalert/notifier/channels/dingding.go b/pkg/services/ngalert/notifier/channels/dingding.go index 7a2d30915df..aa80e15def0 100644 --- a/pkg/services/ngalert/notifier/channels/dingding.go +++ b/pkg/services/ngalert/notifier/channels/dingding.go @@ -6,8 +6,6 @@ import ( "fmt" "net/url" - gokit_log "github.com/go-kit/kit/log" - "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" @@ -16,7 +14,6 @@ import ( "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" - "github.com/grafana/grafana/pkg/services/ngalert/logging" ) const defaultDingdingMsgType = "link" @@ -78,9 +75,11 @@ func (dd *DingDingNotifier) Notify(ctx context.Context, as ...*types.Alert) (boo // Refer: https://open-doc.dingtalk.com/docs/doc.htm?treeId=385&articleId=104972&docType=1#s9 messageURL := "dingtalk://dingtalkclient/page/link?" + q.Encode() - data := notify.GetTemplateData(ctx, dd.tmpl, as, gokit_log.NewLogfmtLogger(logging.NewWrapper(dd.log))) var tmplErr error - tmpl := notify.TmplText(dd.tmpl, data, &tmplErr) + tmpl, _, err := TmplText(ctx, dd.tmpl, as, dd.log, &tmplErr) + if err != nil { + return false, err + } message := tmpl(dd.Message) title := tmpl(`{{ template "default.title" . }}`) diff --git a/pkg/services/ngalert/notifier/channels/dingding_test.go b/pkg/services/ngalert/notifier/channels/dingding_test.go index f0596af1e64..4e5955400a4 100644 --- a/pkg/services/ngalert/notifier/channels/dingding_test.go +++ b/pkg/services/ngalert/notifier/channels/dingding_test.go @@ -40,7 +40,7 @@ func TestDingdingNotifier(t *testing.T) { { Alert: model.Alert{ Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, - Annotations: model.LabelSet{"ann1": "annv1"}, + Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"}, }, }, }, @@ -48,7 +48,7 @@ func TestDingdingNotifier(t *testing.T) { "msgtype": "link", "link": map[string]interface{}{ "messageUrl": "dingtalk://dingtalkclient/page/link?pc_slide=false&url=http%3A%2F%2Flocalhost%2Falerting%2Flist", - "text": "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n", + "text": "**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\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n", "title": "[FIRING:1] (val1)", }, }, diff --git a/pkg/services/ngalert/notifier/channels/discord.go b/pkg/services/ngalert/notifier/channels/discord.go index d89097b78c9..e7b1cf4b812 100644 --- a/pkg/services/ngalert/notifier/channels/discord.go +++ b/pkg/services/ngalert/notifier/channels/discord.go @@ -7,8 +7,6 @@ import ( "strconv" "strings" - gokit_log "github.com/go-kit/kit/log" - "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" @@ -58,14 +56,16 @@ func NewDiscordNotifier(model *NotificationChannelConfig, t *template.Template) } func (d DiscordNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) { - data := notify.GetTemplateData(ctx, d.tmpl, as, gokit_log.NewNopLogger()) alerts := types.Alerts(as...) bodyJSON := simplejson.New() bodyJSON.Set("username", "Grafana") var tmplErr error - tmpl := notify.TmplText(d.tmpl, data, &tmplErr) + tmpl, _, err := TmplText(ctx, d.tmpl, as, d.log, &tmplErr) + if err != nil { + return false, err + } if d.Content != "" { bodyJSON.Set("content", tmpl(d.Content)) } diff --git a/pkg/services/ngalert/notifier/channels/discord_test.go b/pkg/services/ngalert/notifier/channels/discord_test.go index 7f1efa41780..d0b3d842212 100644 --- a/pkg/services/ngalert/notifier/channels/discord_test.go +++ b/pkg/services/ngalert/notifier/channels/discord_test.go @@ -40,12 +40,12 @@ func TestDiscordNotifier(t *testing.T) { { Alert: model.Alert{ Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, - Annotations: model.LabelSet{"ann1": "annv1"}, + Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"}, }, }, }, expMsg: map[string]interface{}{ - "content": "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n", + "content": "**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\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n", "embeds": []interface{}{map[string]interface{}{ "color": 1.4037554e+07, "footer": map[string]interface{}{ diff --git a/pkg/services/ngalert/notifier/channels/email.go b/pkg/services/ngalert/notifier/channels/email.go index 24ec0de23ac..387b12cfaea 100644 --- a/pkg/services/ngalert/notifier/channels/email.go +++ b/pkg/services/ngalert/notifier/channels/email.go @@ -6,8 +6,6 @@ import ( "net/url" "path" - gokit_log "github.com/go-kit/kit/log" - "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" @@ -16,7 +14,6 @@ import ( "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" - "github.com/grafana/grafana/pkg/services/ngalert/logging" "github.com/grafana/grafana/pkg/util" ) @@ -66,13 +63,11 @@ func NewEmailNotifier(model *NotificationChannelConfig, t *template.Template) (* // Notify sends the alert notification. func (en *EmailNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) { - // We only need ExternalURL from this template object. This hack should go away with https://github.com/prometheus/alertmanager/pull/2508. - data, err := ExtendData(notify.GetTemplateData(ctx, &template.Template{ExternalURL: en.tmpl.ExternalURL}, as, gokit_log.NewLogfmtLogger(logging.NewWrapper(en.log)))) + var tmplErr error + tmpl, data, err := TmplText(ctx, en.tmpl, as, en.log, &tmplErr) if err != nil { return false, err } - var tmplErr error - tmpl := TmplText(en.tmpl, data, &tmplErr) title := tmpl(`{{ template "default.title" . }}`) diff --git a/pkg/services/ngalert/notifier/channels/googlechat.go b/pkg/services/ngalert/notifier/channels/googlechat.go index cc7a12bba8b..ce3879c3da0 100644 --- a/pkg/services/ngalert/notifier/channels/googlechat.go +++ b/pkg/services/ngalert/notifier/channels/googlechat.go @@ -6,8 +6,6 @@ import ( "fmt" "time" - gokit_log "github.com/go-kit/kit/log" - "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" @@ -52,9 +50,11 @@ func NewGoogleChatNotifier(model *NotificationChannelConfig, t *template.Templat func (gcn *GoogleChatNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) { gcn.log.Debug("Executing Google Chat notification") - data := notify.GetTemplateData(ctx, gcn.tmpl, as, gokit_log.NewNopLogger()) var tmplErr error - tmpl := notify.TmplText(gcn.tmpl, data, &tmplErr) + tmpl, _, err := TmplText(ctx, gcn.tmpl, as, gcn.log, &tmplErr) + if err != nil { + return false, err + } widgets := []widget{} diff --git a/pkg/services/ngalert/notifier/channels/googlechat_test.go b/pkg/services/ngalert/notifier/channels/googlechat_test.go index 3d234d64666..16a55a7bd4b 100644 --- a/pkg/services/ngalert/notifier/channels/googlechat_test.go +++ b/pkg/services/ngalert/notifier/channels/googlechat_test.go @@ -41,7 +41,7 @@ func TestGoogleChatNotifier(t *testing.T) { { Alert: model.Alert{ Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, - Annotations: model.LabelSet{"ann1": "annv1"}, + Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"}, }, }, }, @@ -58,7 +58,7 @@ func TestGoogleChatNotifier(t *testing.T) { Widgets: []widget{ textParagraphWidget{ Text: text{ - Text: "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n", + Text: "**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\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n", }, }, buttonWidget{ @@ -118,7 +118,7 @@ func TestGoogleChatNotifier(t *testing.T) { Widgets: []widget{ textParagraphWidget{ Text: text{ - Text: "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSource: \n\n\n\n\n", + Text: "**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 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval2\n", }, }, buttonWidget{ diff --git a/pkg/services/ngalert/notifier/channels/kafka.go b/pkg/services/ngalert/notifier/channels/kafka.go index 0bf662c5fa2..5fbaef3e1df 100644 --- a/pkg/services/ngalert/notifier/channels/kafka.go +++ b/pkg/services/ngalert/notifier/channels/kafka.go @@ -5,7 +5,6 @@ import ( "fmt" "strings" - gokit_log "github.com/go-kit/kit/log" "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" @@ -67,9 +66,11 @@ func (kn *KafkaNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, kn.log.Debug("Notifying Kafka", "alert_state", state) - data := notify.GetTemplateData(ctx, kn.tmpl, as, gokit_log.NewNopLogger()) var tmplErr error - tmpl := notify.TmplText(kn.tmpl, data, &tmplErr) + tmpl, _, err := TmplText(ctx, kn.tmpl, as, kn.log, &tmplErr) + if err != nil { + return false, err + } bodyJSON := simplejson.New() bodyJSON.Set("alert_state", state) diff --git a/pkg/services/ngalert/notifier/channels/kafka_test.go b/pkg/services/ngalert/notifier/channels/kafka_test.go index 0b5fe5b9f30..0a93d49f920 100644 --- a/pkg/services/ngalert/notifier/channels/kafka_test.go +++ b/pkg/services/ngalert/notifier/channels/kafka_test.go @@ -41,7 +41,7 @@ func TestKafkaNotifier(t *testing.T) { { Alert: model.Alert{ Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, - Annotations: model.LabelSet{"ann1": "annv1"}, + Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"}, }, }, }, @@ -54,7 +54,7 @@ func TestKafkaNotifier(t *testing.T) { "client": "Grafana", "client_url": "http://localhost/alerting/list", "description": "[FIRING:1] (val1)", - "details": "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n", + "details": "**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\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n", "incident_key": "6e3538104c14b583da237e9693b76debbc17f0f8058ef20492e5853096cf8733" } } @@ -90,7 +90,7 @@ func TestKafkaNotifier(t *testing.T) { "client": "Grafana", "client_url": "http://localhost/alerting/list", "description": "[FIRING:2] ", - "details": "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSource: \n\n\n\n\n", + "details": "**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 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval2\n", "incident_key": "6e3538104c14b583da237e9693b76debbc17f0f8058ef20492e5853096cf8733" } } diff --git a/pkg/services/ngalert/notifier/channels/line.go b/pkg/services/ngalert/notifier/channels/line.go index 46dbf4ba691..2c187f42c29 100644 --- a/pkg/services/ngalert/notifier/channels/line.go +++ b/pkg/services/ngalert/notifier/channels/line.go @@ -6,8 +6,6 @@ import ( "net/url" "path" - gokit_log "github.com/go-kit/kit/log" - "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" @@ -58,9 +56,11 @@ func (ln *LineNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, e ruleURL := path.Join(ln.tmpl.ExternalURL.String(), "/alerting/list") - data := notify.GetTemplateData(ctx, ln.tmpl, as, gokit_log.NewNopLogger()) var tmplErr error - tmpl := notify.TmplText(ln.tmpl, data, &tmplErr) + tmpl, _, err := TmplText(ctx, ln.tmpl, as, ln.log, &tmplErr) + if err != nil { + return false, err + } body := fmt.Sprintf( "%s\n%s\n\n%s", diff --git a/pkg/services/ngalert/notifier/channels/line_test.go b/pkg/services/ngalert/notifier/channels/line_test.go index d6effd4a88c..ae6e618cd1f 100644 --- a/pkg/services/ngalert/notifier/channels/line_test.go +++ b/pkg/services/ngalert/notifier/channels/line_test.go @@ -39,7 +39,7 @@ func TestLineNotifier(t *testing.T) { { Alert: model.Alert{ Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, - Annotations: model.LabelSet{"ann1": "annv1"}, + Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"}, }, }, }, @@ -47,7 +47,7 @@ func TestLineNotifier(t *testing.T) { "Authorization": "Bearer sometoken", "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", }, - expMsg: "message=%5BFIRING%3A1%5D++%28val1%29%0Ahttp%3A%2Flocalhost%2Falerting%2Flist%0A%0A%0A%2A%2AFiring%2A%2A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val1%0AAnnotations%3A%0A+-+ann1+%3D+annv1%0ASource%3A+%0A%0A%0A%0A%0A", + expMsg: "message=%5BFIRING%3A1%5D++%28val1%29%0Ahttp%3A%2Flocalhost%2Falerting%2Flist%0A%0A%2A%2AFiring%2A%2A%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val1%0AAnnotations%3A%0A+-+ann1+%3D+annv1%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval1%0ADashboard%3A+http%3A%2F%2Flocalhost%2Fd%2Fabcd%0APanel%3A+http%3A%2F%2Flocalhost%2Fd%2Fabcd%3FviewPanel%3Defgh%0A", expInitError: nil, expMsgError: nil, }, { @@ -70,7 +70,7 @@ func TestLineNotifier(t *testing.T) { "Authorization": "Bearer sometoken", "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", }, - expMsg: "message=%5BFIRING%3A2%5D++%0Ahttp%3A%2Flocalhost%2Falerting%2Flist%0A%0A%0A%2A%2AFiring%2A%2A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val1%0AAnnotations%3A%0A+-+ann1+%3D+annv1%0ASource%3A+%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val2%0AAnnotations%3A%0A+-+ann1+%3D+annv2%0ASource%3A+%0A%0A%0A%0A%0A", + expMsg: "message=%5BFIRING%3A2%5D++%0Ahttp%3A%2Flocalhost%2Falerting%2Flist%0A%0A%2A%2AFiring%2A%2A%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val1%0AAnnotations%3A%0A+-+ann1+%3D+annv1%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval1%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val2%0AAnnotations%3A%0A+-+ann1+%3D+annv2%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval2%0A", expInitError: nil, expMsgError: nil, }, { diff --git a/pkg/services/ngalert/notifier/channels/opsgenie.go b/pkg/services/ngalert/notifier/channels/opsgenie.go index 4d391cdaa69..d31937b12ad 100644 --- a/pkg/services/ngalert/notifier/channels/opsgenie.go +++ b/pkg/services/ngalert/notifier/channels/opsgenie.go @@ -6,7 +6,6 @@ import ( "fmt" "net/http" - gokit_log "github.com/go-kit/kit/log" "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" @@ -18,7 +17,6 @@ import ( "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" - "github.com/grafana/grafana/pkg/services/ngalert/logging" ) const ( @@ -154,9 +152,11 @@ func (on *OpsgenieNotifier) buildOpsgenieMessage(ctx context.Context, alerts mod return nil, "", err } - data := notify.GetTemplateData(ctx, on.tmpl, as, gokit_log.NewLogfmtLogger(logging.NewWrapper(on.log))) var tmplErr error - tmpl := notify.TmplText(on.tmpl, data, &tmplErr) + tmpl, data, err := TmplText(ctx, on.tmpl, as, on.log, &tmplErr) + if err != nil { + return nil, "", err + } title := tmpl(`{{ template "default.title" . }}`) description := fmt.Sprintf( diff --git a/pkg/services/ngalert/notifier/channels/opsgenie_test.go b/pkg/services/ngalert/notifier/channels/opsgenie_test.go index 91e9628c9ee..18583819f43 100644 --- a/pkg/services/ngalert/notifier/channels/opsgenie_test.go +++ b/pkg/services/ngalert/notifier/channels/opsgenie_test.go @@ -39,13 +39,13 @@ func TestOpsgenieNotifier(t *testing.T) { { Alert: model.Alert{ Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, - Annotations: model.LabelSet{"ann1": "annv1"}, + Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"}, }, }, }, expMsg: `{ "alias": "6e3538104c14b583da237e9693b76debbc17f0f8058ef20492e5853096cf8733", - "description": "[FIRING:1] (val1)\nhttp://localhost/alerting/list\n\n\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n", + "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\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n", "details": { "url": "http://localhost/alerting/list" }, @@ -70,7 +70,7 @@ func TestOpsgenieNotifier(t *testing.T) { }, expMsg: `{ "alias": "6e3538104c14b583da237e9693b76debbc17f0f8058ef20492e5853096cf8733", - "description": "[FIRING:1] (val1)\nhttp://localhost/alerting/list\n\n\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n", + "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": { "url": "http://localhost/alerting/list" }, @@ -95,7 +95,7 @@ func TestOpsgenieNotifier(t *testing.T) { }, expMsg: `{ "alias": "6e3538104c14b583da237e9693b76debbc17f0f8058ef20492e5853096cf8733", - "description": "[FIRING:1] (val1)\nhttp://localhost/alerting/list\n\n\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n", + "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", "url": "http://localhost/alerting/list" @@ -126,7 +126,7 @@ func TestOpsgenieNotifier(t *testing.T) { }, expMsg: `{ "alias": "6e3538104c14b583da237e9693b76debbc17f0f8058ef20492e5853096cf8733", - "description": "[FIRING:2] \nhttp://localhost/alerting/list\n\n\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n", + "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", "url": "http://localhost/alerting/list" diff --git a/pkg/services/ngalert/notifier/channels/pagerduty.go b/pkg/services/ngalert/notifier/channels/pagerduty.go index 70146c57002..72bc93ecaff 100644 --- a/pkg/services/ngalert/notifier/channels/pagerduty.go +++ b/pkg/services/ngalert/notifier/channels/pagerduty.go @@ -6,7 +6,6 @@ import ( "fmt" "os" - gokit_log "github.com/go-kit/kit/log" "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" @@ -17,7 +16,6 @@ import ( "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" - "github.com/grafana/grafana/pkg/services/ngalert/logging" ) const ( @@ -125,9 +123,11 @@ func (pn *PagerdutyNotifier) buildPagerdutyMessage(ctx context.Context, alerts m eventType = pagerDutyEventResolve } - data := notify.GetTemplateData(ctx, pn.tmpl, as, gokit_log.NewLogfmtLogger(logging.NewWrapper(pn.log))) var tmplErr error - tmpl := notify.TmplText(pn.tmpl, data, &tmplErr) + tmpl, data, err := TmplText(ctx, pn.tmpl, as, pn.log, &tmplErr) + if err != nil { + return nil, "", err + } details := make(map[string]string, len(pn.CustomDetails)) for k, v := range pn.CustomDetails { diff --git a/pkg/services/ngalert/notifier/channels/pagerduty_test.go b/pkg/services/ngalert/notifier/channels/pagerduty_test.go index 49d6c4bdc32..34a7eb68532 100644 --- a/pkg/services/ngalert/notifier/channels/pagerduty_test.go +++ b/pkg/services/ngalert/notifier/channels/pagerduty_test.go @@ -44,7 +44,7 @@ func TestPagerdutyNotifier(t *testing.T) { { Alert: model.Alert{ Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, - Annotations: model.LabelSet{"ann1": "annv1"}, + Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"}, }, }, }, @@ -61,7 +61,7 @@ func TestPagerdutyNotifier(t *testing.T) { Component: "Grafana", Group: "default", CustomDetails: map[string]string{ - "firing": "Labels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n", + "firing": "\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n", "num_firing": "1", "num_resolved": "0", "resolved": "", @@ -108,7 +108,7 @@ func TestPagerdutyNotifier(t *testing.T) { Component: "My Grafana", Group: "my_group", CustomDetails: map[string]string{ - "firing": "Labels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSource: \n", + "firing": "\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 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval2\n", "num_firing": "2", "num_resolved": "0", "resolved": "", diff --git a/pkg/services/ngalert/notifier/channels/pushover.go b/pkg/services/ngalert/notifier/channels/pushover.go index d74340fec09..715adf42cf8 100644 --- a/pkg/services/ngalert/notifier/channels/pushover.go +++ b/pkg/services/ngalert/notifier/channels/pushover.go @@ -7,14 +7,12 @@ import ( "mime/multipart" "strconv" - gokit_log "github.com/go-kit/kit/log" "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" "github.com/pkg/errors" - "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" "github.com/prometheus/common/model" @@ -134,8 +132,10 @@ func (pn *PushoverNotifier) genPushoverBody(ctx context.Context, as ...*types.Al alerts := types.Alerts(as...) var tmplErr error - data := notify.GetTemplateData(ctx, pn.tmpl, as, gokit_log.NewNopLogger()) - tmpl := notify.TmplText(pn.tmpl, data, &tmplErr) + tmpl, _, err := TmplText(ctx, pn.tmpl, as, pn.log, &tmplErr) + if err != nil { + return nil, b, err + } w := multipart.NewWriter(&b) boundary := GetBoundary() diff --git a/pkg/services/ngalert/notifier/channels/pushover_test.go b/pkg/services/ngalert/notifier/channels/pushover_test.go index f6990e9109a..a85d901a6ea 100644 --- a/pkg/services/ngalert/notifier/channels/pushover_test.go +++ b/pkg/services/ngalert/notifier/channels/pushover_test.go @@ -47,7 +47,7 @@ func TestPushoverNotifier(t *testing.T) { { Alert: model.Alert{ Labels: model.LabelSet{"__alert_rule_uid__": "rule uid", "alertname": "alert1", "lbl1": "val1"}, - Annotations: model.LabelSet{"ann1": "annv1"}, + Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"}, }, }, }, @@ -56,10 +56,10 @@ func TestPushoverNotifier(t *testing.T) { "token": "", "priority": "0", "sound": "", - "title": "[FIRING:1] (rule uid val1)", + "title": "[FIRING:1] (val1)", "url": "http://localhost/alerting/list", "url_title": "Show alert rule", - "message": "\n**Firing**\nLabels:\n - alertname = alert1\n - __alert_rule_uid__ = rule uid\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n", + "message": "**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\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n", "html": "1", }, expInitError: nil, diff --git a/pkg/services/ngalert/notifier/channels/sensugo.go b/pkg/services/ngalert/notifier/channels/sensugo.go index 674be0679c2..0366d068750 100644 --- a/pkg/services/ngalert/notifier/channels/sensugo.go +++ b/pkg/services/ngalert/notifier/channels/sensugo.go @@ -7,13 +7,11 @@ import ( "strings" "time" - gokit_log "github.com/go-kit/kit/log" "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" - "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" "github.com/prometheus/common/model" @@ -74,9 +72,11 @@ func NewSensuGoNotifier(model *NotificationChannelConfig, t *template.Template) func (sn *SensuGoNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) { sn.log.Debug("Sending Sensu Go result") - data := notify.GetTemplateData(ctx, sn.tmpl, as, gokit_log.NewNopLogger()) var tmplErr error - tmpl := notify.TmplText(sn.tmpl, data, &tmplErr) + tmpl, _, err := TmplText(ctx, sn.tmpl, as, sn.log, &tmplErr) + if err != nil { + return false, err + } // Sensu Go alerts require an entity and a check. We set it to the user-specified // value (optional), else we fallback and use the grafana rule anme and ruleID. diff --git a/pkg/services/ngalert/notifier/channels/sensugo_test.go b/pkg/services/ngalert/notifier/channels/sensugo_test.go index 29923882531..bf4a99e5e1e 100644 --- a/pkg/services/ngalert/notifier/channels/sensugo_test.go +++ b/pkg/services/ngalert/notifier/channels/sensugo_test.go @@ -41,7 +41,7 @@ func TestSensuGoNotifier(t *testing.T) { { Alert: model.Alert{ Labels: model.LabelSet{"__alert_rule_uid__": "rule uid", "alertname": "alert1", "lbl1": "val1"}, - Annotations: model.LabelSet{"ann1": "annv1"}, + Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"}, }, }, }, @@ -59,7 +59,7 @@ func TestSensuGoNotifier(t *testing.T) { "ruleURL": "http://localhost/alerting/list", }, }, - "output": "\n**Firing**\nLabels:\n - alertname = alert1\n - __alert_rule_uid__ = rule uid\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n", + "output": "**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\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n", "issued": time.Now().Unix(), "interval": 86400, "status": 2, diff --git a/pkg/services/ngalert/notifier/channels/slack.go b/pkg/services/ngalert/notifier/channels/slack.go index 4e78877af7d..724910db93b 100644 --- a/pkg/services/ngalert/notifier/channels/slack.go +++ b/pkg/services/ngalert/notifier/channels/slack.go @@ -14,9 +14,7 @@ import ( "strings" "time" - gokit_log "github.com/go-kit/kit/log" "github.com/prometheus/alertmanager/config" - "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" @@ -24,7 +22,6 @@ import ( "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" - "github.com/grafana/grafana/pkg/services/ngalert/logging" "github.com/grafana/grafana/pkg/setting" ) @@ -245,10 +242,12 @@ var sendSlackRequest = func(request *http.Request, logger log.Logger) error { } func (sn *SlackNotifier) buildSlackMessage(ctx context.Context, as []*types.Alert) (*slackMessage, error) { - data := notify.GetTemplateData(ctx, sn.tmpl, as, gokit_log.NewLogfmtLogger(logging.NewWrapper(sn.log))) alerts := types.Alerts(as...) var tmplErr error - tmpl := notify.TmplText(sn.tmpl, data, &tmplErr) + tmpl, _, err := TmplText(ctx, sn.tmpl, as, sn.log, &tmplErr) + if err != nil { + return nil, err + } ruleURL, err := joinUrlPath(sn.tmpl.ExternalURL.String(), "/alerting/list") if err != nil { diff --git a/pkg/services/ngalert/notifier/channels/slack_test.go b/pkg/services/ngalert/notifier/channels/slack_test.go index 8bc3aea8f76..e52438f30d1 100644 --- a/pkg/services/ngalert/notifier/channels/slack_test.go +++ b/pkg/services/ngalert/notifier/channels/slack_test.go @@ -45,7 +45,7 @@ func TestSlackNotifier(t *testing.T) { { Alert: model.Alert{ Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, - Annotations: model.LabelSet{"ann1": "annv1"}, + Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"}, }, }, }, @@ -57,7 +57,7 @@ func TestSlackNotifier(t *testing.T) { { Title: "[FIRING:1] (val1)", TitleLink: "http://localhost/alerting/list", - Text: "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n", + Text: "**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\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n", Fallback: "[FIRING:1] (val1)", Fields: nil, Footer: "Grafana v", @@ -93,7 +93,7 @@ func TestSlackNotifier(t *testing.T) { { Title: "[FIRING:1] (val1)", TitleLink: "http://localhost/alerting/list", - Text: "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n", + Text: "**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", Fallback: "[FIRING:1] (val1)", Fields: nil, Footer: "Grafana v", @@ -136,7 +136,7 @@ func TestSlackNotifier(t *testing.T) { { Title: "2 firing, 0 resolved", TitleLink: "http://localhost/alerting/list", - Text: "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSource: \n\n\n\n\n", + Text: "**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 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval2\n", Fallback: "2 firing, 0 resolved", Fields: nil, Footer: "Grafana v", @@ -226,7 +226,7 @@ func TestSlackNotifier(t *testing.T) { expBody, err := json.Marshal(c.expMsg) require.NoError(t, err) - require.Equal(t, string(expBody), body) + require.JSONEq(t, string(expBody), body) }) } } diff --git a/pkg/services/ngalert/notifier/channels/teams.go b/pkg/services/ngalert/notifier/channels/teams.go index d1bccd57b79..404c15c09b5 100644 --- a/pkg/services/ngalert/notifier/channels/teams.go +++ b/pkg/services/ngalert/notifier/channels/teams.go @@ -4,9 +4,7 @@ import ( "context" "encoding/json" - gokit_log "github.com/go-kit/kit/log" "github.com/pkg/errors" - "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" @@ -15,7 +13,6 @@ import ( "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" - "github.com/grafana/grafana/pkg/services/ngalert/logging" ) // TeamsNotifier is responsible for sending @@ -56,9 +53,11 @@ func NewTeamsNotifier(model *NotificationChannelConfig, t *template.Template) (* // Notify send an alert notification to Microsoft teams. func (tn *TeamsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) { - data := notify.GetTemplateData(ctx, tn.tmpl, as, gokit_log.NewLogfmtLogger(logging.NewWrapper(tn.log))) var tmplErr error - tmpl := notify.TmplText(tn.tmpl, data, &tmplErr) + tmpl, _, err := TmplText(ctx, tn.tmpl, as, tn.log, &tmplErr) + if err != nil { + return false, err + } ruleURL, err := joinUrlPath(tn.tmpl.ExternalURL.String(), "/alerting/list") if err != nil { diff --git a/pkg/services/ngalert/notifier/channels/teams_test.go b/pkg/services/ngalert/notifier/channels/teams_test.go index 94090ba2459..e48405f23c2 100644 --- a/pkg/services/ngalert/notifier/channels/teams_test.go +++ b/pkg/services/ngalert/notifier/channels/teams_test.go @@ -40,7 +40,7 @@ func TestTeamsNotifier(t *testing.T) { { Alert: model.Alert{ Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, - Annotations: model.LabelSet{"ann1": "annv1"}, + Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"}, }, }, }, @@ -53,7 +53,7 @@ func TestTeamsNotifier(t *testing.T) { "sections": []map[string]interface{}{ { "title": "Details", - "text": "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n", + "text": "**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\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n", }, }, "potentialAction": []map[string]interface{}{ diff --git a/pkg/services/ngalert/notifier/channels/telegram.go b/pkg/services/ngalert/notifier/channels/telegram.go index addce0536a6..da9aba69ff8 100644 --- a/pkg/services/ngalert/notifier/channels/telegram.go +++ b/pkg/services/ngalert/notifier/channels/telegram.go @@ -6,8 +6,6 @@ import ( "fmt" "mime/multipart" - gokit_log "github.com/go-kit/kit/log" - "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" @@ -16,7 +14,6 @@ import ( "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" - "github.com/grafana/grafana/pkg/services/ngalert/logging" ) var ( @@ -125,9 +122,11 @@ func (tn *TelegramNotifier) buildTelegramMessage(ctx context.Context, as []*type msg["chat_id"] = tn.ChatID msg["parse_mode"] = "html" - data := notify.GetTemplateData(ctx, &template.Template{ExternalURL: tn.tmpl.ExternalURL}, as, gokit_log.NewLogfmtLogger(logging.NewWrapper(tn.log))) var tmplErr error - tmpl := notify.TmplText(tn.tmpl, data, &tmplErr) + tmpl, _, err := TmplText(ctx, tn.tmpl, as, tn.log, &tmplErr) + if err != nil { + return nil, err + } message := tmpl(tn.Message) if tmplErr != nil { diff --git a/pkg/services/ngalert/notifier/channels/telegram_test.go b/pkg/services/ngalert/notifier/channels/telegram_test.go index 7de37167afe..f5bcef516a2 100644 --- a/pkg/services/ngalert/notifier/channels/telegram_test.go +++ b/pkg/services/ngalert/notifier/channels/telegram_test.go @@ -40,7 +40,7 @@ func TestTelegramNotifier(t *testing.T) { { Alert: model.Alert{ Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, - Annotations: model.LabelSet{"ann1": "annv1"}, + Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"}, GeneratorURL: "a URL", }, }, @@ -48,7 +48,7 @@ func TestTelegramNotifier(t *testing.T) { expMsg: map[string]string{ "chat_id": "someid", "parse_mode": "html", - "text": "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: a URL\n\n\n\n\n", + "text": "**Firing**\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: a URL\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n", }, expInitError: nil, expMsgError: nil, @@ -76,7 +76,7 @@ func TestTelegramNotifier(t *testing.T) { expMsg: map[string]string{ "chat_id": "someid", "parse_mode": "html", - "text": "__Custom Firing__\n2 Firing\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: a URL\nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSource: \n", + "text": "__Custom Firing__\n2 Firing\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: a URL\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\n\nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval2\n", }, expInitError: nil, expMsgError: nil, diff --git a/pkg/services/ngalert/notifier/channels/template_data.go b/pkg/services/ngalert/notifier/channels/template_data.go index bb3ace8d64f..e9c01f1ecf4 100644 --- a/pkg/services/ngalert/notifier/channels/template_data.go +++ b/pkg/services/ngalert/notifier/channels/template_data.go @@ -1,6 +1,7 @@ package channels import ( + "context" "fmt" "net/url" "path" @@ -8,8 +9,14 @@ import ( "strings" "time" + gokit_log "github.com/go-kit/kit/log" + "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" + "github.com/prometheus/alertmanager/types" "github.com/prometheus/common/model" + + "github.com/grafana/grafana/pkg/infra/log" + "github.com/grafana/grafana/pkg/services/ngalert/logging" ) type ExtendedAlert struct { @@ -120,14 +127,20 @@ func ExtendData(data *template.Data) (*ExtendedData, error) { return extended, nil } -func TmplText(tmpl *template.Template, data *ExtendedData, err *error) func(string) string { +func TmplText(ctx context.Context, tmpl *template.Template, alerts []*types.Alert, l log.Logger, tmplErr *error) (func(string) string, *ExtendedData, error) { + promTmplData := notify.GetTemplateData(ctx, tmpl, alerts, gokit_log.NewLogfmtLogger(logging.NewWrapper(l))) + data, err := ExtendData(promTmplData) + if err != nil { + return nil, nil, err + } + return func(name string) (s string) { - if *err != nil { + if *tmplErr != nil { return } - s, *err = tmpl.ExecuteTextString(name, data) + s, *tmplErr = tmpl.ExecuteTextString(name, data) return s - } + }, data, nil } // Firing returns the subset of alerts that are firing. diff --git a/pkg/services/ngalert/notifier/channels/threema.go b/pkg/services/ngalert/notifier/channels/threema.go index e666a776114..e91272260ae 100644 --- a/pkg/services/ngalert/notifier/channels/threema.go +++ b/pkg/services/ngalert/notifier/channels/threema.go @@ -7,8 +7,6 @@ import ( "path" "strings" - gokit_log "github.com/go-kit/kit/log" - "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" "github.com/prometheus/common/model" @@ -85,9 +83,11 @@ func NewThreemaNotifier(model *NotificationChannelConfig, t *template.Template) func (tn *ThreemaNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) { tn.log.Debug("Sending threema alert notification", "from", tn.GatewayID, "to", tn.RecipientID) - tmplData := notify.GetTemplateData(ctx, tn.tmpl, as, gokit_log.NewNopLogger()) var tmplErr error - tmpl := notify.TmplText(tn.tmpl, tmplData, &tmplErr) + tmpl, _, err := TmplText(ctx, tn.tmpl, as, tn.log, &tmplErr) + if err != nil { + return false, err + } // Set up basic API request data data := url.Values{} diff --git a/pkg/services/ngalert/notifier/channels/threema_test.go b/pkg/services/ngalert/notifier/channels/threema_test.go index 83a01db70d1..bcabb62e9c6 100644 --- a/pkg/services/ngalert/notifier/channels/threema_test.go +++ b/pkg/services/ngalert/notifier/channels/threema_test.go @@ -42,11 +42,11 @@ func TestThreemaNotifier(t *testing.T) { { Alert: model.Alert{ Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, - Annotations: model.LabelSet{"ann1": "annv1"}, + Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"}, }, }, }, - expMsg: "from=%2A1234567&secret=supersecret&text=%E2%9A%A0%EF%B8%8F+%5BFIRING%3A1%5D++%28val1%29%0A%0A%2AMessage%3A%2A%0A%0A%2A%2AFiring%2A%2A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val1%0AAnnotations%3A%0A+-+ann1+%3D+annv1%0ASource%3A+%0A%0A%0A%0A%0A%0A%2AURL%3A%2A+http%3A%2Flocalhost%2Falerting%2Flist%0A&to=87654321", + expMsg: "from=%2A1234567&secret=supersecret&text=%E2%9A%A0%EF%B8%8F+%5BFIRING%3A1%5D++%28val1%29%0A%0A%2AMessage%3A%2A%0A%2A%2AFiring%2A%2A%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val1%0AAnnotations%3A%0A+-+ann1+%3D+annv1%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval1%0ADashboard%3A+http%3A%2F%2Flocalhost%2Fd%2Fabcd%0APanel%3A+http%3A%2F%2Flocalhost%2Fd%2Fabcd%3FviewPanel%3Defgh%0A%0A%2AURL%3A%2A+http%3A%2Flocalhost%2Falerting%2Flist%0A&to=87654321", expInitError: nil, expMsgError: nil, }, { @@ -69,7 +69,7 @@ func TestThreemaNotifier(t *testing.T) { }, }, }, - expMsg: "from=%2A1234567&secret=supersecret&text=%E2%9A%A0%EF%B8%8F+%5BFIRING%3A2%5D++%0A%0A%2AMessage%3A%2A%0A%0A%2A%2AFiring%2A%2A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val1%0AAnnotations%3A%0A+-+ann1+%3D+annv1%0ASource%3A+%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val2%0AAnnotations%3A%0A+-+ann1+%3D+annv2%0ASource%3A+%0A%0A%0A%0A%0A%0A%2AURL%3A%2A+http%3A%2Flocalhost%2Falerting%2Flist%0A&to=87654321", + expMsg: "from=%2A1234567&secret=supersecret&text=%E2%9A%A0%EF%B8%8F+%5BFIRING%3A2%5D++%0A%0A%2AMessage%3A%2A%0A%2A%2AFiring%2A%2A%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val1%0AAnnotations%3A%0A+-+ann1+%3D+annv1%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval1%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val2%0AAnnotations%3A%0A+-+ann1+%3D+annv2%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval2%0A%0A%2AURL%3A%2A+http%3A%2Flocalhost%2Falerting%2Flist%0A&to=87654321", expInitError: nil, expMsgError: nil, }, { diff --git a/pkg/services/ngalert/notifier/channels/victorops.go b/pkg/services/ngalert/notifier/channels/victorops.go index 06be4062a15..2e4054996da 100644 --- a/pkg/services/ngalert/notifier/channels/victorops.go +++ b/pkg/services/ngalert/notifier/channels/victorops.go @@ -5,7 +5,6 @@ import ( "strings" "time" - gokit_log "github.com/go-kit/kit/log" "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" @@ -75,9 +74,11 @@ func (vn *VictoropsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bo messageType = victoropsAlertStateRecovery } - data := notify.GetTemplateData(ctx, vn.tmpl, as, gokit_log.NewNopLogger()) var tmplErr error - tmpl := notify.TmplText(vn.tmpl, data, &tmplErr) + tmpl, _, err := TmplText(ctx, vn.tmpl, as, vn.log, &tmplErr) + if err != nil { + return false, err + } groupKey, err := notify.ExtractGroupKey(ctx) if err != nil { diff --git a/pkg/services/ngalert/notifier/channels/victorops_test.go b/pkg/services/ngalert/notifier/channels/victorops_test.go index 40764ba8dfa..2c6f5ad049f 100644 --- a/pkg/services/ngalert/notifier/channels/victorops_test.go +++ b/pkg/services/ngalert/notifier/channels/victorops_test.go @@ -38,7 +38,7 @@ func TestVictoropsNotifier(t *testing.T) { { Alert: model.Alert{ Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, - Annotations: model.LabelSet{"ann1": "annv1"}, + Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"}, }, }, }, @@ -48,7 +48,7 @@ func TestVictoropsNotifier(t *testing.T) { "entity_id": "6e3538104c14b583da237e9693b76debbc17f0f8058ef20492e5853096cf8733", "message_type": "CRITICAL", "monitoring_tool": "Grafana v", - "state_message": "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n" + "state_message": "**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\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n" }`, expInitError: nil, expMsgError: nil, @@ -74,7 +74,7 @@ func TestVictoropsNotifier(t *testing.T) { "entity_id": "6e3538104c14b583da237e9693b76debbc17f0f8058ef20492e5853096cf8733", "message_type": "CRITICAL", "monitoring_tool": "Grafana v", - "state_message": "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSource: \n\n\n\n\n" + "state_message": "**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 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval2\n" }`, expInitError: nil, expMsgError: nil, diff --git a/pkg/services/ngalert/notifier/channels/webhook.go b/pkg/services/ngalert/notifier/channels/webhook.go index 65f56230581..953656d0287 100644 --- a/pkg/services/ngalert/notifier/channels/webhook.go +++ b/pkg/services/ngalert/notifier/channels/webhook.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" - gokit_log "github.com/go-kit/kit/log" "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" @@ -16,7 +15,6 @@ import ( "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" - "github.com/grafana/grafana/pkg/services/ngalert/logging" ) // WebhookNotifier is responsible for sending @@ -59,7 +57,7 @@ func NewWebHookNotifier(model *NotificationChannelConfig, t *template.Template) // webhookMessage defines the JSON object send to webhook endpoints. type webhookMessage struct { - *template.Data + *ExtendedData // The protocol version. Version string `json:"version"` @@ -81,13 +79,14 @@ func (wn *WebhookNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool } as, numTruncated := truncateAlerts(wn.MaxAlerts, as) - data := notify.GetTemplateData(ctx, wn.tmpl, as, gokit_log.NewLogfmtLogger(logging.NewWrapper(wn.log))) - var tmplErr error - tmpl := notify.TmplText(wn.tmpl, data, &tmplErr) + tmpl, data, err := TmplText(ctx, wn.tmpl, as, wn.log, &tmplErr) + if err != nil { + return false, err + } msg := &webhookMessage{ Version: "1", - Data: data, + ExtendedData: data, GroupKey: groupKey.String(), TruncatedAlerts: numTruncated, Title: tmpl(`{{ template "default.title" . }}`), diff --git a/pkg/services/ngalert/notifier/channels/webhook_test.go b/pkg/services/ngalert/notifier/channels/webhook_test.go index 2ab86714eab..f2677d8d445 100644 --- a/pkg/services/ngalert/notifier/channels/webhook_test.go +++ b/pkg/services/ngalert/notifier/channels/webhook_test.go @@ -44,17 +44,17 @@ func TestWebhookNotifier(t *testing.T) { { Alert: model.Alert{ Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, - Annotations: model.LabelSet{"ann1": "annv1"}, + Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"}, }, }, }, expUrl: "http://localhost/test", expHttpMethod: "POST", expMsg: &webhookMessage{ - Data: &template.Data{ + ExtendedData: &ExtendedData{ Receiver: "my_receiver", Status: "firing", - Alerts: template.Alerts{ + Alerts: ExtendedAlerts{ { Status: "firing", Labels: template.KV{ @@ -64,7 +64,10 @@ func TestWebhookNotifier(t *testing.T) { Annotations: template.KV{ "ann1": "annv1", }, - Fingerprint: "fac0861a85de433a", + Fingerprint: "fac0861a85de433a", + DashboardURL: "http://localhost/d/abcd", + PanelURL: "http://localhost/d/abcd?viewPanel=efgh", + SilenceURL: "http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1", }, }, GroupLabels: template.KV{ @@ -83,7 +86,7 @@ func TestWebhookNotifier(t *testing.T) { GroupKey: "alertname", Title: "[FIRING:1] (val1)", State: "alerting", - Message: "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n", + Message: "**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\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n", }, expInitError: nil, expMsgError: nil, @@ -119,10 +122,10 @@ func TestWebhookNotifier(t *testing.T) { expUsername: "user1", expPassword: "mysecret", expMsg: &webhookMessage{ - Data: &template.Data{ + ExtendedData: &ExtendedData{ Receiver: "my_receiver", Status: "firing", - Alerts: template.Alerts{ + Alerts: ExtendedAlerts{ { Status: "firing", Labels: template.KV{ @@ -133,6 +136,7 @@ func TestWebhookNotifier(t *testing.T) { "ann1": "annv1", }, Fingerprint: "fac0861a85de433a", + SilenceURL: "http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1", }, { Status: "firing", Labels: template.KV{ @@ -143,6 +147,7 @@ func TestWebhookNotifier(t *testing.T) { "ann1": "annv2", }, Fingerprint: "fab6861a85d5eeb5", + SilenceURL: "http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval2", }, }, GroupLabels: template.KV{ @@ -159,7 +164,7 @@ func TestWebhookNotifier(t *testing.T) { TruncatedAlerts: 1, Title: "[FIRING:2] ", State: "alerting", - Message: "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSource: \n\n\n\n\n", + Message: "**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 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval2\n", }, expInitError: nil, expMsgError: nil, diff --git a/pkg/tests/api/alerting/api_notification_channel_test.go b/pkg/tests/api/alerting/api_notification_channel_test.go index ce76fd052a6..f66a0b02fd2 100644 --- a/pkg/tests/api/alerting/api_notification_channel_test.go +++ b/pkg/tests/api/alerting/api_notification_channel_test.go @@ -259,10 +259,10 @@ func (nc *mockNotificationChannel) matchesExpNotifications(exp map[string][]stri fallthrough case "slack_recv1/slack_test_without_token": // It has a time component "ts". - r1 = regexp.MustCompile(`.*"ts"\s*:\s*([0-9]{10})`) + r1 = regexp.MustCompile(`.*"ts"\s*:\s*([0-9]+)`) case "sensugo/events": // It has a time component "ts". - r1 = regexp.MustCompile(`.*"issued"\s*:\s*([0-9]{10})`) + r1 = regexp.MustCompile(`.*"issued"\s*:\s*([0-9]+)`) case "pagerduty_recvX/pagerduty_testX": // It has a changing "source". r1 = regexp.MustCompile(`.*"source"\s*:\s*"([^"]+)"`) @@ -271,7 +271,7 @@ func (nc *mockNotificationChannel) matchesExpNotifications(exp map[string][]stri r1 = regexp.MustCompile(`.*"text"\s*:\s*"(Grafana v[^"]+)"`) case "victorops_recv/victorops_test": // It has a time component "timestamp". - r1 = regexp.MustCompile(`.*"timestamp"\s*:\s*([0-9]{10})`) + r1 = regexp.MustCompile(`.*"timestamp"\s*:\s*([0-9]+)`) case "v1/alerts": // It has a changing time fields. r1 = regexp.MustCompile(`.*"startsAt"\s*:\s*"([^"]+)"`) @@ -1304,10 +1304,10 @@ var expNotifications = map[string][]string{ "icon_url": "https://awesomeemoji.com/rocket", "attachments": [ { - "title": "Integration Test [FIRING:1] SlackAlert1 (UID_SlackAlert1)", + "title": "Integration Test [FIRING:1] SlackAlert1 ", "title_link": "http://localhost:3000/alerting/list", "text": "Integration Test ", - "fallback": "Integration Test [FIRING:1] SlackAlert1 (UID_SlackAlert1)", + "fallback": "Integration Test [FIRING:1] SlackAlert1 ", "footer": "Grafana v", "footer_icon": "https://grafana.com/assets/img/fav32.png", "color": "#D63232", @@ -1331,10 +1331,10 @@ var expNotifications = map[string][]string{ "username": "Integration Test", "attachments": [ { - "title": "[FIRING:1] SlackAlert2 (UID_SlackAlert2)", + "title": "[FIRING:1] SlackAlert2 ", "title_link": "http://localhost:3000/alerting/list", - "text": "\n**Firing**\nLabels:\n - alertname = SlackAlert2\n - __alert_rule_uid__ = UID_SlackAlert2\nAnnotations:\nSource: \n\n\n\n\n", - "fallback": "[FIRING:1] SlackAlert2 (UID_SlackAlert2)", + "text": "**Firing**\n\nLabels:\n - alertname = SlackAlert2\nAnnotations:\nSilence: http://localhost:3000/alerting/silence/new?alertmanager=grafana&matchers=alertname%%3DSlackAlert2\n", + "fallback": "[FIRING:1] SlackAlert2 ", "footer": "Grafana v", "footer_icon": "https://grafana.com/assets/img/fav32.png", "color": "#D63232", @@ -1356,17 +1356,17 @@ var expNotifications = map[string][]string{ `{ "routing_key": "pagerduty_recv/pagerduty_test", "dedup_key": "234edb34441f942f713f3c2ccf58b1d719d921b4cbe34e57a1630f1dee847e3b", - "description": "[FIRING:1] PagerdutyAlert (UID_PagerdutyAlert)", + "description": "[FIRING:1] PagerdutyAlert ", "event_action": "trigger", "payload": { - "summary": "Integration Test [FIRING:1] PagerdutyAlert (UID_PagerdutyAlert)", + "summary": "Integration Test [FIRING:1] PagerdutyAlert ", "source": "%s", "severity": "warning", "class": "testclass", "component": "Integration Test", "group": "testgroup", "custom_details": { - "firing": "Labels:\n - alertname = PagerdutyAlert\n - __alert_rule_uid__ = UID_PagerdutyAlert\nAnnotations:\nSource: \n", + "firing": "\nLabels:\n - alertname = PagerdutyAlert\nAnnotations:\nSilence: http://localhost:3000/alerting/silence/new?alertmanager=grafana&matchers=alertname%%3DPagerdutyAlert\n", "num_firing": "1", "num_resolved": "0", "resolved": "" @@ -1386,8 +1386,8 @@ var expNotifications = map[string][]string{ `{ "link": { "messageUrl": "dingtalk://dingtalkclient/page/link?pc_slide=false&url=http%3A%2F%2Flocalhost%3A3000%2Falerting%2Flist", - "text": "\n**Firing**\nLabels:\n - alertname = DingDingAlert\n - __alert_rule_uid__ = UID_DingDingAlert\nAnnotations:\nSource: \n\n\n\n\n", - "title": "[FIRING:1] DingDingAlert (UID_DingDingAlert)" + "text": "**Firing**\n\nLabels:\n - alertname = DingDingAlert\nAnnotations:\nSilence: http://localhost:3000/alerting/silence/new?alertmanager=grafana&matchers=alertname%3DDingDingAlert\n", + "title": "[FIRING:1] DingDingAlert " }, "msgtype": "link" }`, @@ -1411,13 +1411,13 @@ var expNotifications = map[string][]string{ ], "sections": [ { - "text": "\n**Firing**\nLabels:\n - alertname = TeamsAlert\n - __alert_rule_uid__ = UID_TeamsAlert\nAnnotations:\nSource: \n\n\n\n\n", + "text": "**Firing**\n\nLabels:\n - alertname = TeamsAlert\nAnnotations:\nSilence: http://localhost:3000/alerting/silence/new?alertmanager=grafana&matchers=alertname%3DTeamsAlert\n", "title": "Details" } ], - "summary": "[FIRING:1] TeamsAlert (UID_TeamsAlert)", + "summary": "[FIRING:1] TeamsAlert ", "themeColor": "#D63232", - "title": "[FIRING:1] TeamsAlert (UID_TeamsAlert)" + "title": "[FIRING:1] TeamsAlert " }`, }, "webhook_recv/webhook_test": { @@ -1428,19 +1428,22 @@ var expNotifications = map[string][]string{ { "status": "firing", "labels": { - "__alert_rule_uid__": "UID_WebhookAlert", "alertname": "WebhookAlert" }, "annotations": {}, "startsAt": "%s", "endsAt": "0001-01-01T00:00:00Z", "generatorURL": "", - "fingerprint": "929467973978d053" + "fingerprint": "929467973978d053", + "silenceURL": "http://localhost:3000/alerting/silence/new?alertmanager=grafana&matchers=alertname%%3DWebhookAlert", + "dashboardURL": "", + "panelURL": "" } ], - "groupLabels": {"alertname": "WebhookAlert"}, + "groupLabels": { + "alertname": "WebhookAlert" + }, "commonLabels": { - "__alert_rule_uid__": "UID_WebhookAlert", "alertname": "WebhookAlert" }, "commonAnnotations": {}, @@ -1448,14 +1451,14 @@ var expNotifications = map[string][]string{ "version": "1", "groupKey": "{}/{alertname=\"WebhookAlert\"}:{alertname=\"WebhookAlert\"}", "truncatedAlerts": 0, - "title": "[FIRING:1] WebhookAlert (UID_WebhookAlert)", + "title": "[FIRING:1] WebhookAlert ", "state": "alerting", - "message": "\n**Firing**\nLabels:\n - alertname = WebhookAlert\n - __alert_rule_uid__ = UID_WebhookAlert\nAnnotations:\nSource: \n\n\n\n\n" + "message": "**Firing**\n\nLabels:\n - alertname = WebhookAlert\nAnnotations:\nSilence: http://localhost:3000/alerting/silence/new?alertmanager=grafana&matchers=alertname%%3DWebhookAlert\n" }`, }, "discord_recv/discord_test": { `{ - "content": "\n**Firing**\nLabels:\n - alertname = DiscordAlert\n - __alert_rule_uid__ = UID_DiscordAlert\nAnnotations:\nSource: \n\n\n\n\n", + "content": "**Firing**\n\nLabels:\n - alertname = DiscordAlert\nAnnotations:\nSilence: http://localhost:3000/alerting/silence/new?alertmanager=grafana&matchers=alertname%3DDiscordAlert\n", "embeds": [ { "color": 14037554, @@ -1463,7 +1466,7 @@ var expNotifications = map[string][]string{ "icon_url": "https://grafana.com/assets/img/fav32.png", "text": "Grafana v" }, - "title": "[FIRING:1] DiscordAlert (UID_DiscordAlert)", + "title": "[FIRING:1] DiscordAlert ", "type": "rich", "url": "http://localhost:3000/alerting/list" } @@ -1483,7 +1486,7 @@ var expNotifications = map[string][]string{ }, "name": "default" }, - "output": "\n**Firing**\nLabels:\n - alertname = SensuGoAlert\n - __alert_rule_uid__ = UID_SensuGoAlert\nAnnotations:\nSource: \n\n\n\n\n", + "output": "**Firing**\n\nLabels:\n - alertname = SensuGoAlert\nAnnotations:\nSilence: http://localhost:3000/alerting/silence/new?alertmanager=grafana&matchers=alertname%%3DSensuGoAlert\n", "status": 2 }, "entity": { @@ -1496,26 +1499,26 @@ var expNotifications = map[string][]string{ }`, }, "pushover_recv/pushover_test": { - "--abcd\r\nContent-Disposition: form-data; name=\"user\"\r\n\r\nmysecretkey\r\n--abcd\r\nContent-Disposition: form-data; name=\"token\"\r\n\r\nmysecrettoken\r\n--abcd\r\nContent-Disposition: form-data; name=\"priority\"\r\n\r\n0\r\n--abcd\r\nContent-Disposition: form-data; name=\"sound\"\r\n\r\n\r\n--abcd\r\nContent-Disposition: form-data; name=\"title\"\r\n\r\n[FIRING:1] PushoverAlert (UID_PushoverAlert)\r\n--abcd\r\nContent-Disposition: form-data; name=\"url\"\r\n\r\nhttp://localhost:3000/alerting/list\r\n--abcd\r\nContent-Disposition: form-data; name=\"url_title\"\r\n\r\nShow alert rule\r\n--abcd\r\nContent-Disposition: form-data; name=\"message\"\r\n\r\n\n**Firing**\nLabels:\n - alertname = PushoverAlert\n - __alert_rule_uid__ = UID_PushoverAlert\nAnnotations:\nSource: \n\n\n\n\n\r\n--abcd\r\nContent-Disposition: form-data; name=\"html\"\r\n\r\n1\r\n--abcd--\r\n", + "--abcd\r\nContent-Disposition: form-data; name=\"user\"\r\n\r\nmysecretkey\r\n--abcd\r\nContent-Disposition: form-data; name=\"token\"\r\n\r\nmysecrettoken\r\n--abcd\r\nContent-Disposition: form-data; name=\"priority\"\r\n\r\n0\r\n--abcd\r\nContent-Disposition: form-data; name=\"sound\"\r\n\r\n\r\n--abcd\r\nContent-Disposition: form-data; name=\"title\"\r\n\r\n[FIRING:1] PushoverAlert \r\n--abcd\r\nContent-Disposition: form-data; name=\"url\"\r\n\r\nhttp://localhost:3000/alerting/list\r\n--abcd\r\nContent-Disposition: form-data; name=\"url_title\"\r\n\r\nShow alert rule\r\n--abcd\r\nContent-Disposition: form-data; name=\"message\"\r\n\r\n**Firing**\n\nLabels:\n - alertname = PushoverAlert\nAnnotations:\nSilence: http://localhost:3000/alerting/silence/new?alertmanager=grafana&matchers=alertname%3DPushoverAlert\n\r\n--abcd\r\nContent-Disposition: form-data; name=\"html\"\r\n\r\n1\r\n--abcd--\r\n", }, "telegram_recv/bot6sh027hs034h": { - "--abcd\r\nContent-Disposition: form-data; name=\"chat_id\"\r\n\r\ntelegram_chat_id\r\n--abcd\r\nContent-Disposition: form-data; name=\"parse_mode\"\r\n\r\nhtml\r\n--abcd\r\nContent-Disposition: form-data; name=\"text\"\r\n\r\n\n**Firing**\nLabels:\n - alertname = TelegramAlert\n - __alert_rule_uid__ = UID_TelegramAlert\nAnnotations:\nSource: \n\n\n\n\n\r\n--abcd--\r\n", + "--abcd\r\nContent-Disposition: form-data; name=\"chat_id\"\r\n\r\ntelegram_chat_id\r\n--abcd\r\nContent-Disposition: form-data; name=\"parse_mode\"\r\n\r\nhtml\r\n--abcd\r\nContent-Disposition: form-data; name=\"text\"\r\n\r\n**Firing**\n\nLabels:\n - alertname = TelegramAlert\nAnnotations:\nSilence: http://localhost:3000/alerting/silence/new?alertmanager=grafana&matchers=alertname%3DTelegramAlert\n\r\n--abcd--\r\n", }, "googlechat_recv/googlechat_test": { `{ - "previewText": "[FIRING:1] GoogleChatAlert (UID_GoogleChatAlert)", - "fallbackText": "[FIRING:1] GoogleChatAlert (UID_GoogleChatAlert)", + "previewText": "[FIRING:1] GoogleChatAlert ", + "fallbackText": "[FIRING:1] GoogleChatAlert ", "cards": [ { "header": { - "title": "[FIRING:1] GoogleChatAlert (UID_GoogleChatAlert)" + "title": "[FIRING:1] GoogleChatAlert " }, "sections": [ { "widgets": [ { "textParagraph": { - "text": "\n**Firing**\nLabels:\n - alertname = GoogleChatAlert\n - __alert_rule_uid__ = UID_GoogleChatAlert\nAnnotations:\nSource: \n\n\n\n\n" + "text": "**Firing**\n\nLabels:\n - alertname = GoogleChatAlert\nAnnotations:\nSilence: http://localhost:3000/alerting/silence/new?alertmanager=grafana&matchers=alertname%%3DGoogleChatAlert\n" } }, { @@ -1552,8 +1555,8 @@ var expNotifications = map[string][]string{ "alert_state": "alerting", "client": "Grafana", "client_url": "http://localhost:3000/alerting/list", - "description": "[FIRING:1] KafkaAlert (UID_KafkaAlert)", - "details": "\n**Firing**\nLabels:\n - alertname = KafkaAlert\n - __alert_rule_uid__ = UID_KafkaAlert\nAnnotations:\nSource: \n\n\n\n\n", + "description": "[FIRING:1] KafkaAlert ", + "details": "**Firing**\n\nLabels:\n - alertname = KafkaAlert\nAnnotations:\nSilence: http://localhost:3000/alerting/silence/new?alertmanager=grafana&matchers=alertname%3DKafkaAlert\n", "incident_key": "35c0bdb1715f9162a20d7b2a01cb2e3a4c5b1dc663571701e3f67212b696332f" } } @@ -1561,30 +1564,30 @@ var expNotifications = map[string][]string{ }`, }, "line_recv/line_test": { - `message=%5BFIRING%3A1%5D+LineAlert+%28UID_LineAlert%29%0Ahttp%3A%2Flocalhost%3A3000%2Falerting%2Flist%0A%0A%0A%2A%2AFiring%2A%2A%0ALabels%3A%0A+-+alertname+%3D+LineAlert%0A+-+__alert_rule_uid__+%3D+UID_LineAlert%0AAnnotations%3A%0ASource%3A+%0A%0A%0A%0A%0A`, + `message=%5BFIRING%3A1%5D+LineAlert+%0Ahttp%3A%2Flocalhost%3A3000%2Falerting%2Flist%0A%0A%2A%2AFiring%2A%2A%0A%0ALabels%3A%0A+-+alertname+%3D+LineAlert%0AAnnotations%3A%0ASilence%3A+http%3A%2F%2Flocalhost%3A3000%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253DLineAlert%0A`, }, "threema_recv/threema_test": { - `from=%2A1234567&secret=myapisecret&text=%E2%9A%A0%EF%B8%8F+%5BFIRING%3A1%5D+ThreemaAlert+%28UID_ThreemaAlert%29%0A%0A%2AMessage%3A%2A%0A%0A%2A%2AFiring%2A%2A%0ALabels%3A%0A+-+alertname+%3D+ThreemaAlert%0A+-+__alert_rule_uid__+%3D+UID_ThreemaAlert%0AAnnotations%3A%0ASource%3A+%0A%0A%0A%0A%0A%0A%2AURL%3A%2A+http%3A%2Flocalhost%3A3000%2Falerting%2Flist%0A&to=abcdefgh`, + `from=%2A1234567&secret=myapisecret&text=%E2%9A%A0%EF%B8%8F+%5BFIRING%3A1%5D+ThreemaAlert+%0A%0A%2AMessage%3A%2A%0A%2A%2AFiring%2A%2A%0A%0ALabels%3A%0A+-+alertname+%3D+ThreemaAlert%0AAnnotations%3A%0ASilence%3A+http%3A%2F%2Flocalhost%3A3000%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253DThreemaAlert%0A%0A%2AURL%3A%2A+http%3A%2Flocalhost%3A3000%2Falerting%2Flist%0A&to=abcdefgh`, }, "victorops_recv/victorops_test": { `{ "alert_url": "http://localhost:3000/alerting/list", - "entity_display_name": "[FIRING:1] VictorOpsAlert (UID_VictorOpsAlert)", + "entity_display_name": "[FIRING:1] VictorOpsAlert ", "entity_id": "633ae988fa7074bcb51f3d1c5fef2ba1c5c4ccb45b3ecbf681f7d507b078b1ae", "message_type": "CRITICAL", "monitoring_tool": "Grafana v", - "state_message": "\n**Firing**\nLabels:\n - alertname = VictorOpsAlert\n - __alert_rule_uid__ = UID_VictorOpsAlert\nAnnotations:\nSource: \n\n\n\n\n", + "state_message": "**Firing**\n\nLabels:\n - alertname = VictorOpsAlert\nAnnotations:\nSilence: http://localhost:3000/alerting/silence/new?alertmanager=grafana&matchers=alertname%%3DVictorOpsAlert\n", "timestamp": %s }`, }, "opsgenie_recv/opsgenie_test": { `{ "alias": "47e92f0f6ef9fe99f3954e0d6155f8d09c4b9a038d8c3105e82c0cee4c62956e", - "description": "[FIRING:1] OpsGenieAlert (UID_OpsGenieAlert)\nhttp://localhost:3000/alerting/list\n\n\n**Firing**\nLabels:\n - alertname = OpsGenieAlert\n - __alert_rule_uid__ = UID_OpsGenieAlert\nAnnotations:\nSource: \n\n\n\n\n", + "description": "[FIRING:1] OpsGenieAlert \nhttp://localhost:3000/alerting/list\n\n**Firing**\n\nLabels:\n - alertname = OpsGenieAlert\nAnnotations:\nSilence: http://localhost:3000/alerting/silence/new?alertmanager=grafana&matchers=alertname%3DOpsGenieAlert\n", "details": { "url": "http://localhost:3000/alerting/list" }, - "message": "[FIRING:1] OpsGenieAlert (UID_OpsGenieAlert)", + "message": "[FIRING:1] OpsGenieAlert ", "source": "Grafana", "tags": [] }`,