Skip to content

Commit 6b77acd

Browse files
authored
Extend discord messages with content (prometheus#4007)
* Extend discord messages with content Signed-off-by: Yevhen Sydorenko <[email protected]> Signed-off-by: Yevhen Sydorenko <[email protected]> * Updated configuration.md Updated assets_vfsdata.go Signed-off-by: Yevhen Sydorenko <[email protected]> * Fix grammar Signed-off-by: Yevhen Sydorenko <[email protected]> * Updated vfsdata Signed-off-by: Yevhen Sydorenko <[email protected]> --------- Signed-off-by: Yevhen Sydorenko <[email protected]>
1 parent 69fe3f8 commit 6b77acd

File tree

5 files changed

+18
-2
lines changed

5 files changed

+18
-2
lines changed

asset/assets_vfsdata.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/notifiers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ type DiscordConfig struct {
251251
WebhookURL *SecretURL `yaml:"webhook_url,omitempty" json:"webhook_url,omitempty"`
252252
WebhookURLFile string `yaml:"webhook_url_file,omitempty" json:"webhook_url_file,omitempty"`
253253

254+
Content string `yaml:"content,omitempty" json:"content,omitempty"`
254255
Title string `yaml:"title,omitempty" json:"title,omitempty"`
255256
Message string `yaml:"message,omitempty" json:"message,omitempty"`
256257
}

docs/configuration.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,9 @@ webhook_url_file: <filepath>
883883
# Message body template.
884884
[ message: <tmpl_string> | default = '{{ template "discord.default.message" . }}' ]
885885
886+
# Message content template. Limited to 2000 characters.
887+
[ content: <tmpl_string> | default = '{{ template "discord.default.content" . }}' ]
888+
886889
# The HTTP client's configuration.
887890
[ http_config: <http_config> | default = global.http_config ]
888891
```

notify/discord/discord.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const (
3838
maxTitleLenRunes = 256
3939
// https://discord.com/developers/docs/resources/channel#embed-object-embed-limits - 4096 characters or runes.
4040
maxDescriptionLenRunes = 4096
41+
42+
maxContentLenRunes = 2000
4143
)
4244

4345
const (
@@ -115,6 +117,14 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
115117
level.Warn(n.logger).Log("msg", "Truncated message", "key", key, "max_runes", maxDescriptionLenRunes)
116118
}
117119

120+
content, truncated := notify.TruncateInRunes(tmpl(n.conf.Content), maxContentLenRunes)
121+
if err != nil {
122+
return false, err
123+
}
124+
if truncated {
125+
level.Warn(n.logger).Log("msg", "Truncated message", "key", key, "max_runes", maxContentLenRunes)
126+
}
127+
118128
color := colorGrey
119129
if alerts.Status() == model.AlertFiring {
120130
color = colorRed
@@ -135,6 +145,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
135145
}
136146

137147
w := webhook{
148+
Content: content,
138149
Embeds: []webhookEmbed{{
139150
Title: title,
140151
Description: description,

template/default.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ Alerts Resolved:
123123
{{ end }}
124124
{{ end }}
125125

126+
{{ define "discord.default.content" }}{{ end }}
126127
{{ define "discord.default.title" }}{{ template "__subject" . }}{{ end }}
127128
{{ define "discord.default.message" }}
128129
{{ if gt (len .Alerts.Firing) 0 }}

0 commit comments

Comments
 (0)