diff --git a/config/samples/template.yaml b/config/samples/template.yaml
index d5a96e9..4f543bb 100644
--- a/config/samples/template.yaml
+++ b/config/samples/template.yaml
@@ -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 }}
@@ -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" }}
@@ -50,6 +58,7 @@ data:
{{ 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 }}
diff --git a/helm/templates/template.yaml b/helm/templates/template.yaml
index 9986bb5..c0a6ea8 100644
--- a/helm/templates/template.yaml
+++ b/helm/templates/template.yaml
@@ -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 }}" }}
@@ -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\" }}" }}
{{ " " }}
@@ -50,6 +58,7 @@ data:
{{ " " }}
{{ " {{ 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 }}" }}
diff --git a/pkg/notify/notifier/webhook/webhook.go b/pkg/notify/notifier/webhook/webhook.go
index 85be03b..abd99e7 100644
--- a/pkg/notify/notifier/webhook/webhook.go
+++ b/pkg/notify/notifier/webhook/webhook.go
@@ -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
diff --git a/pkg/template/types.go b/pkg/template/types.go
index aaeeb2b..53782a2 100644
--- a/pkg/template/types.go
+++ b/pkg/template/types.go
@@ -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 {
@@ -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))
@@ -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 {
@@ -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]
@@ -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]