From 6a3dfaff45fafba1ef8553451e6b1fc0435b6523 Mon Sep 17 00:00:00 2001 From: Evan Baker Date: Fri, 15 Dec 2017 06:18:05 -0500 Subject: [PATCH] Add Slack additional "fields" to notifications (#1135) * impl slack fields * wrap title and value in tmplText --- config/notifiers.go | 20 +++++++++++--------- notify/impl.go | 27 +++++++++++++++++++++------ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/config/notifiers.go b/config/notifiers.go index c454238a..e92736a7 100644 --- a/config/notifiers.go +++ b/config/notifiers.go @@ -234,15 +234,17 @@ type SlackConfig struct { Username string `yaml:"username,omitempty" json:"username,omitempty"` Color string `yaml:"color,omitempty" json:"color,omitempty"` - Title string `yaml:"title,omitempty" json:"title,omitempty"` - TitleLink string `yaml:"title_link,omitempty" json:"title_link,omitempty"` - Pretext string `yaml:"pretext,omitempty" json:"pretext,omitempty"` - Text string `yaml:"text,omitempty" json:"text,omitempty"` - Footer string `yaml:"footer,omitempty" json:"footer,omitempty"` - Fallback string `yaml:"fallback,omitempty" json:"fallback,omitempty"` - IconEmoji string `yaml:"icon_emoji,omitempty" json:"icon_emoji,omitempty"` - IconURL string `yaml:"icon_url,omitempty" json:"icon_url,omitempty"` - LinkNames bool `yaml:"link_names,omitempty" json:"link_names,omitempty"` + Title string `yaml:"title,omitempty" json:"title,omitempty"` + TitleLink string `yaml:"title_link,omitempty" json:"title_link,omitempty"` + Pretext string `yaml:"pretext,omitempty" json:"pretext,omitempty"` + Text string `yaml:"text,omitempty" json:"text,omitempty"` + Fields []map[string]string `yaml:"fields,omitempty" json:"fields,omitempty"` + ShortFields bool `yaml:"short_fields,omitempty" json:"short_fields,omitempty"` + Footer string `yaml:"footer,omitempty" json:"footer,omitempty"` + Fallback string `yaml:"fallback,omitempty" json:"fallback,omitempty"` + IconEmoji string `yaml:"icon_emoji,omitempty" json:"icon_emoji,omitempty"` + IconURL string `yaml:"icon_url,omitempty" json:"icon_url,omitempty"` + LinkNames bool `yaml:"link_names,omitempty" json:"link_names,omitempty"` // Catches all undefined fields and must be empty after parsing. XXX map[string]interface{} `yaml:",inline" json:"-"` diff --git a/notify/impl.go b/notify/impl.go index f7efd1af..506acdf3 100644 --- a/notify/impl.go +++ b/notify/impl.go @@ -626,12 +626,13 @@ type slackReq struct { // slackAttachment is used to display a richly-formatted message block. type slackAttachment struct { - Title string `json:"title,omitempty"` - TitleLink string `json:"title_link,omitempty"` - Pretext string `json:"pretext,omitempty"` - Text string `json:"text"` - Fallback string `json:"fallback"` - Footer string `json:"footer"` + Title string `json:"title,omitempty"` + TitleLink string `json:"title_link,omitempty"` + Pretext string `json:"pretext,omitempty"` + Text string `json:"text"` + Fallback string `json:"fallback"` + Fields []slackAttachmentField `json:"fields"` + Footer string `json:"footer"` Color string `json:"color,omitempty"` MrkdwnIn []string `json:"mrkdwn_in,omitempty"` @@ -662,6 +663,20 @@ func (n *Slack) Notify(ctx context.Context, as ...*types.Alert) (bool, error) { Color: tmplText(n.conf.Color), MrkdwnIn: []string{"fallback", "pretext", "text"}, } + + var numFields = len(n.conf.Fields) + if numFields > 0 { + var fields = make([]slackAttachmentField, numFields) + for k, v := range n.conf.Fields { + fields[k] = slackAttachmentField{ + tmplText(v["title"]), + tmplText(v["value"]), + n.conf.ShortFields, + } + } + attachment.Fields = fields + } + req := &slackReq{ Channel: tmplText(n.conf.Channel), Username: tmplText(n.conf.Username),