From 3a580dc3cd1b74028931bc7a5760d86fb844fea1 Mon Sep 17 00:00:00 2001 From: Alexander Weaver Date: Tue, 20 Sep 2022 10:20:06 -0500 Subject: [PATCH] Alerting: Extract default message embed into named constant (#55424) * Resolve conflicts * Combine multiple const definitions --- .../notifier/channels/default_template.go | 5 ++++- .../notifier/channels/default_template_test.go | 2 +- .../ngalert/notifier/channels/dingding.go | 2 +- .../ngalert/notifier/channels/discord.go | 2 +- .../ngalert/notifier/channels/googlechat.go | 2 +- .../ngalert/notifier/channels/kafka.go | 2 +- pkg/services/ngalert/notifier/channels/line.go | 2 +- .../ngalert/notifier/channels/opsgenie.go | 2 +- .../ngalert/notifier/channels/pushover.go | 2 +- .../ngalert/notifier/channels/sensugo.go | 2 +- .../ngalert/notifier/channels/slack.go | 2 +- .../ngalert/notifier/channels/telegram.go | 2 +- .../ngalert/notifier/channels/threema.go | 2 +- .../ngalert/notifier/channels/victorops.go | 2 +- .../ngalert/notifier/channels/webhook.go | 2 +- .../ngalert/notifier/channels/wecom.go | 2 +- .../channels_config/available_channels.go | 18 +++++++++--------- 17 files changed, 28 insertions(+), 25 deletions(-) diff --git a/pkg/services/ngalert/notifier/channels/default_template.go b/pkg/services/ngalert/notifier/channels/default_template.go index 6e82f622406..36dc5cb6095 100644 --- a/pkg/services/ngalert/notifier/channels/default_template.go +++ b/pkg/services/ngalert/notifier/channels/default_template.go @@ -8,7 +8,10 @@ import ( "github.com/stretchr/testify/require" ) -const DefaultMessageTitleEmbed = `{{ template "default.title" . }}` +const ( + DefaultMessageTitleEmbed = `{{ template "default.title" . }}` + DefaultMessageEmbed = `{{ template "default.message" . }}` +) var DefaultTemplateString = ` {{ define "__subject" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ if gt (.Alerts.Resolved | len) 0 }}, RESOLVED:{{ .Alerts.Resolved | len }}{{ end }}{{ end }}] {{ .GroupLabels.SortedPairs.Values | join " " }} {{ if gt (len .CommonLabels) (len .GroupLabels) }}({{ with .CommonLabels.Remove .GroupLabels.Names }}{{ .Values | join " " }}{{ end }}){{ end }}{{ end }} diff --git a/pkg/services/ngalert/notifier/channels/default_template_test.go b/pkg/services/ngalert/notifier/channels/default_template_test.go index cbec7352a09..6f2827c9076 100644 --- a/pkg/services/ngalert/notifier/channels/default_template_test.go +++ b/pkg/services/ngalert/notifier/channels/default_template_test.go @@ -88,7 +88,7 @@ func TestDefaultTemplateString(t *testing.T) { expected: `[FIRING:2, RESOLVED:2] (alert1)`, }, { - templateString: `{{ template "default.message" .}}`, + templateString: DefaultMessageEmbed, expected: `**Firing** Value: 1234 diff --git a/pkg/services/ngalert/notifier/channels/dingding.go b/pkg/services/ngalert/notifier/channels/dingding.go index db4c21e3e9e..544c94eaba2 100644 --- a/pkg/services/ngalert/notifier/channels/dingding.go +++ b/pkg/services/ngalert/notifier/channels/dingding.go @@ -32,7 +32,7 @@ func NewDingDingConfig(config *NotificationChannelConfig) (*DingDingConfig, erro return &DingDingConfig{ NotificationChannelConfig: config, MsgType: config.Settings.Get("msgType").MustString(defaultDingdingMsgType), - Message: config.Settings.Get("message").MustString(`{{ template "default.message" .}}`), + Message: config.Settings.Get("message").MustString(DefaultMessageEmbed), URL: config.Settings.Get("url").MustString(), }, nil } diff --git a/pkg/services/ngalert/notifier/channels/discord.go b/pkg/services/ngalert/notifier/channels/discord.go index a6296b5a96f..3e8ddbbef84 100644 --- a/pkg/services/ngalert/notifier/channels/discord.go +++ b/pkg/services/ngalert/notifier/channels/discord.go @@ -61,7 +61,7 @@ func NewDiscordConfig(config *NotificationChannelConfig) (*DiscordConfig, error) } return &DiscordConfig{ NotificationChannelConfig: config, - Content: config.Settings.Get("message").MustString(`{{ template "default.message" . }}`), + Content: config.Settings.Get("message").MustString(DefaultMessageEmbed), AvatarURL: config.Settings.Get("avatar_url").MustString(), WebhookURL: discordURL, UseDiscordUsername: config.Settings.Get("use_discord_username").MustBool(false), diff --git a/pkg/services/ngalert/notifier/channels/googlechat.go b/pkg/services/ngalert/notifier/channels/googlechat.go index b213f9c4147..1b19031c029 100644 --- a/pkg/services/ngalert/notifier/channels/googlechat.go +++ b/pkg/services/ngalert/notifier/channels/googlechat.go @@ -55,7 +55,7 @@ func NewGoogleChatConfig(config *NotificationChannelConfig) (*GoogleChatConfig, return &GoogleChatConfig{ NotificationChannelConfig: config, URL: url, - Content: config.Settings.Get("message").MustString(`{{ template "default.message" . }}`), + Content: config.Settings.Get("message").MustString(DefaultMessageEmbed), }, nil } diff --git a/pkg/services/ngalert/notifier/channels/kafka.go b/pkg/services/ngalert/notifier/channels/kafka.go index e97c0173662..4a4656cf295 100644 --- a/pkg/services/ngalert/notifier/channels/kafka.go +++ b/pkg/services/ngalert/notifier/channels/kafka.go @@ -100,7 +100,7 @@ func (kn *KafkaNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, bodyJSON.Set("alert_state", state) bodyJSON.Set("description", tmpl(DefaultMessageTitleEmbed)) bodyJSON.Set("client", "Grafana") - bodyJSON.Set("details", tmpl(`{{ template "default.message" . }}`)) + bodyJSON.Set("details", tmpl(DefaultMessageEmbed)) ruleURL := joinUrlPath(kn.tmpl.ExternalURL.String(), "/alerting/list", kn.log) bodyJSON.Set("client_url", ruleURL) diff --git a/pkg/services/ngalert/notifier/channels/line.go b/pkg/services/ngalert/notifier/channels/line.go index 0ec73c9feb3..c66ea101ae2 100644 --- a/pkg/services/ngalert/notifier/channels/line.go +++ b/pkg/services/ngalert/notifier/channels/line.go @@ -86,7 +86,7 @@ func (ln *LineNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, e "%s\n%s\n\n%s", tmpl(DefaultMessageTitleEmbed), ruleURL, - tmpl(`{{ template "default.message" . }}`), + tmpl(DefaultMessageEmbed), ) if tmplErr != nil { ln.log.Warn("failed to template Line message", "err", tmplErr.Error()) diff --git a/pkg/services/ngalert/notifier/channels/opsgenie.go b/pkg/services/ngalert/notifier/channels/opsgenie.go index 1e55b66b158..3a64ca79219 100644 --- a/pkg/services/ngalert/notifier/channels/opsgenie.go +++ b/pkg/services/ngalert/notifier/channels/opsgenie.go @@ -205,7 +205,7 @@ func (on *OpsgenieNotifier) buildOpsgenieMessage(ctx context.Context, alerts mod "%s\n%s\n\n%s", tmpl(DefaultMessageTitleEmbed), ruleURL, - tmpl(`{{ template "default.message" . }}`), + tmpl(DefaultMessageEmbed), ) } diff --git a/pkg/services/ngalert/notifier/channels/pushover.go b/pkg/services/ngalert/notifier/channels/pushover.go index 43c107e8216..e49223b0b5f 100644 --- a/pkg/services/ngalert/notifier/channels/pushover.go +++ b/pkg/services/ngalert/notifier/channels/pushover.go @@ -106,7 +106,7 @@ func NewPushoverConfig(config *NotificationChannelConfig, decryptFunc GetDecrypt AlertingSound: config.Settings.Get("sound").MustString(), OKSound: config.Settings.Get("okSound").MustString(), Upload: config.Settings.Get("uploadImage").MustBool(true), - Message: config.Settings.Get("message").MustString(`{{ template "default.message" .}}`), + Message: config.Settings.Get("message").MustString(DefaultMessageEmbed), }, nil } diff --git a/pkg/services/ngalert/notifier/channels/sensugo.go b/pkg/services/ngalert/notifier/channels/sensugo.go index 2407e340c3e..a7adcbd85dc 100644 --- a/pkg/services/ngalert/notifier/channels/sensugo.go +++ b/pkg/services/ngalert/notifier/channels/sensugo.go @@ -72,7 +72,7 @@ func NewSensuGoConfig(config *NotificationChannelConfig, decryptFunc GetDecrypte Namespace: config.Settings.Get("namespace").MustString(), Handler: config.Settings.Get("handler").MustString(), APIKey: apikey, - Message: config.Settings.Get("message").MustString(`{{ template "default.message" .}}`), + Message: config.Settings.Get("message").MustString(DefaultMessageEmbed), }, nil } diff --git a/pkg/services/ngalert/notifier/channels/slack.go b/pkg/services/ngalert/notifier/channels/slack.go index af300fbed2a..56cae7d9cc9 100644 --- a/pkg/services/ngalert/notifier/channels/slack.go +++ b/pkg/services/ngalert/notifier/channels/slack.go @@ -101,7 +101,7 @@ func buildSlackNotifier(factoryConfig FactoryConfig) (*SlackNotifier, error) { settings.Username = "Grafana" } if settings.Text == "" { - settings.Text = `{{ template "default.message" . }}` + settings.Text = DefaultMessageEmbed } if settings.Title == "" { settings.Title = DefaultMessageTitleEmbed diff --git a/pkg/services/ngalert/notifier/channels/telegram.go b/pkg/services/ngalert/notifier/channels/telegram.go index b28c621f009..feae141eff4 100644 --- a/pkg/services/ngalert/notifier/channels/telegram.go +++ b/pkg/services/ngalert/notifier/channels/telegram.go @@ -67,7 +67,7 @@ func NewTelegramConfig(config *NotificationChannelConfig, fn GetDecryptedValueFn NotificationChannelConfig: config, BotToken: botToken, ChatID: chatID, - Message: config.Settings.Get("message").MustString(`{{ template "default.message" . }}`), + Message: config.Settings.Get("message").MustString(DefaultMessageEmbed), }, nil } diff --git a/pkg/services/ngalert/notifier/channels/threema.go b/pkg/services/ngalert/notifier/channels/threema.go index 81f53290bb5..143fa672f5c 100644 --- a/pkg/services/ngalert/notifier/channels/threema.go +++ b/pkg/services/ngalert/notifier/channels/threema.go @@ -127,7 +127,7 @@ func (tn *ThreemaNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool message := fmt.Sprintf("%s%s\n\n*Message:*\n%s\n*URL:* %s\n", stateEmoji, tmpl(DefaultMessageTitleEmbed), - tmpl(`{{ template "default.message" . }}`), + tmpl(DefaultMessageEmbed), path.Join(tn.tmpl.ExternalURL.String(), "/alerting/list"), ) diff --git a/pkg/services/ngalert/notifier/channels/victorops.go b/pkg/services/ngalert/notifier/channels/victorops.go index cfd66db7b97..56bb7355c78 100644 --- a/pkg/services/ngalert/notifier/channels/victorops.go +++ b/pkg/services/ngalert/notifier/channels/victorops.go @@ -115,7 +115,7 @@ func (vn *VictoropsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bo bodyJSON.Set("entity_id", groupKey.Hash()) bodyJSON.Set("entity_display_name", tmpl(DefaultMessageTitleEmbed)) bodyJSON.Set("timestamp", time.Now().Unix()) - bodyJSON.Set("state_message", tmpl(`{{ template "default.message" . }}`)) + bodyJSON.Set("state_message", tmpl(DefaultMessageEmbed)) bodyJSON.Set("monitoring_tool", "Grafana v"+setting.BuildVersion) _ = withStoredImages(ctx, vn.log, vn.images, diff --git a/pkg/services/ngalert/notifier/channels/webhook.go b/pkg/services/ngalert/notifier/channels/webhook.go index 326a6c4b78e..21917df1a02 100644 --- a/pkg/services/ngalert/notifier/channels/webhook.go +++ b/pkg/services/ngalert/notifier/channels/webhook.go @@ -160,7 +160,7 @@ func (wn *WebhookNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool TruncatedAlerts: numTruncated, OrgID: wn.orgID, Title: tmpl(DefaultMessageTitleEmbed), - Message: tmpl(`{{ template "default.message" . }}`), + Message: tmpl(DefaultMessageEmbed), } if types.Alerts(as...).Status() == model.AlertFiring { msg.State = string(models.AlertStateAlerting) diff --git a/pkg/services/ngalert/notifier/channels/wecom.go b/pkg/services/ngalert/notifier/channels/wecom.go index 4d25c1b8eff..762634bdf00 100644 --- a/pkg/services/ngalert/notifier/channels/wecom.go +++ b/pkg/services/ngalert/notifier/channels/wecom.go @@ -40,7 +40,7 @@ func NewWeComConfig(config *NotificationChannelConfig, decryptFunc GetDecryptedV return &WeComConfig{ NotificationChannelConfig: config, URL: url, - Message: config.Settings.Get("message").MustString(`{{ template "default.message" .}}`), + Message: config.Settings.Get("message").MustString(DefaultMessageEmbed), Title: config.Settings.Get("title").MustString(DefaultMessageTitleEmbed), }, nil } diff --git a/pkg/services/ngalert/notifier/channels_config/available_channels.go b/pkg/services/ngalert/notifier/channels_config/available_channels.go index 1fb24cd68e0..0b98633cff1 100644 --- a/pkg/services/ngalert/notifier/channels_config/available_channels.go +++ b/pkg/services/ngalert/notifier/channels_config/available_channels.go @@ -135,7 +135,7 @@ func GetAvailableNotifiers() []*NotifierPlugin { { // New in 8.0. Label: "Message", Element: ElementTypeTextArea, - Placeholder: `{{ template "default.message" . }}`, + Placeholder: channels.DefaultMessageEmbed, PropertyName: "message", }, }, @@ -263,7 +263,7 @@ func GetAvailableNotifiers() []*NotifierPlugin { Label: "Summary", Description: "You can use templates for summary", Element: ElementTypeTextArea, - Placeholder: `{{ template "default.message" . }}`, + Placeholder: channels.DefaultMessageEmbed, PropertyName: "summary", }, }, @@ -372,7 +372,7 @@ func GetAvailableNotifiers() []*NotifierPlugin { { // New in 8.0. Label: "Message", Element: ElementTypeTextArea, - Placeholder: `{{ template "default.message" . }}`, + Placeholder: channels.DefaultMessageEmbed, PropertyName: "message", }, }, @@ -550,7 +550,7 @@ func GetAvailableNotifiers() []*NotifierPlugin { { // New in 8.0. Label: "Message", Element: ElementTypeTextArea, - Placeholder: `{{ template "default.message" . }}`, + Placeholder: channels.DefaultMessageEmbed, PropertyName: "message", }, }, @@ -587,7 +587,7 @@ func GetAvailableNotifiers() []*NotifierPlugin { { // New in 8.0. Label: "Message", Element: ElementTypeTextArea, - Placeholder: `{{ template "default.message" . }}`, + Placeholder: channels.DefaultMessageEmbed, PropertyName: "message", }, }, @@ -618,7 +618,7 @@ func GetAvailableNotifiers() []*NotifierPlugin { { // New in 8.0. Label: "Message", Element: ElementTypeTextArea, - Placeholder: `{{ template "default.message" . }}`, + Placeholder: channels.DefaultMessageEmbed, PropertyName: "message", }, }, @@ -708,7 +708,7 @@ func GetAvailableNotifiers() []*NotifierPlugin { Label: "Message", Description: "Custom WeCom message. You can use template variables.", Element: ElementTypeTextArea, - Placeholder: `{{ template "default.message" . }}`, + Placeholder: channels.DefaultMessageEmbed, PropertyName: "message", }, { // New in 9.1. @@ -761,7 +761,7 @@ func GetAvailableNotifiers() []*NotifierPlugin { Description: "Mention a group using @ or a user using <@ID> when notifying in a channel", Element: ElementTypeInput, InputType: InputTypeText, - Placeholder: `{{ template "default.message" . }}`, + Placeholder: channels.DefaultMessageEmbed, PropertyName: "message", }, { @@ -803,7 +803,7 @@ func GetAvailableNotifiers() []*NotifierPlugin { { Label: "Message", Element: ElementTypeTextArea, - Placeholder: `{{ template "default.message" . }}`, + Placeholder: channels.DefaultMessageEmbed, PropertyName: "message", }, },