Added uid for alert notifications

This commit is contained in:
Pavel Bakulev
2019-01-16 16:50:00 +02:00
parent 6d1ec19fe9
commit f132e929ce
26 changed files with 533 additions and 357 deletions
+1 -1
View File
@@ -24,7 +24,7 @@ type Notifier interface {
// ShouldNotify checks this evaluation should send an alert notification
ShouldNotify(ctx context.Context, evalContext *EvalContext, notificationState *models.AlertNotificationState) bool
GetNotifierId() int64
GetNotifierUid() string
GetIsDefault() bool
GetSendReminder() bool
GetDisableResolveMessage() bool
+6 -6
View File
@@ -60,13 +60,13 @@ func (n *notificationService) SendIfNeeded(context *EvalContext) error {
func (n *notificationService) sendAndMarkAsComplete(evalContext *EvalContext, notifierState *notifierState) error {
notifier := notifierState.notifier
n.log.Debug("Sending notification", "type", notifier.GetType(), "id", notifier.GetNotifierId(), "isDefault", notifier.GetIsDefault())
n.log.Debug("Sending notification", "type", notifier.GetType(), "uid", notifier.GetNotifierUid(), "isDefault", notifier.GetIsDefault())
metrics.M_Alerting_Notification_Sent.WithLabelValues(notifier.GetType()).Inc()
err := notifier.Notify(evalContext)
if err != nil {
n.log.Error("failed to send notification", "id", notifier.GetNotifierId(), "error", err)
n.log.Error("failed to send notification", "uid", notifier.GetNotifierUid(), "error", err)
}
if evalContext.IsTestRun {
@@ -110,7 +110,7 @@ func (n *notificationService) sendNotifications(evalContext *EvalContext, notifi
for _, notifierState := range notifierStates {
err := n.sendNotification(evalContext, notifierState)
if err != nil {
n.log.Error("failed to send notification", "id", notifierState.notifier.GetNotifierId(), "error", err)
n.log.Error("failed to send notification", "uid", notifierState.notifier.GetNotifierUid(), "error", err)
}
}
@@ -157,8 +157,8 @@ func (n *notificationService) uploadImage(context *EvalContext) (err error) {
return nil
}
func (n *notificationService) getNeededNotifiers(orgId int64, notificationIds []int64, evalContext *EvalContext) (notifierStateSlice, error) {
query := &m.GetAlertNotificationsToSendQuery{OrgId: orgId, Ids: notificationIds}
func (n *notificationService) getNeededNotifiers(orgId int64, notificationUids []string, evalContext *EvalContext) (notifierStateSlice, error) {
query := &m.GetAlertNotificationsWithUidToSendQuery{OrgId: orgId, Uids: notificationUids}
if err := bus.Dispatch(query); err != nil {
return nil, err
@@ -168,7 +168,7 @@ func (n *notificationService) getNeededNotifiers(orgId int64, notificationIds []
for _, notification := range query.Result {
not, err := InitNotifier(notification)
if err != nil {
n.log.Error("Could not create notifier", "notifier", notification.Id, "error", err)
n.log.Error("Could not create notifier", "notifier", notification.Uid, "error", err)
continue
}
+4 -4
View File
@@ -16,7 +16,7 @@ const (
type NotifierBase struct {
Name string
Type string
Id int64
Uid string
IsDeault bool
UploadImage bool
SendReminder bool
@@ -34,7 +34,7 @@ func NewNotifierBase(model *models.AlertNotification) NotifierBase {
}
return NotifierBase{
Id: model.Id,
Uid: model.Uid,
Name: model.Name,
IsDeault: model.IsDefault,
Type: model.Type,
@@ -110,8 +110,8 @@ func (n *NotifierBase) NeedsImage() bool {
return n.UploadImage
}
func (n *NotifierBase) GetNotifierId() int64 {
return n.Id
func (n *NotifierBase) GetNotifierUid() string {
return n.Uid
}
func (n *NotifierBase) GetIsDefault() bool {
+1 -1
View File
@@ -173,7 +173,7 @@ func TestBaseNotifier(t *testing.T) {
bJson := simplejson.New()
model := &m.AlertNotification{
Id: 1,
Uid: "1",
Name: "name",
Type: "email",
Settings: bJson,
+4 -17
View File
@@ -7,8 +7,6 @@ import (
"strconv"
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
)
@@ -32,7 +30,7 @@ type Rule struct {
ExecutionErrorState m.ExecutionErrorOption
State m.AlertStateType
Conditions []Condition
Notifications []int64
Notifications []string
StateChanges int64
}
@@ -128,22 +126,11 @@ func NewRuleFromDBAlert(ruleDef *m.Alert) (*Rule, error) {
for _, v := range ruleDef.Settings.Get("notifications").MustArray() {
jsonModel := simplejson.NewFromAny(v)
id, err := jsonModel.Get("id").Int64()
uid, err := jsonModel.Get("uid").String()
if err != nil {
notificationName, notificationNameErr := jsonModel.Get("name").String()
if notificationNameErr != nil {
return nil, ValidationError{Reason: "Invalid notification schema", DashboardId: model.DashboardId, Alertid: model.Id, PanelId: model.PanelId}
}
cmd := &m.GetAlertNotificationsQuery{OrgId: ruleDef.OrgId, Name: notificationName}
nameErr := bus.Dispatch(cmd)
if nameErr != nil || cmd.Result == nil {
errorMsg := fmt.Sprintf("Cannot lookup notification by name %s and orgId %d", notificationName, ruleDef.OrgId)
return nil, ValidationError{Reason: errorMsg, DashboardId: model.DashboardId, Alertid: model.Id, PanelId: model.PanelId}
}
model.Notifications = append(model.Notifications, cmd.Result.Id)
} else {
model.Notifications = append(model.Notifications, id)
return nil, ValidationError{Reason: "Invalid notification schema", DashboardId: model.DashboardId, Alertid: model.Id, PanelId: model.PanelId}
}
model.Notifications = append(model.Notifications, uid)
}
+2 -157
View File
@@ -3,17 +3,11 @@ package alerting
import (
"testing"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
m "github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
var (
fakeRepo *fakeRepository
)
type FakeCondition struct{}
func (f *FakeCondition) Eval(context *EvalContext) (*ConditionResult, error) {
@@ -78,8 +72,8 @@ func TestAlertRuleModel(t *testing.T) {
}
],
"notifications": [
{"id": 1134},
{"id": 22}
{"uid": "1134"},
{"uid": "22"}
]
}
`
@@ -135,155 +129,6 @@ func TestAlertRuleModel(t *testing.T) {
So(err, ShouldBeNil)
So(alertRule.Frequency, ShouldEqual, 60)
})
Convey("can construct alert rule model mixed notification ids and names", func() {
json := `
{
"name": "name2",
"description": "desc2",
"handler": 0,
"noDataMode": "critical",
"enabled": true,
"frequency": "60s",
"conditions": [
{
"type": "test",
"prop": 123
}
],
"notifications": [
{"id": 1134},
{"id": 22},
{"name": "channel1"},
{"name": "channel2"}
]
}
`
alertJSON, jsonErr := simplejson.NewJson([]byte(json))
So(jsonErr, ShouldBeNil)
alert := &m.Alert{
Id: 1,
OrgId: 1,
DashboardId: 1,
PanelId: 1,
Settings: alertJSON,
}
fakeRepo = &fakeRepository{}
bus.ClearBusHandlers()
bus.AddHandler("test", mockGet)
fakeRepo.loadAll = []*models.AlertNotification{
{Name: "channel1", OrgId: 1, Id: 1},
{Name: "channel2", OrgId: 1, Id: 2},
}
alertRule, err := NewRuleFromDBAlert(alert)
So(err, ShouldBeNil)
Convey("Can read notifications", func() {
So(len(alertRule.Notifications), ShouldEqual, 4)
So(alertRule.Notifications, ShouldResemble, []int64{1134, 22, 1, 2})
})
})
Convey("raise error in case of left id", func() {
json := `
{
"name": "name2",
"description": "desc2",
"handler": 0,
"noDataMode": "critical",
"enabled": true,
"frequency": "60s",
"conditions": [
{
"type": "test",
"prop": 123
}
],
"notifications": [
{"not_id": 1134}
]
}
`
alertJSON, jsonErr := simplejson.NewJson([]byte(json))
So(jsonErr, ShouldBeNil)
alert := &m.Alert{
Id: 1,
OrgId: 1,
DashboardId: 1,
PanelId: 1,
Settings: alertJSON,
}
_, err := NewRuleFromDBAlert(alert)
So(err, ShouldNotBeNil)
})
Convey("raise error in case of left id but existed name with alien orgId", func() {
json := `
{
"name": "name2",
"description": "desc2",
"handler": 0,
"noDataMode": "critical",
"enabled": true,
"frequency": "60s",
"conditions": [
{
"type": "test",
"prop": 123
}
],
"notifications": [
{"not_id": 1134, "name": "channel1"}
]
}
`
alertJSON, jsonErr := simplejson.NewJson([]byte(json))
So(jsonErr, ShouldBeNil)
alert := &m.Alert{
Id: 1,
OrgId: 1,
DashboardId: 1,
PanelId: 1,
Settings: alertJSON,
}
fakeRepo = &fakeRepository{}
bus.ClearBusHandlers()
bus.AddHandler("test", mockGet)
fakeRepo.loadAll = []*models.AlertNotification{
{Name: "channel1", OrgId: 2, Id: 1},
}
_, err := NewRuleFromDBAlert(alert)
So(err, ShouldNotBeNil)
})
})
}
type fakeRepository struct {
loadAll []*models.AlertNotification
}
func mockGet(cmd *models.GetAlertNotificationsQuery) error {
for _, v := range fakeRepo.loadAll {
if cmd.Name == v.Name && cmd.OrgId == v.OrgId {
cmd.Result = v
return nil
}
}
return nil
}
+1 -1
View File
@@ -44,7 +44,7 @@
"noDataState": "no_data",
"notifications": [
{
"id": 6
"uid": "notifier1"
}
]
},
+1 -1
View File
@@ -45,7 +45,7 @@
"noDataState": "no_data",
"notifications": [
{
"id": 6
"uid": "notifier1"
}
]
},