Alerting: Support for configuring content field for Discord alert notifier (#17017)

This commit is contained in:
Abhilash Gnan
2019-05-23 08:31:02 +02:00
committed by Carl Bergquist
parent a9c94ec93b
commit ca6151e23f
2 changed files with 24 additions and 4 deletions
+21 -3
View File
@@ -24,15 +24,27 @@ func init() {
Factory: NewDiscordNotifier, Factory: NewDiscordNotifier,
OptionsTemplate: ` OptionsTemplate: `
<h3 class="page-heading">Discord settings</h3> <h3 class="page-heading">Discord settings</h3>
<div class="gf-form"> <div class="gf-form max-width-30">
<span class="gf-form-label width-14">Webhook URL</span> <span class="gf-form-label width-10">Message Content</span>
<input type="text" required class="gf-form-input max-width-22" ng-model="ctrl.model.settings.url" placeholder="Discord webhook URL"></input> <input type="text"
class="gf-form-input max-width-30"
ng-model="ctrl.model.settings.content"
data-placement="right">
</input>
<info-popover mode="right-absolute">
Mention a group using @ or a user using <@ID> when notifying in a channel
</info-popover>
</div>
<div class="gf-form max-width-30">
<span class="gf-form-label width-10">Webhook URL</span>
<input type="text" required class="gf-form-input max-width-30" ng-model="ctrl.model.settings.url" placeholder="Discord webhook URL"></input>
</div> </div>
`, `,
}) })
} }
func NewDiscordNotifier(model *models.AlertNotification) (alerting.Notifier, error) { func NewDiscordNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
content := model.Settings.Get("content").MustString()
url := model.Settings.Get("url").MustString() url := model.Settings.Get("url").MustString()
if url == "" { if url == "" {
return nil, alerting.ValidationError{Reason: "Could not find webhook url property in settings"} 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{ return &DiscordNotifier{
NotifierBase: NewNotifierBase(model), NotifierBase: NewNotifierBase(model),
Content: content,
WebhookURL: url, WebhookURL: url,
log: log.New("alerting.notifier.discord"), log: log.New("alerting.notifier.discord"),
}, nil }, nil
@@ -47,6 +60,7 @@ func NewDiscordNotifier(model *models.AlertNotification) (alerting.Notifier, err
type DiscordNotifier struct { type DiscordNotifier struct {
NotifierBase NotifierBase
Content string
WebhookURL string WebhookURL string
log log.Logger log log.Logger
} }
@@ -63,6 +77,10 @@ func (this *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
bodyJSON := simplejson.New() bodyJSON := simplejson.New()
bodyJSON.Set("username", "Grafana") bodyJSON.Set("username", "Grafana")
if this.Content != "" {
bodyJSON.Set("content", this.Content)
}
fields := make([]map[string]interface{}, 0) fields := make([]map[string]interface{}, 0)
for _, evt := range evalContext.EvalMatches { for _, evt := range evalContext.EvalMatches {
@@ -29,7 +29,8 @@ func TestDiscordNotifier(t *testing.T) {
Convey("settings should trigger incident", func() { Convey("settings should trigger incident", func() {
json := ` json := `
{ {
"url": "https://web.hook/" "content": "@everyone Please check this notification",
"url": "https://web.hook/"
}` }`
settingsJSON, _ := simplejson.NewJson([]byte(json)) settingsJSON, _ := simplejson.NewJson([]byte(json))
@@ -45,6 +46,7 @@ func TestDiscordNotifier(t *testing.T) {
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(discordNotifier.Name, ShouldEqual, "discord_testing") So(discordNotifier.Name, ShouldEqual, "discord_testing")
So(discordNotifier.Type, ShouldEqual, "discord") So(discordNotifier.Type, ShouldEqual, "discord")
So(discordNotifier.Content, ShouldEqual, "@everyone Please check this notification")
So(discordNotifier.WebhookURL, ShouldEqual, "https://web.hook/") So(discordNotifier.WebhookURL, ShouldEqual, "https://web.hook/")
}) })
}) })