mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Template all possible variables in notification channels (#35749)
Signed-off-by: Ganesh Vernekar <ganeshvern@gmail.com>
This commit is contained in:
parent
e94b82c833
commit
9a5c1f06df
@ -79,7 +79,7 @@ func (dd *DingDingNotifier) Notify(ctx context.Context, as ...*types.Alert) (boo
|
|||||||
title := tmpl(`{{ template "default.title" . }}`)
|
title := tmpl(`{{ template "default.title" . }}`)
|
||||||
|
|
||||||
var bodyMsg map[string]interface{}
|
var bodyMsg map[string]interface{}
|
||||||
if dd.MsgType == "actionCard" {
|
if tmpl(dd.MsgType) == "actionCard" {
|
||||||
bodyMsg = map[string]interface{}{
|
bodyMsg = map[string]interface{}{
|
||||||
"msgtype": "actionCard",
|
"msgtype": "actionCard",
|
||||||
"actionCard": map[string]string{
|
"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 {
|
if tmplErr != nil {
|
||||||
dd.log.Debug("failed to template DingDing message", "err", tmplErr.Error())
|
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{
|
cmd := &models.SendWebhookSync{
|
||||||
Url: dd.URL,
|
Url: u,
|
||||||
Body: string(body),
|
Body: string(body),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,6 +93,7 @@ func (d DiscordNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool,
|
|||||||
|
|
||||||
bodyJSON.Set("embeds", []interface{}{embed})
|
bodyJSON.Set("embeds", []interface{}{embed})
|
||||||
|
|
||||||
|
u := tmpl(d.WebhookURL)
|
||||||
if tmplErr != nil {
|
if tmplErr != nil {
|
||||||
d.log.Debug("failed to template Discord message", "err", tmplErr.Error())
|
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
|
return false, err
|
||||||
}
|
}
|
||||||
cmd := &models.SendWebhookSync{
|
cmd := &models.SendWebhookSync{
|
||||||
Url: d.WebhookURL,
|
Url: u,
|
||||||
HttpMethod: "POST",
|
HttpMethod: "POST",
|
||||||
ContentType: "application/json",
|
ContentType: "application/json",
|
||||||
Body: string(body),
|
Body: string(body),
|
||||||
|
@ -107,6 +107,7 @@ func (gcn *GoogleChatNotifier) Notify(ctx context.Context, as ...*types.Alert) (
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u := tmpl(gcn.URL)
|
||||||
if tmplErr != nil {
|
if tmplErr != nil {
|
||||||
gcn.log.Debug("failed to template GoogleChat message", "err", tmplErr.Error())
|
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{
|
cmd := &models.SendWebhookSync{
|
||||||
Url: gcn.URL,
|
Url: u,
|
||||||
HttpMethod: "POST",
|
HttpMethod: "POST",
|
||||||
HttpHeader: map[string]string{
|
HttpHeader: map[string]string{
|
||||||
"Content-Type": "application/json; charset=UTF-8",
|
"Content-Type": "application/json; charset=UTF-8",
|
||||||
|
@ -89,16 +89,16 @@ func (kn *KafkaNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool,
|
|||||||
recordJSON := simplejson.New()
|
recordJSON := simplejson.New()
|
||||||
recordJSON.Set("records", []interface{}{valueJSON})
|
recordJSON.Set("records", []interface{}{valueJSON})
|
||||||
|
|
||||||
if tmplErr != nil {
|
|
||||||
kn.log.Debug("failed to template Kafka message", "err", tmplErr.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
body, err := recordJSON.MarshalJSON()
|
body, err := recordJSON.MarshalJSON()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
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{
|
cmd := &models.SendWebhookSync{
|
||||||
Url: topicURL,
|
Url: topicURL,
|
||||||
|
@ -201,7 +201,7 @@ func (on *OpsgenieNotifier) buildOpsgenieMessage(ctx context.Context, alerts mod
|
|||||||
|
|
||||||
bodyJSON.Set("tags", tags)
|
bodyJSON.Set("tags", tags)
|
||||||
bodyJSON.Set("details", details)
|
bodyJSON.Set("details", details)
|
||||||
apiURL = on.APIUrl
|
apiURL = tmpl(on.APIUrl)
|
||||||
|
|
||||||
if tmplErr != nil {
|
if tmplErr != nil {
|
||||||
on.log.Debug("failed to template Opsgenie message", "err", tmplErr.Error())
|
on.log.Debug("failed to template Opsgenie message", "err", tmplErr.Error())
|
||||||
|
@ -140,7 +140,7 @@ func (pn *PushoverNotifier) genPushoverBody(ctx context.Context, as ...*types.Al
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add the user token
|
// Add the user token
|
||||||
err := w.WriteField("user", pn.UserKey)
|
err := w.WriteField("user", tmpl(pn.UserKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, b, err
|
return nil, b, err
|
||||||
}
|
}
|
||||||
@ -175,16 +175,16 @@ func (pn *PushoverNotifier) genPushoverBody(ctx context.Context, as ...*types.Al
|
|||||||
|
|
||||||
// Add device
|
// Add device
|
||||||
if pn.Device != "" {
|
if pn.Device != "" {
|
||||||
err = w.WriteField("device", pn.Device)
|
err = w.WriteField("device", tmpl(pn.Device))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, b, err
|
return nil, b, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add sound
|
// Add sound
|
||||||
sound := pn.AlertingSound
|
sound := tmpl(pn.AlertingSound)
|
||||||
if alerts.Status() == model.AlertResolved {
|
if alerts.Status() == model.AlertResolved {
|
||||||
sound = pn.OKSound
|
sound = tmpl(pn.OKSound)
|
||||||
}
|
}
|
||||||
if sound != "default" {
|
if sound != "default" {
|
||||||
err = w.WriteField("sound", sound)
|
err = w.WriteField("sound", sound)
|
||||||
|
@ -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
|
// 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.
|
// value (optional), else we fallback and use the grafana rule anme and ruleID.
|
||||||
entity := sn.Entity
|
entity := tmpl(sn.Entity)
|
||||||
if entity == "" {
|
if entity == "" {
|
||||||
entity = "default"
|
entity = "default"
|
||||||
}
|
}
|
||||||
|
|
||||||
check := sn.Check
|
check := tmpl(sn.Check)
|
||||||
if check == "" {
|
if check == "" {
|
||||||
check = "default"
|
check = "default"
|
||||||
}
|
}
|
||||||
@ -94,14 +94,14 @@ func (sn *SensuGoNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool
|
|||||||
status = 2
|
status = 2
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace := sn.Namespace
|
namespace := tmpl(sn.Namespace)
|
||||||
if namespace == "" {
|
if namespace == "" {
|
||||||
namespace = "default"
|
namespace = "default"
|
||||||
}
|
}
|
||||||
|
|
||||||
var handlers []string
|
var handlers []string
|
||||||
if sn.Handler != "" {
|
if sn.Handler != "" {
|
||||||
handlers = []string{sn.Handler}
|
handlers = []string{tmpl(sn.Handler)}
|
||||||
}
|
}
|
||||||
|
|
||||||
ruleURL := joinUrlPath(sn.tmpl.ExternalURL.String(), "/alerting/list", sn.log)
|
ruleURL := joinUrlPath(sn.tmpl.ExternalURL.String(), "/alerting/list", sn.log)
|
||||||
|
@ -284,13 +284,13 @@ func (sn *SlackNotifier) buildSlackMessage(ctx context.Context, as []*types.Aler
|
|||||||
if len(sn.MentionGroups) > 0 {
|
if len(sn.MentionGroups) > 0 {
|
||||||
appendSpace()
|
appendSpace()
|
||||||
for _, g := range sn.MentionGroups {
|
for _, g := range sn.MentionGroups {
|
||||||
mentionsBuilder.WriteString(fmt.Sprintf("<!subteam^%s>", g))
|
mentionsBuilder.WriteString(fmt.Sprintf("<!subteam^%s>", tmpl(g)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(sn.MentionUsers) > 0 {
|
if len(sn.MentionUsers) > 0 {
|
||||||
appendSpace()
|
appendSpace()
|
||||||
for _, u := range sn.MentionUsers {
|
for _, u := range sn.MentionUsers {
|
||||||
mentionsBuilder.WriteString(fmt.Sprintf("<@%s>", u))
|
mentionsBuilder.WriteString(fmt.Sprintf("<@%s>", tmpl(u)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,6 +88,7 @@ func (tn *TeamsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool,
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u := tmpl(tn.URL)
|
||||||
if tmplErr != nil {
|
if tmplErr != nil {
|
||||||
tn.log.Debug("failed to template Teams message", "err", tmplErr.Error())
|
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 {
|
if err != nil {
|
||||||
return false, errors.Wrap(err, "marshal json")
|
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 {
|
if err := bus.DispatchCtx(ctx, cmd); err != nil {
|
||||||
return false, errors.Wrap(err, "send notification to Teams")
|
return false, errors.Wrap(err, "send notification to Teams")
|
||||||
|
@ -99,7 +99,7 @@ func (tn *TelegramNotifier) Notify(ctx context.Context, as ...*types.Alert) (boo
|
|||||||
return false, err
|
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{
|
cmd := &models.SendWebhookSync{
|
||||||
Url: fmt.Sprintf(TelegramAPIURL, tn.BotToken),
|
Url: fmt.Sprintf(TelegramAPIURL, tn.BotToken),
|
||||||
Body: body.String(),
|
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) {
|
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
|
var tmplErr error
|
||||||
tmpl, _ := TmplText(ctx, tn.tmpl, as, tn.log, &tmplErr)
|
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)
|
message := tmpl(tn.Message)
|
||||||
if tmplErr != nil {
|
if tmplErr != nil {
|
||||||
tn.log.Debug("failed to template Telegram message", "err", tmplErr.Error())
|
tn.log.Debug("failed to template Telegram message", "err", tmplErr.Error())
|
||||||
|
@ -44,7 +44,7 @@ func NewVictoropsNotifier(model *NotificationChannelConfig, t *template.Template
|
|||||||
Settings: model.Settings,
|
Settings: model.Settings,
|
||||||
}),
|
}),
|
||||||
URL: url,
|
URL: url,
|
||||||
MessageType: strings.ToUpper(model.Settings.Get("messageType").MustString()),
|
MessageType: model.Settings.Get("messageType").MustString(),
|
||||||
log: log.New("alerting.notifier.victorops"),
|
log: log.New("alerting.notifier.victorops"),
|
||||||
tmpl: t,
|
tmpl: t,
|
||||||
}, nil
|
}, nil
|
||||||
@ -65,7 +65,10 @@ type VictoropsNotifier struct {
|
|||||||
func (vn *VictoropsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
|
func (vn *VictoropsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
|
||||||
vn.log.Debug("Executing victorops notification", "notification", vn.Name)
|
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 == "" {
|
if messageType == "" {
|
||||||
messageType = victoropsAlertStateCritical
|
messageType = victoropsAlertStateCritical
|
||||||
}
|
}
|
||||||
@ -74,9 +77,6 @@ func (vn *VictoropsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bo
|
|||||||
messageType = victoropsAlertStateRecovery
|
messageType = victoropsAlertStateRecovery
|
||||||
}
|
}
|
||||||
|
|
||||||
var tmplErr error
|
|
||||||
tmpl, _ := TmplText(ctx, vn.tmpl, as, vn.log, &tmplErr)
|
|
||||||
|
|
||||||
groupKey, err := notify.ExtractGroupKey(ctx)
|
groupKey, err := notify.ExtractGroupKey(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
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)
|
ruleURL := joinUrlPath(vn.tmpl.ExternalURL.String(), "/alerting/list", vn.log)
|
||||||
bodyJSON.Set("alert_url", ruleURL)
|
bodyJSON.Set("alert_url", ruleURL)
|
||||||
|
|
||||||
|
u := tmpl(vn.URL)
|
||||||
if tmplErr != nil {
|
if tmplErr != nil {
|
||||||
vn.log.Debug("failed to template VictorOps message", "err", tmplErr.Error())
|
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
|
return false, err
|
||||||
}
|
}
|
||||||
cmd := &models.SendWebhookSync{
|
cmd := &models.SendWebhookSync{
|
||||||
Url: vn.URL,
|
Url: u,
|
||||||
Body: string(b),
|
Body: string(b),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user