Improve Slack integration
This commit is contained in:
parent
42b73a6291
commit
18e9e728f8
|
@ -48,9 +48,10 @@ var (
|
||||||
|
|
||||||
// DefaultSlackConfig defines default values for Slack configurations.
|
// DefaultSlackConfig defines default values for Slack configurations.
|
||||||
DefaultSlackConfig = SlackConfig{
|
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" . }}`,
|
Title: `{{ template "slack.default.title" . }}`,
|
||||||
TitleLink: `{{ template "slack.default.title_link" . }}`,
|
TitleLink: `{{ template "slack.default.titlelink" . }}`,
|
||||||
Pretext: `{{ template "slack.default.pretext" . }}`,
|
Pretext: `{{ template "slack.default.pretext" . }}`,
|
||||||
Text: `{{ template "slack.default.text" . }}`,
|
Text: `{{ template "slack.default.text" . }}`,
|
||||||
Fallback: `{{ template "slack.default.fallback" . }}`,
|
Fallback: `{{ template "slack.default.fallback" . }}`,
|
||||||
|
@ -250,8 +251,7 @@ type SlackConfig struct {
|
||||||
|
|
||||||
// Slack channel override, (like #other-channel or @username).
|
// Slack channel override, (like #other-channel or @username).
|
||||||
Channel string `yaml:"channel"`
|
Channel string `yaml:"channel"`
|
||||||
|
Username string `yaml:"username"`
|
||||||
// The message color.
|
|
||||||
Color string `yaml:"color"`
|
Color string `yaml:"color"`
|
||||||
|
|
||||||
Title string `yaml:"title"`
|
Title string `yaml:"title"`
|
||||||
|
|
|
@ -60,6 +60,9 @@ func Build(confs []*config.Receiver, tmpl *template.Template) map[string]Fanout
|
||||||
for i, c := range nc.OpsGenieConfigs {
|
for i, c := range nc.OpsGenieConfigs {
|
||||||
add(i, NewOpsGenie(c, tmpl))
|
add(i, NewOpsGenie(c, tmpl))
|
||||||
}
|
}
|
||||||
|
for i, c := range nc.SlackConfigs {
|
||||||
|
add(i, NewSlack(c, tmpl))
|
||||||
|
}
|
||||||
|
|
||||||
res[nc.Name] = fo
|
res[nc.Name] = fo
|
||||||
}
|
}
|
||||||
|
@ -342,6 +345,14 @@ type Slack struct {
|
||||||
tmpl *template.Template
|
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.
|
// slackReq is the request for sending a slack notification.
|
||||||
type slackReq struct {
|
type slackReq struct {
|
||||||
Channel string `json:"channel,omitempty"`
|
Channel string `json:"channel,omitempty"`
|
||||||
|
@ -359,7 +370,6 @@ type slackAttachment struct {
|
||||||
|
|
||||||
Color string `json:"color,omitempty"`
|
Color string `json:"color,omitempty"`
|
||||||
MrkdwnIn []string `json:"mrkdwn_in,omitempty"`
|
MrkdwnIn []string `json:"mrkdwn_in,omitempty"`
|
||||||
Fields []slackAttachmentField `json:"fields,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// slackAttachmentField is displayed in a table inside the message attachment.
|
// 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 {
|
func (n *Slack) Notify(ctx context.Context, as ...*types.Alert) error {
|
||||||
var err error
|
var err error
|
||||||
var (
|
var (
|
||||||
alerts = types.Alerts(as...)
|
|
||||||
data = n.tmpl.Data(receiver(ctx), groupLabels(ctx), as...)
|
data = n.tmpl.Data(receiver(ctx), groupLabels(ctx), as...)
|
||||||
tmplText = tmplText(n.tmpl, data, &err)
|
tmplText = tmplText(n.tmpl, data, &err)
|
||||||
tmplHTML = tmplHTML(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),
|
Pretext: tmplText(n.conf.Pretext),
|
||||||
Text: tmplHTML(n.conf.Text),
|
Text: tmplHTML(n.conf.Text),
|
||||||
Fallback: tmplText(n.conf.Fallback),
|
Fallback: tmplText(n.conf.Fallback),
|
||||||
|
|
||||||
Fields: []slackAttachmentField{{
|
|
||||||
Title: "Status",
|
|
||||||
Value: string(alerts.Status()),
|
|
||||||
Short: true,
|
|
||||||
}},
|
|
||||||
Color: tmplText(n.conf.Color),
|
Color: tmplText(n.conf.Color),
|
||||||
MrkdwnIn: []string{"fallback", "pretext"},
|
MrkdwnIn: []string{"fallback", "pretext"},
|
||||||
}
|
}
|
||||||
req := &slackReq{
|
req := &slackReq{
|
||||||
Channel: tmplText(n.conf.Channel),
|
Channel: tmplText(n.conf.Channel),
|
||||||
|
Username: tmplText(n.conf.Username),
|
||||||
Attachments: []slackAttachment{*attachment},
|
Attachments: []slackAttachment{*attachment},
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -14,10 +14,11 @@
|
||||||
|
|
||||||
|
|
||||||
{{ define "slack.default.title" }}{{ template "__subject" . }}{{ end }}
|
{{ 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.pretext" }}{{ end }}
|
||||||
{{ define "slack.default.titlelink" }}{{ template "__alertmanagerURL" . }}{{ 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 }}
|
{{ define "pagerduty.default.description" }}{{ template "__subject" . }}{{ end }}
|
||||||
|
|
Loading…
Reference in New Issue