Skip to content

Commit

Permalink
Merge pull request #239 from wanjunlei/release-2.5
Browse files Browse the repository at this point in the history
add annotation of alert to notification
  • Loading branch information
benjaminhuo authored Jan 26, 2024
2 parents c3fc735 + a321514 commit a7b9620
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
11 changes: 10 additions & 1 deletion config/samples/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ data:
{{ define "nm.default.text" }}{{ range .Alerts }}{{ template "nm.default.message" . }}
{{ range .Labels.SortedPairs }} {{ .Name | translate }}: {{ .Value }}
{{ end }}{{ range .Annotations.SortedPairs.Filter "runbook_url" "message" "summary" "summary_cn" }} {{ .Name | translate }}: {{ .Value }}
{{ end }}
{{ end }}{{- end }}
{{ define "nm.default.markdown" }}{{ range .Alerts }}### {{ template "nm.default.message" . }}
{{ range .Labels.SortedPairs }}- {{ .Name | translate }}: {{ .Value }}
{{ end }}{{ range .Annotations.SortedPairs.Filter "runbook_url" "message" "summary" "summary_cn" }}- {{ .Name | translate }}: {{ .Value }}
{{ end }}
{{ end }}{{- end }}
Expand All @@ -26,12 +28,18 @@ data:
- - tag: text
text: ' {{ .Name | translate }}: {{ .Value }}'
{{- end }}
{{ if ne (len .Annotations.SortedPairs.Filter) 0 }}
{{ range .Annotations.SortedPairs.Filter "runbook_url" "message" "summary" "summary_cn" }}
- - tag: text
text: ' {{ .Name | translate }}: {{ .Value }}'
{{- end }}
{{- end }}
- - tag: text
text: ' '
{{- end }}
{{- end }}
{{ define "nm.feishu.text" }}{{ range .Alerts }}{{ template "nm.default.message" . }}{{ "\n" }}{{ range .Labels.SortedPairs }} {{ .Name | translate }}: {{ .Value }}{{ "\n" }}{{ end }}{{ "\n" }}{{ end }}{{- end }}
{{ define "nm.feishu.text" }}{{ range .Alerts }}{{ template "nm.default.message" . }}{{ "\n" }}{{ range .Labels.SortedPairs }} {{ .Name | translate }}: {{ .Value }}{{ "\n" }}{{ end }}{{ range .Annotations.SortedPairs.Filter "runbook_url" "message" "summary" "summary_cn" }} {{ .Name | translate }}: {{ .Value }}{{ "\n" }}{{ end }}{{ "\n" }}{{ end }}{{- end }}
{{ define "nm.default.html" }}
<html xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
Expand All @@ -50,6 +58,7 @@ data:
<td style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
<strong style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">{{ template "nm.default.message" . }}</strong><br style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;" />
{{ range .Labels.SortedPairs }}&nbsp;&nbsp;{{ .Name | translate }}: {{ .Value }}<br style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;" />{{ end }}
{{ range .Annotations.SortedPairs.Filter "runbook_url" "message" "summary" "summary_cn" }}&nbsp;&nbsp;{{ .Name | translate }}: {{ .Value }}<br style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;" />{{ end }}
</td>
</tr>
{{ end }}
Expand Down
11 changes: 10 additions & 1 deletion helm/templates/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ data:
{{ "{{ define \"nm.default.text\" }}{{ range .Alerts }}{{ template \"nm.default.message\" . }}" }}
{{ "{{ range .Labels.SortedPairs }} {{ .Name | translate }}: {{ .Value }}" }}
{{ "{{ end }}{{ range .Annotations.SortedPairs.Filter \"runbook_url\" \"message\" \"summary\" \"summary_cn\" }} {{ .Name | translate }}: {{ .Value }}" }}
{{ "{{ end }}" }}
{{ "{{ end }}{{- end }}" }}
{{ "{{ define \"nm.default.markdown\" }}{{ range .Alerts }}### {{ template \"nm.default.message\" . }}" }}
{{ "{{ range .Labels.SortedPairs }}- {{ .Name | translate }}: {{ .Value }}" }}
{{ "{{ end }}{{ range .Annotations.SortedPairs.Filter \"runbook_url\" \"message\" \"summary\" \"summary_cn\" }}- {{ .Name | translate }}: {{ .Value }}" }}
{{ "{{ end }}" }}
{{ "{{ end }}{{- end }}" }}
Expand All @@ -26,12 +28,18 @@ data:
{{ " - - tag: text" }}
{{ " text: ' {{ .Name | translate }}: {{ .Value }}'" }}
{{ " {{- end }}" }}
{{ " {{ if ne (len .Annotations.SortedPairs.Filter) 0 }}" }}
{{ " {{ range .Annotations.SortedPairs.Filter \"runbook_url\" \"message\" \"summary\" \"summary_cn\" }}" }}
{{ " - - tag: text" }}
{{ " text: ' {{ .Name | translate }}: {{ .Value }}'" }}
{{ " {{- end }}" }}
{{ " {{- end }}" }}
{{ " - - tag: text" }}
{{ " text: ' '" }}
{{ " {{- end }}" }}
{{ "{{- end }}" }}
{{ "{{ define \"nm.feishu.text\" }}{{ range .Alerts }}{{ template \"nm.default.message\" . }}{{ \"\\n\" }}{{ range .Labels.SortedPairs }} {{ .Name | translate }}: {{ .Value }}{{ \"\\n\" }}{{ end }}{{ \"\\n\" }}{{ end }}{{- end }}" }}
{{ "{{ define \"nm.feishu.text\" }}{{ range .Alerts }}{{ template \"nm.default.message\" . }}{{ \"\\n\" }}{{ range .Labels.SortedPairs }} {{ .Name | translate }}: {{ .Value }}{{ \"\\n\" }}{{ end }}{{ range .Annotations.SortedPairs.Filter \"runbook_url\" \"message\" \"summary\" \"summary_cn\" }} {{ .Name | translate }}: {{ .Value }}{{ \"\\n\" }}{{ end }}{{ \"\\n\" }}{{ end }}{{- end }}" }}
{{ "{{ define \"nm.default.html\" }}" }}
{{ " <html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns=\"http://www.w3.org/1999/xhtml\" style=\"font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;\">" }}
Expand All @@ -50,6 +58,7 @@ data:
{{ " <td style=\"font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;\" valign=\"top\">" }}
{{ " <strong style=\"font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;\">{{ template \"nm.default.message\" . }}</strong><br style=\"font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;\" />" }}
{{ " {{ range .Labels.SortedPairs }}&nbsp;&nbsp;{{ .Name | translate }}: {{ .Value }}<br style=\"font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;\" />{{ end }}" }}
{{ " {{ range .Annotations.SortedPairs.Filter \"runbook_url\" \"message\" \"summary\" \"summary_cn\" }}&nbsp;&nbsp;{{ .Name | translate }}: {{ .Value }}<br style=\"font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;\" />{{ end }}" }}
{{ " </td>" }}
{{ " </tr>" }}
{{ " {{ end }}" }}
Expand Down
18 changes: 6 additions & 12 deletions pkg/notify/notifier/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,25 +180,19 @@ func generateNotificationHistory(buf *bytes.Buffer, data *template.Data) error {
m[StartsAt] = alert.StartsAt
m[EndsAt] = alert.EndsAt
m[NotificationTime] = time.Now()
m[constants.AlertMessage] = alert.Message()

for k, v := range alert.Labels {
m[k] = v
}

for k, v := range alert.Annotations {
if k != RunbookURL && k != Message && k != Summary && k != SummaryCn {
if alert.Labels != nil {
for k, v := range alert.Labels {
m[k] = v
}
}

message := alert.Annotations[Message]
if message == "" {
message = alert.Annotations[Summary]
if message == "" {
message = alert.Annotations[SummaryCn]
if alert.Annotations != nil {
for _, p := range alert.Annotations.SortedPairs().DefaultFilter() {
m[p.Name] = p.Value
}
}
m[Message] = message

if err := utils.JsonEncode(buf, m); err != nil {
return err
Expand Down
26 changes: 24 additions & 2 deletions pkg/template/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

var (
keysNeedToHiden = []string{"rule_id"}
labelNeedToHiden = []string{"rule_id"}
annotationNeedToHiden = []string{"runbook_url", "message", "summary", "summary_cn"}
)

type Data struct {
Expand Down Expand Up @@ -82,6 +83,21 @@ type Pair struct {
// Pairs is a list of key/value string pairs.
type Pairs []Pair

func (ps Pairs) DefaultFilter() Pairs {
return ps.Filter(append(labelNeedToHiden, annotationNeedToHiden...)...)
}

func (ps Pairs) Filter(keys ...string) Pairs {
for i := 0; i < len(ps); i++ {
if utils.StringInList(ps[i].Name, keys) {
ps = append(ps[:i], ps[i+1:]...)
i--
}
}

return ps
}

// Names returns a list of names of the pairs.
func (ps Pairs) Names() []string {
ns := make([]string, 0, len(ps))
Expand Down Expand Up @@ -111,7 +127,7 @@ func (kv KV) SortedPairs() Pairs {
sortStart = 0
)
for k := range kv {
if utils.StringInList(k, keysNeedToHiden) {
if utils.StringInList(k, labelNeedToHiden) {
continue
}
if k == constants.AlertName {
Expand Down Expand Up @@ -176,6 +192,9 @@ type Alert struct {
}

func (a *Alert) Message() string {
if a.Annotations == nil {
return ""
}
message := a.Annotations[constants.AlertMessage]
if utils.StringIsNil(message) {
message = a.Annotations[constants.AlertSummary]
Expand All @@ -188,6 +207,9 @@ func (a *Alert) Message() string {
}

func (a *Alert) MessageCN() string {
if a.Annotations == nil {
return ""
}
message := a.Annotations[constants.AlertSummaryCN]
if utils.StringIsNil(message) {
message = a.Annotations[constants.AlertMessage]
Expand Down

0 comments on commit a7b9620

Please sign in to comment.