Alerting: Add additional newlines to Microsoft Teams notification message where necessary (#36126)

Signed-off-by: Ganesh Vernekar <ganeshvern@gmail.com>
This commit is contained in:
Ganesh Vernekar 2021-06-24 20:52:00 +05:30 committed by GitHub
parent f2bb3faea8
commit 8fe58fc2e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 97 additions and 1 deletions

View File

@ -30,6 +30,32 @@ Labels:
{{ end }}{{ end }}{{ if gt (len .Alerts.Resolved) 0 }}**Resolved**
{{ template "__text_alert_list" .Alerts.Resolved }}{{ end }}{{ end }}
{{ define "__teams_text_alert_list" }}{{ range . }}
Labels:
{{ range .Labels.SortedPairs }} - {{ .Name }} = {{ .Value }}
{{ end }}
Annotations:
{{ range .Annotations.SortedPairs }} - {{ .Name }} = {{ .Value }}
{{ 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 "teams.default.message" }}{{ if gt (len .Alerts.Firing) 0 }}**Firing**
{{ template "__teams_text_alert_list" .Alerts.Firing }}{{ if gt (len .Alerts.Resolved) 0 }}
{{ end }}{{ end }}{{ if gt (len .Alerts.Resolved) 0 }}**Resolved**
{{ template "__teams_text_alert_list" .Alerts.Resolved }}{{ end }}{{ end }}
`
// TemplateForTestsString is the template used for unit tests and integration tests.
@ -56,6 +82,8 @@ Labels:
{{ end }}{{ end }}{{ if gt (len .Alerts.Resolved) 0 }}**Resolved**
{{ template "__text_alert_list" .Alerts.Resolved }}{{ end }}{{ end }}
{{ define "teams.default.message" }}{{ template "default.message" . }}{{ end }}
`
func templateForTests(t *testing.T) *template.Template {

View File

@ -127,6 +127,74 @@ Annotations:
- ann1 = annv4
Source: http://localhost/alert4
Silence: http://localhost/grafana/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval4
`,
},
{
templateString: `{{ template "teams.default.message" .}}`,
expected: `**Firing**
Labels:
- alertname = alert1
- lbl1 = val1
Annotations:
- ann1 = annv1
Source: http://localhost/alert1
Silence: http://localhost/grafana/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1
Dashboard: http://localhost/grafana/d/dbuid123
Panel: http://localhost/grafana/d/dbuid123?viewPanel=puid123
Labels:
- alertname = alert1
- lbl1 = val2
Annotations:
- ann1 = annv2
Source: http://localhost/alert2
Silence: http://localhost/grafana/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval2
**Resolved**
Labels:
- alertname = alert1
- lbl1 = val3
Annotations:
- ann1 = annv3
Source: http://localhost/alert3
Silence: http://localhost/grafana/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval3
Dashboard: http://localhost/grafana/d/dbuid456
Panel: http://localhost/grafana/d/dbuid456?viewPanel=puid456
Labels:
- alertname = alert1
- lbl1 = val4
Annotations:
- ann1 = annv4
Source: http://localhost/alert4
Silence: http://localhost/grafana/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval4
`,
},
}

View File

@ -45,7 +45,7 @@ func NewTeamsNotifier(model *NotificationChannelConfig, t *template.Template) (*
Settings: model.Settings,
}),
URL: u,
Message: model.Settings.Get("message").MustString(`{{ template "default.message" .}}`),
Message: model.Settings.Get("message").MustString(`{{ template "teams.default.message" .}}`),
log: log.New("alerting.notifier.teams"),
tmpl: t,
}, nil