Skip to content

Commit

Permalink
Merge pull request #227 from wanjunlei/master
Browse files Browse the repository at this point in the history
support send notification history without adaptor
  • Loading branch information
benjaminhuo authored Nov 20, 2023
2 parents ab15448 + e35efd8 commit 4ce1c9e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
Notification = "notification"

DefaultWebhookTemplate = `{{ template "webhook.default.message" . }}`
DefaultHistoryTemplate = `{{ template "nm.default.history" . }}`

DefaultClusterName = "default"

Expand Down
7 changes: 5 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
60 changes: 54 additions & 6 deletions pkg/notify/notifier/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -88,19 +97,25 @@ 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())
return err
}

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)
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 5 additions & 6 deletions pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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, " ", "")

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 4ce1c9e

Please sign in to comment.