Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add changes to #4007 #4080

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ type DiscordConfig struct {
WebhookURL *SecretURL `yaml:"webhook_url,omitempty" json:"webhook_url,omitempty"`
WebhookURLFile string `yaml:"webhook_url_file,omitempty" json:"webhook_url_file,omitempty"`

Content string `yaml:"content,omitempty" json:"content,omitempty"`
Title string `yaml:"title,omitempty" json:"title,omitempty"`
Message string `yaml:"message,omitempty" json:"message,omitempty"`
}
Expand Down
3 changes: 3 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,9 @@ webhook_url_file: <filepath>
# Message body template.
[ message: <tmpl_string> | default = '{{ template "discord.default.message" . }}' ]

# Message content template. Has a limit of 2000 characters.
[ content: <tmpl_string> | default = '{{ template "discord.default.content" . }}' ]

# The HTTP client's configuration.
[ http_config: <http_config> | default = global.http_config ]
```
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/prometheus/alertmanager

go 1.22
go 1.22.8

require (
github.com/KimMachineGun/automemlimit v0.6.1
Expand Down
11 changes: 11 additions & 0 deletions notify/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
maxTitleLenRunes = 256
// https://discord.com/developers/docs/resources/channel#embed-object-embed-limits - 4096 characters or runes.
maxDescriptionLenRunes = 4096

maxContentLenRunes = 2000
)

const (
Expand Down Expand Up @@ -115,6 +117,14 @@ 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)
}

content, truncated := notify.TruncateInRunes(tmpl(n.conf.Content), maxContentLenRunes)
if err != nil {
return false, err
}
if truncated {
level.Warn(n.logger).Log("msg", "Truncated message", "key", key, "max_runes", maxContentLenRunes)
}

color := colorGrey
if alerts.Status() == model.AlertFiring {
color = colorRed
Expand All @@ -135,6 +145,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
}

w := webhook{
Content: content,
Embeds: []webhookEmbed{{
Title: title,
Description: description,
Expand Down
1 change: 1 addition & 0 deletions template/default.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ Alerts Resolved:
{{ end }}
{{ end }}

{{ define "discord.default.content" }}{{ end }}
{{ define "discord.default.title" }}{{ template "__subject" . }}{{ end }}
{{ define "discord.default.message" }}
{{ if gt (len .Alerts.Firing) 0 }}
Expand Down
Loading