From e35efd837b99f4a83a319785e2d9d795eb64d3c0 Mon Sep 17 00:00:00 2001 From: wanjunlei Date: Fri, 17 Nov 2023 17:12:47 +0800 Subject: [PATCH] support send notification history without adaptor Signed-off-by: wanjunlei --- pkg/constants/constants.go | 1 + pkg/controller/controller.go | 7 ++- pkg/notify/notifier/webhook/webhook.go | 60 +++++++++++++++++++++++--- pkg/template/template.go | 11 +++-- 4 files changed, 65 insertions(+), 14 deletions(-) diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index b9f64b12..8d93ec64 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -42,6 +42,7 @@ const ( Notification = "notification" DefaultWebhookTemplate = `{{ template "webhook.default.message" . }}` + DefaultHistoryTemplate = `{{ template "nm.default.history" . }}` DefaultClusterName = "default" diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 63551f6a..d07896f4 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -957,8 +957,11 @@ func (c *Controller) GetHistoryReceivers() []internal.Receiver { Webhook: c.history.Webhook, }, } - tmpl := constants.DefaultWebhookTemplate - receiver.Spec.Webhook.Template = &tmpl + if receiver.Spec.Webhook.Template == nil || *receiver.Spec.Webhook.Template == "" { + tmpl := constants.DefaultWebhookTemplate + receiver.Spec.Webhook.Template = &tmpl + } + historyReceivers := NewReceivers("", receiver) if historyReceivers != nil { for _, v := range historyReceivers { diff --git a/pkg/notify/notifier/webhook/webhook.go b/pkg/notify/notifier/webhook/webhook.go index c2c9a7d8..67d146ef 100644 --- a/pkg/notify/notifier/webhook/webhook.go +++ b/pkg/notify/notifier/webhook/webhook.go @@ -24,6 +24,15 @@ import ( const ( DefaultSendTimeout = time.Second * 5 + + Status = "status" + StartsAt = "startsAt" + EndsAt = "endsAt" + NotificationTime = "notificationTime" + RunbookURL = "runbook_url" + Message = "message" + Summary = "summary" + SummaryCn = "summaryCn" ) type Notifier struct { @@ -88,7 +97,18 @@ func (n *Notifier) Notify(ctx context.Context, data *template.Data) error { }() var buf bytes.Buffer - if n.receiver.TmplName != constants.DefaultWebhookTemplate { + if n.tmpl.Transform(n.receiver.TmplName) == constants.DefaultWebhookTemplate { + if err := utils.JsonEncode(&buf, data); err != nil { + _ = level.Error(n.logger).Log("msg", "WebhookNotifier: encode message error", "error", err.Error()) + return err + } + } else if n.tmpl.Transform(n.receiver.TmplName) == constants.DefaultHistoryTemplate { + // just for notification history + if err := generateNotificationHistory(&buf, data); err != nil { + _ = level.Error(n.logger).Log("msg", "WebhookNotifier: generate notification history error", "error", err.Error()) + return err + } + } else { msg, err := n.tmpl.Text(n.receiver.TmplName, data) if err != nil { _ = level.Error(n.logger).Log("msg", "WebhookNotifier: generate message error", "error", err.Error()) @@ -96,11 +116,6 @@ func (n *Notifier) Notify(ctx context.Context, data *template.Data) error { } buf.WriteString(msg) - } else { - if err := utils.JsonEncode(&buf, data); err != nil { - _ = level.Error(n.logger).Log("msg", "WebhookNotifier: encode message error", "error", err.Error()) - return err - } } request, err := http.NewRequest(http.MethodPost, n.receiver.URL, &buf) @@ -156,7 +171,40 @@ func (n *Notifier) Notify(ctx context.Context, data *template.Data) error { _ = level.Debug(n.logger).Log("msg", "WebhookNotifier: send message", "to", n.receiver.URL) return nil +} + +func generateNotificationHistory(buf *bytes.Buffer, data *template.Data) error { + var res []interface{} + for _, alert := range data.Alerts { + m := make(map[string]interface{}) + m[Status] = alert.Status + m[StartsAt] = alert.StartsAt + m[EndsAt] = alert.EndsAt + m[NotificationTime] = time.Now() + + for k, v := range alert.Labels { + m[k] = v + } + + for k, v := range alert.Annotations { + if k != RunbookURL && k != Message && k != Summary && k != SummaryCn { + m[k] = v + } + } + + message := alert.Annotations[Message] + if message == "" { + message = alert.Annotations[Summary] + if message == "" { + message = alert.Annotations[SummaryCn] + } + } + m[Message] = message + + res = append(res, m) + } + return utils.JsonEncode(buf, res) } func (n *Notifier) getTransport(r *webhook.Receiver) (http.RoundTripper, error) { diff --git a/pkg/template/template.go b/pkg/template/template.go index fe5a6a29..229d55bf 100644 --- a/pkg/template/template.go +++ b/pkg/template/template.go @@ -162,11 +162,10 @@ func (t *Template) Text(name string, data *Data) (string, error) { if err != nil { return "", err } - tmpl, err = tmpl.New("").Option("missingkey=zero").Parse(t.transform(name)) + tmpl, err = tmpl.New("").Option("missingkey=zero").Parse(t.Transform(name)) if err != nil { return "", err } - var buf bytes.Buffer if err = tmpl.Execute(&buf, data); err != nil { return "", err @@ -185,7 +184,7 @@ func (t *Template) Html(name string, data *Data) (string, error) { if err != nil { return "", err } - tmpl, err = tmpl.New("").Option("missingkey=zero").Parse(t.transform(name)) + tmpl, err = tmpl.New("").Option("missingkey=zero").Parse(t.Transform(name)) if err != nil { return "", err } @@ -217,7 +216,7 @@ func cleanSuffix(s string) string { return s } -func (t *Template) transform(name string) string { +func (t *Template) Transform(name string) string { n := strings.ReplaceAll(name, " ", "") @@ -248,14 +247,14 @@ func (t *Template) Split(data *Data, maxSize int, templateName string, subjectTe } d.Alerts = append(d.Alerts, data.Alerts[i]) - msg, err := t.Text(t.transform(templateName), d.Format()) + msg, err := t.Text(t.Transform(templateName), d.Format()) if err != nil { return nil, err } title := "" if subjectTemplateName != "" { - title, err = t.Text(t.transform(subjectTemplateName), d) + title, err = t.Text(t.Transform(subjectTemplateName), d) if err != nil { return nil, err }