Alerting: Add support for configuring avatar URL for the Discord notifier (#33355)

Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
This commit is contained in:
Chip Wolf ‮
2021-05-28 22:00:21 +01:00
committed by GitHub
parent 66e2624ae0
commit badec6c6ad
8 changed files with 71 additions and 2 deletions

View File

@@ -688,6 +688,12 @@ func GetAvailableNotifiers() []*alerting.NotifierPlugin {
PropertyName: "url",
Required: true,
},
{
Label: "Avatar URL",
Element: alerting.ElementTypeInput,
InputType: alerting.InputTypeText,
PropertyName: "avatar_url",
},
},
},
{

View File

@@ -24,6 +24,7 @@ type DiscordNotifier struct {
log log.Logger
tmpl *template.Template
Content string
AvatarURL string
WebhookURL string
}
@@ -32,6 +33,8 @@ func NewDiscordNotifier(model *NotificationChannelConfig, t *template.Template)
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
}
avatarURL := model.Settings.Get("avatar_url").MustString()
discordURL := model.Settings.Get("url").MustString()
if discordURL == "" {
return nil, alerting.ValidationError{Reason: "Could not find webhook url property in settings"}
@@ -49,6 +52,7 @@ func NewDiscordNotifier(model *NotificationChannelConfig, t *template.Template)
SecureSettings: model.SecureSettings,
}),
Content: content,
AvatarURL: avatarURL,
WebhookURL: discordURL,
log: log.New("alerting.notifier.discord"),
tmpl: t,
@@ -70,6 +74,10 @@ func (d DiscordNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool,
bodyJSON.Set("content", tmpl(d.Content))
}
if d.AvatarURL != "" {
bodyJSON.Set("avatar_url", tmpl(d.AvatarURL))
}
footer := map[string]interface{}{
"text": "Grafana v" + setting.BuildVersion,
"icon_url": "https://grafana.com/assets/img/fav32.png",

View File

@@ -64,6 +64,7 @@ func TestDiscordNotifier(t *testing.T) {
{
name: "Custom config with multiple alerts",
settings: `{
"avatar_url": "https://grafana.com/assets/img/fav32.png",
"url": "http://localhost",
"message": "{{ len .Alerts.Firing }} alerts are firing, {{ len .Alerts.Resolved }} are resolved"
}`,
@@ -81,7 +82,8 @@ func TestDiscordNotifier(t *testing.T) {
},
},
expMsg: map[string]interface{}{
"content": "2 alerts are firing, 0 are resolved",
"avatar_url": "https://grafana.com/assets/img/fav32.png",
"content": "2 alerts are firing, 0 are resolved",
"embeds": []interface{}{map[string]interface{}{
"color": 1.4037554e+07,
"footer": map[string]interface{}{