Commit 8bc04628 by Carl Bergquist Committed by GitHub

Merge pull request #6467 from utkarshcmu/slack_mention

Added Slack mention feature
parents 6fa89456 dc3a62da
...@@ -23,11 +23,13 @@ func NewSlackNotifier(model *m.AlertNotification) (alerting.Notifier, error) { ...@@ -23,11 +23,13 @@ func NewSlackNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
} }
recipient := model.Settings.Get("recipient").MustString() recipient := model.Settings.Get("recipient").MustString()
mention := model.Settings.Get("mention").MustString()
return &SlackNotifier{ return &SlackNotifier{
NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings), NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
Url: url, Url: url,
Recipient: recipient, Recipient: recipient,
Mention: mention,
log: log.New("alerting.notifier.slack"), log: log.New("alerting.notifier.slack"),
}, nil }, nil
} }
...@@ -36,6 +38,7 @@ type SlackNotifier struct { ...@@ -36,6 +38,7 @@ type SlackNotifier struct {
NotifierBase NotifierBase
Url string Url string
Recipient string Recipient string
Mention string
log log.Logger log log.Logger
} }
...@@ -70,9 +73,9 @@ func (this *SlackNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -70,9 +73,9 @@ func (this *SlackNotifier) Notify(evalContext *alerting.EvalContext) error {
}) })
} }
message := "" message := this.Mention
if evalContext.Rule.State != m.AlertStateOK { //dont add message when going back to alert state ok. if evalContext.Rule.State != m.AlertStateOK { //dont add message when going back to alert state ok.
message = evalContext.Rule.Message message += " " + evalContext.Rule.Message
} }
body := map[string]interface{}{ body := map[string]interface{}{
......
package notifiers
import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
func TestSlackNotifier(t *testing.T) {
Convey("Slack notifier tests", t, func() {
Convey("Parsing alert notification from settings", func() {
Convey("empty settings should return error", func() {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
Name: "ops",
Type: "slack",
Settings: settingsJSON,
}
_, err := NewSlackNotifier(model)
So(err, ShouldNotBeNil)
})
Convey("from settings", func() {
json := `
{
"url": "http://google.com"
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
Name: "ops",
Type: "slack",
Settings: settingsJSON,
}
not, err := NewSlackNotifier(model)
slackNotifier := not.(*SlackNotifier)
So(err, ShouldBeNil)
So(slackNotifier.Name, ShouldEqual, "ops")
So(slackNotifier.Type, ShouldEqual, "slack")
So(slackNotifier.Url, ShouldEqual, "http://google.com")
So(slackNotifier.Recipient, ShouldEqual, "")
So(slackNotifier.Mention, ShouldEqual, "")
})
Convey("from settings with Recipient and Mention", func() {
json := `
{
"url": "http://google.com",
"recipient": "#ds-opentsdb",
"mention": "@carl"
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
Name: "ops",
Type: "slack",
Settings: settingsJSON,
}
not, err := NewSlackNotifier(model)
slackNotifier := not.(*SlackNotifier)
So(err, ShouldBeNil)
So(slackNotifier.Name, ShouldEqual, "ops")
So(slackNotifier.Type, ShouldEqual, "slack")
So(slackNotifier.Url, ShouldEqual, "http://google.com")
So(slackNotifier.Recipient, ShouldEqual, "#ds-opentsdb")
So(slackNotifier.Mention, ShouldEqual, "@carl")
})
})
})
}
...@@ -40,12 +40,12 @@ func TestWebhookNotifier(t *testing.T) { ...@@ -40,12 +40,12 @@ func TestWebhookNotifier(t *testing.T) {
} }
not, err := NewWebHookNotifier(model) not, err := NewWebHookNotifier(model)
emailNotifier := not.(*WebhookNotifier) webhookNotifier := not.(*WebhookNotifier)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(emailNotifier.Name, ShouldEqual, "ops") So(webhookNotifier.Name, ShouldEqual, "ops")
So(emailNotifier.Type, ShouldEqual, "email") So(webhookNotifier.Type, ShouldEqual, "email")
So(emailNotifier.Url, ShouldEqual, "http://google.com") So(webhookNotifier.Url, ShouldEqual, "http://google.com")
}) })
}) })
}) })
......
...@@ -74,6 +74,17 @@ ...@@ -74,6 +74,17 @@
Override default channel or user, use #channel-name or @username Override default channel or user, use #channel-name or @username
</info-popover> </info-popover>
</div> </div>
<div class="gf-form max-width-30">
<span class="gf-form-label width-6">Mention</span>
<input type="text"
class="gf-form-input max-width-30"
ng-model="ctrl.model.settings.mention"
data-placement="right">
</input>
<info-popover mode="right-absolute">
Mention a user or a group using @ when notifying in a channel
</info-popover>
</div>
</div> </div>
<div class="gf-form-group section" ng-if="ctrl.model.type === 'email'"> <div class="gf-form-group section" ng-if="ctrl.model.type === 'email'">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment