diff --git a/pkg/services/ngalert/notifier/channels/dingding.go b/pkg/services/ngalert/notifier/channels/dingding.go index d474c8a022a..40364544011 100644 --- a/pkg/services/ngalert/notifier/channels/dingding.go +++ b/pkg/services/ngalert/notifier/channels/dingding.go @@ -79,7 +79,7 @@ func (dd *DingDingNotifier) Notify(ctx context.Context, as ...*types.Alert) (boo title := tmpl(`{{ template "default.title" . }}`) var bodyMsg map[string]interface{} - if dd.MsgType == "actionCard" { + if tmpl(dd.MsgType) == "actionCard" { bodyMsg = map[string]interface{}{ "msgtype": "actionCard", "actionCard": map[string]string{ @@ -102,6 +102,7 @@ func (dd *DingDingNotifier) Notify(ctx context.Context, as ...*types.Alert) (boo } } + u := tmpl(dd.URL) if tmplErr != nil { dd.log.Debug("failed to template DingDing message", "err", tmplErr.Error()) } @@ -112,7 +113,7 @@ func (dd *DingDingNotifier) Notify(ctx context.Context, as ...*types.Alert) (boo } cmd := &models.SendWebhookSync{ - Url: dd.URL, + Url: u, Body: string(body), } diff --git a/pkg/services/ngalert/notifier/channels/discord.go b/pkg/services/ngalert/notifier/channels/discord.go index f11d9f4da34..08c3329ddec 100644 --- a/pkg/services/ngalert/notifier/channels/discord.go +++ b/pkg/services/ngalert/notifier/channels/discord.go @@ -93,6 +93,7 @@ func (d DiscordNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, bodyJSON.Set("embeds", []interface{}{embed}) + u := tmpl(d.WebhookURL) if tmplErr != nil { d.log.Debug("failed to template Discord message", "err", tmplErr.Error()) } @@ -102,7 +103,7 @@ func (d DiscordNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, return false, err } cmd := &models.SendWebhookSync{ - Url: d.WebhookURL, + Url: u, HttpMethod: "POST", ContentType: "application/json", Body: string(body), diff --git a/pkg/services/ngalert/notifier/channels/googlechat.go b/pkg/services/ngalert/notifier/channels/googlechat.go index 7e731874272..05abf571bb7 100644 --- a/pkg/services/ngalert/notifier/channels/googlechat.go +++ b/pkg/services/ngalert/notifier/channels/googlechat.go @@ -107,6 +107,7 @@ func (gcn *GoogleChatNotifier) Notify(ctx context.Context, as ...*types.Alert) ( }, } + u := tmpl(gcn.URL) if tmplErr != nil { gcn.log.Debug("failed to template GoogleChat message", "err", tmplErr.Error()) } @@ -117,7 +118,7 @@ func (gcn *GoogleChatNotifier) Notify(ctx context.Context, as ...*types.Alert) ( } cmd := &models.SendWebhookSync{ - Url: gcn.URL, + Url: u, HttpMethod: "POST", HttpHeader: map[string]string{ "Content-Type": "application/json; charset=UTF-8", diff --git a/pkg/services/ngalert/notifier/channels/kafka.go b/pkg/services/ngalert/notifier/channels/kafka.go index 7993dbb5fbf..677086c2e7d 100644 --- a/pkg/services/ngalert/notifier/channels/kafka.go +++ b/pkg/services/ngalert/notifier/channels/kafka.go @@ -89,16 +89,16 @@ func (kn *KafkaNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, recordJSON := simplejson.New() recordJSON.Set("records", []interface{}{valueJSON}) - if tmplErr != nil { - kn.log.Debug("failed to template Kafka message", "err", tmplErr.Error()) - } - body, err := recordJSON.MarshalJSON() if err != nil { return false, err } - topicURL := strings.TrimRight(kn.Endpoint, "/") + "/topics/" + kn.Topic + topicURL := strings.TrimRight(kn.Endpoint, "/") + "/topics/" + tmpl(kn.Topic) + + if tmplErr != nil { + kn.log.Debug("failed to template Kafka message", "err", tmplErr.Error()) + } cmd := &models.SendWebhookSync{ Url: topicURL, diff --git a/pkg/services/ngalert/notifier/channels/opsgenie.go b/pkg/services/ngalert/notifier/channels/opsgenie.go index fb588e19345..f2227faa2b8 100644 --- a/pkg/services/ngalert/notifier/channels/opsgenie.go +++ b/pkg/services/ngalert/notifier/channels/opsgenie.go @@ -201,7 +201,7 @@ func (on *OpsgenieNotifier) buildOpsgenieMessage(ctx context.Context, alerts mod bodyJSON.Set("tags", tags) bodyJSON.Set("details", details) - apiURL = on.APIUrl + apiURL = tmpl(on.APIUrl) if tmplErr != nil { on.log.Debug("failed to template Opsgenie message", "err", tmplErr.Error()) diff --git a/pkg/services/ngalert/notifier/channels/pushover.go b/pkg/services/ngalert/notifier/channels/pushover.go index f37aca440ba..c9554a0dc8d 100644 --- a/pkg/services/ngalert/notifier/channels/pushover.go +++ b/pkg/services/ngalert/notifier/channels/pushover.go @@ -140,7 +140,7 @@ func (pn *PushoverNotifier) genPushoverBody(ctx context.Context, as ...*types.Al } // Add the user token - err := w.WriteField("user", pn.UserKey) + err := w.WriteField("user", tmpl(pn.UserKey)) if err != nil { return nil, b, err } @@ -175,16 +175,16 @@ func (pn *PushoverNotifier) genPushoverBody(ctx context.Context, as ...*types.Al // Add device if pn.Device != "" { - err = w.WriteField("device", pn.Device) + err = w.WriteField("device", tmpl(pn.Device)) if err != nil { return nil, b, err } } // Add sound - sound := pn.AlertingSound + sound := tmpl(pn.AlertingSound) if alerts.Status() == model.AlertResolved { - sound = pn.OKSound + sound = tmpl(pn.OKSound) } if sound != "default" { err = w.WriteField("sound", sound) diff --git a/pkg/services/ngalert/notifier/channels/sensugo.go b/pkg/services/ngalert/notifier/channels/sensugo.go index 902dc8c6687..43912aabeb1 100644 --- a/pkg/services/ngalert/notifier/channels/sensugo.go +++ b/pkg/services/ngalert/notifier/channels/sensugo.go @@ -77,12 +77,12 @@ func (sn *SensuGoNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool // 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. - entity := sn.Entity + entity := tmpl(sn.Entity) if entity == "" { entity = "default" } - check := sn.Check + check := tmpl(sn.Check) if check == "" { check = "default" } @@ -94,14 +94,14 @@ func (sn *SensuGoNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool status = 2 } - namespace := sn.Namespace + namespace := tmpl(sn.Namespace) if namespace == "" { namespace = "default" } var handlers []string if sn.Handler != "" { - handlers = []string{sn.Handler} + handlers = []string{tmpl(sn.Handler)} } ruleURL := joinUrlPath(sn.tmpl.ExternalURL.String(), "/alerting/list", sn.log) diff --git a/pkg/services/ngalert/notifier/channels/slack.go b/pkg/services/ngalert/notifier/channels/slack.go index 21cc0078979..29d8a65b8aa 100644 --- a/pkg/services/ngalert/notifier/channels/slack.go +++ b/pkg/services/ngalert/notifier/channels/slack.go @@ -284,13 +284,13 @@ func (sn *SlackNotifier) buildSlackMessage(ctx context.Context, as []*types.Aler if len(sn.MentionGroups) > 0 { appendSpace() for _, g := range sn.MentionGroups { - mentionsBuilder.WriteString(fmt.Sprintf("", g)) + mentionsBuilder.WriteString(fmt.Sprintf("", tmpl(g))) } } if len(sn.MentionUsers) > 0 { appendSpace() for _, u := range sn.MentionUsers { - mentionsBuilder.WriteString(fmt.Sprintf("<@%s>", u)) + mentionsBuilder.WriteString(fmt.Sprintf("<@%s>", tmpl(u))) } } diff --git a/pkg/services/ngalert/notifier/channels/teams.go b/pkg/services/ngalert/notifier/channels/teams.go index bcb005b4665..8d43cd0121f 100644 --- a/pkg/services/ngalert/notifier/channels/teams.go +++ b/pkg/services/ngalert/notifier/channels/teams.go @@ -88,6 +88,7 @@ func (tn *TeamsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, }, } + u := tmpl(tn.URL) if tmplErr != nil { tn.log.Debug("failed to template Teams message", "err", tmplErr.Error()) } @@ -96,7 +97,7 @@ func (tn *TeamsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, if err != nil { return false, errors.Wrap(err, "marshal json") } - cmd := &models.SendWebhookSync{Url: tn.URL, Body: string(b)} + cmd := &models.SendWebhookSync{Url: u, Body: string(b)} if err := bus.DispatchCtx(ctx, cmd); err != nil { return false, errors.Wrap(err, "send notification to Teams") diff --git a/pkg/services/ngalert/notifier/channels/telegram.go b/pkg/services/ngalert/notifier/channels/telegram.go index 9b909d188fd..cb439433cba 100644 --- a/pkg/services/ngalert/notifier/channels/telegram.go +++ b/pkg/services/ngalert/notifier/channels/telegram.go @@ -99,7 +99,7 @@ func (tn *TelegramNotifier) Notify(ctx context.Context, as ...*types.Alert) (boo return false, err } - tn.log.Info("sending telegram notification", "chat_id", tn.ChatID) + tn.log.Info("sending telegram notification", "chat_id", msg["chat_id"]) cmd := &models.SendWebhookSync{ Url: fmt.Sprintf(TelegramAPIURL, tn.BotToken), Body: body.String(), @@ -118,13 +118,13 @@ func (tn *TelegramNotifier) Notify(ctx context.Context, as ...*types.Alert) (boo } func (tn *TelegramNotifier) buildTelegramMessage(ctx context.Context, as []*types.Alert) (map[string]string, error) { - msg := map[string]string{} - msg["chat_id"] = tn.ChatID - msg["parse_mode"] = "html" - var tmplErr error tmpl, _ := TmplText(ctx, tn.tmpl, as, tn.log, &tmplErr) + msg := map[string]string{} + msg["chat_id"] = tmpl(tn.ChatID) + msg["parse_mode"] = "html" + message := tmpl(tn.Message) if tmplErr != nil { tn.log.Debug("failed to template Telegram message", "err", tmplErr.Error()) diff --git a/pkg/services/ngalert/notifier/channels/victorops.go b/pkg/services/ngalert/notifier/channels/victorops.go index 7b0a5bced01..eef86c2984b 100644 --- a/pkg/services/ngalert/notifier/channels/victorops.go +++ b/pkg/services/ngalert/notifier/channels/victorops.go @@ -44,7 +44,7 @@ func NewVictoropsNotifier(model *NotificationChannelConfig, t *template.Template Settings: model.Settings, }), URL: url, - MessageType: strings.ToUpper(model.Settings.Get("messageType").MustString()), + MessageType: model.Settings.Get("messageType").MustString(), log: log.New("alerting.notifier.victorops"), tmpl: t, }, nil @@ -65,7 +65,10 @@ type VictoropsNotifier struct { func (vn *VictoropsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) { vn.log.Debug("Executing victorops notification", "notification", vn.Name) - messageType := vn.MessageType + var tmplErr error + tmpl, _ := TmplText(ctx, vn.tmpl, as, vn.log, &tmplErr) + + messageType := strings.ToUpper(tmpl(vn.MessageType)) if messageType == "" { messageType = victoropsAlertStateCritical } @@ -74,9 +77,6 @@ func (vn *VictoropsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bo messageType = victoropsAlertStateRecovery } - var tmplErr error - tmpl, _ := TmplText(ctx, vn.tmpl, as, vn.log, &tmplErr) - groupKey, err := notify.ExtractGroupKey(ctx) if err != nil { return false, err @@ -93,6 +93,7 @@ func (vn *VictoropsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bo ruleURL := joinUrlPath(vn.tmpl.ExternalURL.String(), "/alerting/list", vn.log) bodyJSON.Set("alert_url", ruleURL) + u := tmpl(vn.URL) if tmplErr != nil { vn.log.Debug("failed to template VictorOps message", "err", tmplErr.Error()) } @@ -102,7 +103,7 @@ func (vn *VictoropsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bo return false, err } cmd := &models.SendWebhookSync{ - Url: vn.URL, + Url: u, Body: string(b), }