Merge pull request #165 from prometheus/slack

Improve Slack integration
This commit is contained in:
Fabian Reinartz 2015-11-30 14:01:10 +01:00
commit 7465e4abe5
3 changed files with 25 additions and 20 deletions

View File

@ -48,9 +48,10 @@ var (
// DefaultSlackConfig defines default values for Slack configurations.
DefaultSlackConfig = SlackConfig{
Color: `{{ if eq .Status "firing" }}warning{{ else }}good{{ end }}`,
Color: `{{ if eq .Status "firing" }}danger{{ else }}good{{ end }}`,
Username: `{{ template "slack.default.username" . }}`,
Title: `{{ template "slack.default.title" . }}`,
TitleLink: `{{ template "slack.default.title_link" . }}`,
TitleLink: `{{ template "slack.default.titlelink" . }}`,
Pretext: `{{ template "slack.default.pretext" . }}`,
Text: `{{ template "slack.default.text" . }}`,
Fallback: `{{ template "slack.default.fallback" . }}`,
@ -249,10 +250,9 @@ type SlackConfig struct {
URL string `yaml:"url"`
// Slack channel override, (like #other-channel or @username).
Channel string `yaml:"channel"`
// The message color.
Color string `yaml:"color"`
Channel string `yaml:"channel"`
Username string `yaml:"username"`
Color string `yaml:"color"`
Title string `yaml:"title"`
TitleLink string `yaml:"title_link"`

View File

@ -60,6 +60,9 @@ func Build(confs []*config.Receiver, tmpl *template.Template) map[string]Fanout
for i, c := range nc.OpsGenieConfigs {
add(i, NewOpsGenie(c, tmpl))
}
for i, c := range nc.SlackConfigs {
add(i, NewSlack(c, tmpl))
}
res[nc.Name] = fo
}
@ -342,6 +345,14 @@ type Slack struct {
tmpl *template.Template
}
// NewSlack returns a new Slack notification handler.
func NewSlack(conf *config.SlackConfig, tmpl *template.Template) *Slack {
return &Slack{
conf: conf,
tmpl: tmpl,
}
}
// slackReq is the request for sending a slack notification.
type slackReq struct {
Channel string `json:"channel,omitempty"`
@ -357,9 +368,8 @@ type slackAttachment struct {
Text string `json:"text"`
Fallback string `json:"fallback"`
Color string `json:"color,omitempty"`
MrkdwnIn []string `json:"mrkdwn_in,omitempty"`
Fields []slackAttachmentField `json:"fields,omitempty"`
Color string `json:"color,omitempty"`
MrkdwnIn []string `json:"mrkdwn_in,omitempty"`
}
// slackAttachmentField is displayed in a table inside the message attachment.
@ -373,7 +383,6 @@ type slackAttachmentField struct {
func (n *Slack) Notify(ctx context.Context, as ...*types.Alert) error {
var err error
var (
alerts = types.Alerts(as...)
data = n.tmpl.Data(receiver(ctx), groupLabels(ctx), as...)
tmplText = tmplText(n.tmpl, data, &err)
tmplHTML = tmplHTML(n.tmpl, data, &err)
@ -385,17 +394,12 @@ func (n *Slack) Notify(ctx context.Context, as ...*types.Alert) error {
Pretext: tmplText(n.conf.Pretext),
Text: tmplHTML(n.conf.Text),
Fallback: tmplText(n.conf.Fallback),
Fields: []slackAttachmentField{{
Title: "Status",
Value: string(alerts.Status()),
Short: true,
}},
Color: tmplText(n.conf.Color),
MrkdwnIn: []string{"fallback", "pretext"},
Color: tmplText(n.conf.Color),
MrkdwnIn: []string{"fallback", "pretext"},
}
req := &slackReq{
Channel: tmplText(n.conf.Channel),
Username: tmplText(n.conf.Username),
Attachments: []slackAttachment{*attachment},
}
if err != nil {

View File

@ -14,10 +14,11 @@
{{ define "slack.default.title" }}{{ template "__subject" . }}{{ end }}
{{ define "slack.default.fallback" }}{{ template "__subject" . }}{{ end }}
{{ define "slack.default.username" }}{{ template "__alertmanager" . }}{{ end }}
{{ define "slack.default.fallback" }}{{ template "slack.default.title" . }} | {{ template "slack.default.titlelink" . }}{{ end }}
{{ define "slack.default.pretext" }}{{ end }}
{{ define "slack.default.titlelink" }}{{ template "__alertmanagerURL" . }}{{ end }}
{{ define "slack.default.text" }}{{ template "__subject" . }}{{ end }}
{{ define "slack.default.text" }}{{ end }}
{{ define "pagerduty.default.description" }}{{ template "__subject" . }}{{ end }}