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

Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
(cherry picked from commit badec6c6ad)

Co-authored-by: Chip Wolf ‮ <hello@chipwolf.uk>
This commit is contained in:
Grot (@grafanabot)
2021-05-31 03:02:32 -04:00
committed by GitHub
parent bfb3e90750
commit 2335c3d0d5
8 changed files with 71 additions and 2 deletions
@@ -25,6 +25,13 @@ func init() {
Factory: newDiscordNotifier,
Heading: "Discord settings",
Options: []alerting.NotifierOption{
{
Label: "Avatar URL",
Element: alerting.ElementTypeInput,
InputType: alerting.InputTypeText,
Description: "Provide a URL to an image to use as the avatar for the bot's message",
PropertyName: "avatar_url",
},
{
Label: "Message Content",
Description: "Mention a group using @ or a user using <@ID> when notifying in a channel",
@@ -45,6 +52,7 @@ func init() {
}
func newDiscordNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
avatar := model.Settings.Get("avatar_url").MustString()
content := model.Settings.Get("content").MustString()
url := model.Settings.Get("url").MustString()
if url == "" {
@@ -54,6 +62,7 @@ func newDiscordNotifier(model *models.AlertNotification) (alerting.Notifier, err
return &DiscordNotifier{
NotifierBase: NewNotifierBase(model),
Content: content,
AvatarURL: avatar,
WebhookURL: url,
log: log.New("alerting.notifier.discord"),
}, nil
@@ -64,6 +73,7 @@ func newDiscordNotifier(model *models.AlertNotification) (alerting.Notifier, err
type DiscordNotifier struct {
NotifierBase
Content string
AvatarURL string
WebhookURL string
log log.Logger
}
@@ -85,6 +95,10 @@ func (dn *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
bodyJSON.Set("content", dn.Content)
}
if dn.AvatarURL != "" {
bodyJSON.Set("avatar_url", dn.AvatarURL)
}
fields := make([]map[string]interface{}, 0)
for _, evt := range evalContext.EvalMatches {
@@ -28,6 +28,7 @@ func TestDiscordNotifier(t *testing.T) {
Convey("settings should trigger incident", func() {
json := `
{
"avatar_url": "https://grafana.com/img/fav32.png",
"content": "@everyone Please check this notification",
"url": "https://web.hook/"
}`
@@ -45,6 +46,7 @@ func TestDiscordNotifier(t *testing.T) {
So(err, ShouldBeNil)
So(discordNotifier.Name, ShouldEqual, "discord_testing")
So(discordNotifier.Type, ShouldEqual, "discord")
So(discordNotifier.AvatarURL, ShouldEqual, "https://grafana.com/img/fav32.png")
So(discordNotifier.Content, ShouldEqual, "@everyone Please check this notification")
So(discordNotifier.WebhookURL, ShouldEqual, "https://web.hook/")
})