Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
},
Title: `{{ template "discord.default.title" . }}`,
Message: `{{ template "discord.default.message" . }}`,
Color: `{{ template "discord.default.color" . }}`,
}

// DefaultEmailConfig defines default values for Email configurations.
Expand Down Expand Up @@ -221,6 +222,7 @@ type DiscordConfig struct {

Title string `yaml:"title,omitempty" json:"title,omitempty"`
Message string `yaml:"message,omitempty" json:"message,omitempty"`
Color string `yaml:"color,omitempty" json:"color,omitempty"`
}

// UnmarshalYAML implements the yaml.Unmarshaler interface.
Expand Down
3 changes: 3 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,9 @@ webhook_url: <secret>
# Message body template.
[ message: <tmpl_string> | default = '{{ template "discord.default.message" . }}' ]

# Color of message frame.
[ color: <tmpl_string> | default = '{{ template "discord.default.color" . }}' ]

# The HTTP client's configuration.
[ http_config: <http_config> | default = global.http_config ]
```
Expand Down
18 changes: 2 additions & 16 deletions notify/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"

"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/notify"
Expand All @@ -37,12 +36,6 @@ const (
maxDescriptionLenRunes = 4096
)

const (
colorRed = 0x992D22
colorGreen = 0x2ECC71
colorGrey = 0x95A5A6
)

// Notifier implements a Notifier for Discord notifications.
type Notifier struct {
conf *config.DiscordConfig
Expand Down Expand Up @@ -78,7 +71,7 @@ type webhook struct {
type webhookEmbed struct {
Title string `json:"title"`
Description string `json:"description"`
Color int `json:"color"`
Color string `json:"color,omitempty"`
}

// Notify implements the Notifier interface.
Expand All @@ -90,7 +83,6 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)

level.Debug(n.logger).Log("incident", key)

alerts := types.Alerts(as...)
data := notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
tmpl := notify.TmplText(n.tmpl, data, &err)
if err != nil {
Expand All @@ -112,13 +104,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
level.Warn(n.logger).Log("msg", "Truncated message", "key", key, "max_runes", maxDescriptionLenRunes)
}

color := colorGrey
if alerts.Status() == model.AlertFiring {
color = colorRed
}
if alerts.Status() == model.AlertResolved {
color = colorGreen
}
color := tmpl(n.conf.Color)

w := webhook{
Embeds: []webhookEmbed{{
Expand Down
9 changes: 9 additions & 0 deletions template/default.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ Alerts Resolved:
{{ template "__text_alert_list" .Alerts.Resolved }}
{{ end }}
{{ end }}
{{ define "discord.default.color" }}
{{ if gt (len .Alerts.Firing) 0 }}
10038562

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a comment here what color it is, please?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's dark red (#992D22 / 0x992D22), which was the original colorRed constant in discord.go.
Will definitely add a clarifying comment like {{/* Red: #992D22 */}} during the rebase.

{{ else if gt (len .Alerts.Resolved) 0 }}
3066993
{{ else }}
9807270
{{ end }}
{{ end }}

{{ define "webex.default.message" }}{{ .CommonAnnotations.SortedPairs.Values | join " " }}
{{ if gt (len .Alerts.Firing) 0 }}
Expand Down