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

@@ -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 {

View File

@@ -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/")
})