Add Slack additional "fields" to notifications (#1135)

* impl slack fields

* wrap title and value in tmplText
This commit is contained in:
Evan Baker 2017-12-15 06:18:05 -05:00 committed by stuart nelson
parent d75ff37a38
commit 6a3dfaff45
2 changed files with 32 additions and 15 deletions

View File

@ -238,6 +238,8 @@ type SlackConfig struct {
TitleLink string `yaml:"title_link,omitempty" json:"title_link,omitempty"` TitleLink string `yaml:"title_link,omitempty" json:"title_link,omitempty"`
Pretext string `yaml:"pretext,omitempty" json:"pretext,omitempty"` Pretext string `yaml:"pretext,omitempty" json:"pretext,omitempty"`
Text string `yaml:"text,omitempty" json:"text,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"` Footer string `yaml:"footer,omitempty" json:"footer,omitempty"`
Fallback string `yaml:"fallback,omitempty" json:"fallback,omitempty"` Fallback string `yaml:"fallback,omitempty" json:"fallback,omitempty"`
IconEmoji string `yaml:"icon_emoji,omitempty" json:"icon_emoji,omitempty"` IconEmoji string `yaml:"icon_emoji,omitempty" json:"icon_emoji,omitempty"`

View File

@ -631,6 +631,7 @@ type slackAttachment struct {
Pretext string `json:"pretext,omitempty"` Pretext string `json:"pretext,omitempty"`
Text string `json:"text"` Text string `json:"text"`
Fallback string `json:"fallback"` Fallback string `json:"fallback"`
Fields []slackAttachmentField `json:"fields"`
Footer string `json:"footer"` Footer string `json:"footer"`
Color string `json:"color,omitempty"` Color string `json:"color,omitempty"`
@ -662,6 +663,20 @@ func (n *Slack) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
Color: tmplText(n.conf.Color), Color: tmplText(n.conf.Color),
MrkdwnIn: []string{"fallback", "pretext", "text"}, 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{ req := &slackReq{
Channel: tmplText(n.conf.Channel), Channel: tmplText(n.conf.Channel),
Username: tmplText(n.conf.Username), Username: tmplText(n.conf.Username),