From ca6151e23fcc85ab3f67662f4d67e71dc1008b3e Mon Sep 17 00:00:00 2001 From: Abhilash Gnan Date: Thu, 23 May 2019 08:31:02 +0200 Subject: [PATCH] Alerting: Support for configuring content field for Discord alert notifier (#17017) --- pkg/services/alerting/notifiers/discord.go | 24 ++++++++++++++++--- .../alerting/notifiers/discord_test.go | 4 +++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/pkg/services/alerting/notifiers/discord.go b/pkg/services/alerting/notifiers/discord.go index f85fa7f785e..160c76528dd 100644 --- a/pkg/services/alerting/notifiers/discord.go +++ b/pkg/services/alerting/notifiers/discord.go @@ -24,15 +24,27 @@ func init() { Factory: NewDiscordNotifier, OptionsTemplate: `

Discord settings

-
- Webhook URL - +
+ Message Content + + + + Mention a group using @ or a user using <@ID> when notifying in a channel + +
+
+ Webhook URL +
`, }) } func NewDiscordNotifier(model *models.AlertNotification) (alerting.Notifier, error) { + content := model.Settings.Get("content").MustString() url := model.Settings.Get("url").MustString() if url == "" { return nil, alerting.ValidationError{Reason: "Could not find webhook url property in settings"} @@ -40,6 +52,7 @@ func NewDiscordNotifier(model *models.AlertNotification) (alerting.Notifier, err return &DiscordNotifier{ NotifierBase: NewNotifierBase(model), + Content: content, WebhookURL: url, log: log.New("alerting.notifier.discord"), }, nil @@ -47,6 +60,7 @@ func NewDiscordNotifier(model *models.AlertNotification) (alerting.Notifier, err type DiscordNotifier struct { NotifierBase + Content string WebhookURL string log log.Logger } @@ -63,6 +77,10 @@ func (this *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error { bodyJSON := simplejson.New() bodyJSON.Set("username", "Grafana") + if this.Content != "" { + bodyJSON.Set("content", this.Content) + } + fields := make([]map[string]interface{}, 0) for _, evt := range evalContext.EvalMatches { diff --git a/pkg/services/alerting/notifiers/discord_test.go b/pkg/services/alerting/notifiers/discord_test.go index dfc6bbe9aee..5fe700245e2 100644 --- a/pkg/services/alerting/notifiers/discord_test.go +++ b/pkg/services/alerting/notifiers/discord_test.go @@ -29,7 +29,8 @@ func TestDiscordNotifier(t *testing.T) { Convey("settings should trigger incident", func() { json := ` { - "url": "https://web.hook/" + "content": "@everyone Please check this notification", + "url": "https://web.hook/" }` settingsJSON, _ := simplejson.NewJson([]byte(json)) @@ -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.Content, ShouldEqual, "@everyone Please check this notification") So(discordNotifier.WebhookURL, ShouldEqual, "https://web.hook/") }) })