From 3558cadb7ed0f490d9d3848530af51bf634e9ddd Mon Sep 17 00:00:00 2001 From: Alex Moreno Date: Thu, 3 Nov 2022 10:52:07 +0100 Subject: [PATCH] Alerting: Add title and description to Webhook contact point (#58058) * Add title and description to Webhook contact point * Remove deprecation message --- .../ngalert/notifier/channels/webhook.go | 22 ++++++++++++------- .../ngalert/notifier/channels/webhook_test.go | 11 +++++----- .../channels_config/available_channels.go | 15 +++++++++++++ 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/pkg/services/ngalert/notifier/channels/webhook.go b/pkg/services/ngalert/notifier/channels/webhook.go index 6b8928f8f4f..d5aaff281a1 100644 --- a/pkg/services/ngalert/notifier/channels/webhook.go +++ b/pkg/services/ngalert/notifier/channels/webhook.go @@ -43,6 +43,9 @@ type webhookSettings struct { // HTTP Basic Authentication. User string `json:"username,omitempty" yaml:"username,omitempty"` Password string `json:"password,omitempty" yaml:"password,omitempty"` + + Title string `json:"title,omitempty" yaml:"title,omitempty"` + Message string `json:"message,omitempty" yaml:"message,omitempty"` } func buildWebhookSettings(factoryConfig FactoryConfig) (webhookSettings, error) { @@ -66,6 +69,12 @@ func buildWebhookSettings(factoryConfig FactoryConfig) (webhookSettings, error) if settings.User != "" && settings.Password != "" && settings.AuthorizationScheme != "" && settings.AuthorizationCredentials != "" { return settings, errors.New("both HTTP Basic Authentication and Authorization Header are set, only 1 is permitted") } + if settings.Title == "" { + settings.Title = DefaultMessageTitleEmbed + } + if settings.Message == "" { + settings.Message = DefaultMessageEmbed + } return settings, err } @@ -132,12 +141,9 @@ type WebhookMessage struct { GroupKey string `json:"groupKey"` TruncatedAlerts int `json:"truncatedAlerts"` OrgID int64 `json:"orgId"` - - // Deprecated, to be removed in Grafana 10 - // These are present to make migration a little less disruptive. - Title string `json:"title"` - State string `json:"state"` - Message string `json:"message"` + Title string `json:"title"` + State string `json:"state"` + Message string `json:"message"` } // Notify implements the Notifier interface. @@ -167,8 +173,8 @@ func (wn *WebhookNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool GroupKey: groupKey.String(), TruncatedAlerts: numTruncated, OrgID: wn.orgID, - Title: tmpl(DefaultMessageTitleEmbed), - Message: tmpl(DefaultMessageEmbed), + Title: tmpl(wn.settings.Title), + Message: tmpl(wn.settings.Message), } if types.Alerts(as...).Status() == model.AlertFiring { msg.State = string(models.AlertStateAlerting) diff --git a/pkg/services/ngalert/notifier/channels/webhook_test.go b/pkg/services/ngalert/notifier/channels/webhook_test.go index 7ad2b2a9879..0563dcf08b6 100644 --- a/pkg/services/ngalert/notifier/channels/webhook_test.go +++ b/pkg/services/ngalert/notifier/channels/webhook_test.go @@ -42,8 +42,8 @@ func TestWebhookNotifier(t *testing.T) { expMsgError error }{ { - name: "Default config with one alert", - settings: `{"url": "http://localhost/test"}`, + name: "Default config with one alert with custom message", + settings: `{"url": "http://localhost/test", "message": "Custom message"}`, alerts: []*types.Alert{ { Alert: model.Alert{ @@ -90,16 +90,17 @@ func TestWebhookNotifier(t *testing.T) { GroupKey: "alertname", Title: "[FIRING:1] (val1)", State: "alerting", - Message: "**Firing**\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n", + Message: "Custom message", OrgID: orgID, }, expMsgError: nil, expHeaders: map[string]string{}, }, { - name: "Custom config with multiple alerts", + name: "Custom config with multiple alerts with custom title", settings: `{ "url": "http://localhost/test1", + "title": "Alerts firing: {{ len .Alerts.Firing }}", "username": "user1", "password": "mysecret", "httpMethod": "PUT", @@ -168,7 +169,7 @@ func TestWebhookNotifier(t *testing.T) { Version: "1", GroupKey: "alertname", TruncatedAlerts: 1, - Title: "[FIRING:2] ", + Title: "Alerts firing: 2", State: "alerting", Message: "**Firing**\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval2\n", OrgID: orgID, diff --git a/pkg/services/ngalert/notifier/channels_config/available_channels.go b/pkg/services/ngalert/notifier/channels_config/available_channels.go index cdf843aab62..64143f8ab03 100644 --- a/pkg/services/ngalert/notifier/channels_config/available_channels.go +++ b/pkg/services/ngalert/notifier/channels_config/available_channels.go @@ -735,6 +735,21 @@ func GetAvailableNotifiers() []*NotifierPlugin { InputType: InputTypeText, PropertyName: "maxAlerts", }, + { // New in 9.3. + Label: "Title", + Description: "Templated title of the message.", + Element: ElementTypeInput, + InputType: InputTypeText, + PropertyName: "title", + Placeholder: channels.DefaultMessageTitleEmbed, + }, + { // New in 9.3. + Label: "Message", + Description: "Custom message. You can use template variables.", + Element: ElementTypeTextArea, + PropertyName: "message", + Placeholder: channels.DefaultMessageEmbed, + }, }, }, {