Skip to content

Commit fa309ec

Browse files
committed
feat(alerting): add default notification group
1 parent 0a85efb commit fa309ec

File tree

7 files changed

+107
-50
lines changed

7 files changed

+107
-50
lines changed

pkg/models/alert_notifications.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,33 @@ import (
77
)
88

99
type AlertNotification struct {
10-
Id int64 `json:"id"`
11-
OrgId int64 `json:"-"`
12-
Name string `json:"name"`
13-
Type string `json:"type"`
14-
Settings *simplejson.Json `json:"settings"`
15-
Created time.Time `json:"created"`
16-
Updated time.Time `json:"updated"`
10+
Id int64 `json:"id"`
11+
OrgId int64 `json:"-"`
12+
Name string `json:"name"`
13+
Type string `json:"type"`
14+
AlwaysExecute bool `json:"alwaysExecute"`
15+
Settings *simplejson.Json `json:"settings"`
16+
Created time.Time `json:"created"`
17+
Updated time.Time `json:"updated"`
1718
}
1819

1920
type CreateAlertNotificationCommand struct {
20-
Name string `json:"name" binding:"Required"`
21-
Type string `json:"type" binding:"Required"`
22-
OrgID int64 `json:"-"`
23-
Settings *simplejson.Json `json:"settings"`
21+
Name string `json:"name" binding:"Required"`
22+
Type string `json:"type" binding:"Required"`
23+
AlwaysExecute bool `json:"alwaysExecute"`
24+
OrgID int64 `json:"-"`
25+
Settings *simplejson.Json `json:"settings"`
2426

2527
Result *AlertNotification
2628
}
2729

2830
type UpdateAlertNotificationCommand struct {
29-
Id int64 `json:"id" binding:"Required"`
30-
Name string `json:"name" binding:"Required"`
31-
Type string `json:"type" binding:"Required"`
32-
OrgID int64 `json:"-"`
33-
Settings *simplejson.Json `json:"settings" binding:"Required"`
31+
Id int64 `json:"id" binding:"Required"`
32+
Name string `json:"name" binding:"Required"`
33+
Type string `json:"type" binding:"Required"`
34+
AlwaysExecute bool `json:"alwaysExecute"`
35+
OrgID int64 `json:"-"`
36+
Settings *simplejson.Json `json:"settings" binding:"Required"`
3437

3538
Result *AlertNotification
3639
}
@@ -41,10 +44,11 @@ type DeleteAlertNotificationCommand struct {
4144
}
4245

4346
type GetAlertNotificationQuery struct {
44-
Name string
45-
Id int64
46-
Ids []int64
47-
OrgID int64
47+
Name string
48+
Id int64
49+
Ids []int64
50+
OrgID int64
51+
IncludeAlwaysExecute bool
4852

4953
Result []*AlertNotification
5054
}

pkg/services/alerting/alert_rule.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ func NewAlertRuleFromDBModel(ruleDef *m.Alert) (*AlertRule, error) {
6363
model.State = ruleDef.State
6464
model.Frequency = ruleDef.Frequency
6565

66+
model.NotificationGroups = []int64{1, 2}
67+
6668
critical := ruleDef.Settings.Get("crit")
6769
model.Critical = Level{
6870
Operator: critical.Get("op").MustString(),

pkg/services/alerting/notifier.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ func NewNotifier() *NotifierImpl {
2222
}
2323

2424
func (n *NotifierImpl) Notify(alertResult *AlertResult) {
25-
notifiers := n.getNotifiers(alertResult.AlertJob.Rule.OrgId, []int64{1, 2})
25+
notifiers := n.getNotifiers(alertResult.AlertJob.Rule.OrgId, alertResult.AlertJob.Rule.NotificationGroups)
2626

2727
for _, notifier := range notifiers {
2828
warn := alertResult.State == alertstates.Warn && notifier.SendWarning
2929
crit := alertResult.State == alertstates.Critical && notifier.SendCritical
30-
if warn || crit {
30+
if (warn || crit) || alertResult.State == alertstates.Ok {
3131
n.log.Info("Sending notification", "state", alertResult.State, "type", notifier.Type)
3232
go notifier.Notifierr.Dispatch(alertResult)
3333
}
@@ -109,20 +109,23 @@ type NotificationDispatcher interface {
109109

110110
func (n *NotifierImpl) getNotifiers(orgId int64, notificationGroups []int64) []*Notification {
111111
query := &m.GetAlertNotificationQuery{
112-
OrgID: orgId,
113-
Ids: notificationGroups,
112+
OrgID: orgId,
113+
Ids: notificationGroups,
114+
IncludeAlwaysExecute: true,
114115
}
115116
err := bus.Dispatch(query)
116117
if err != nil {
117118
n.log.Error("Failed to read notifications", "error", err)
118119
}
119120

120121
var result []*Notification
121-
122+
n.log.Info("notifiriring", "count", len(query.Result), "groups", notificationGroups)
122123
for _, notification := range query.Result {
123124
not, err := NewNotificationFromDBModel(notification)
124125
if err == nil {
125126
result = append(result, not)
127+
} else {
128+
n.log.Error("Failed to read notification model", "error", err)
126129
}
127130
}
128131

pkg/services/notifications/send_email_integration_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ func TestEmailIntegrationTest(t *testing.T) {
2222
err := Init()
2323
So(err, ShouldBeNil)
2424

25-
var sentMsg *Message
2625
addToMailQueue = func(msg *Message) {
27-
sentMsg = msg
2826
ioutil.WriteFile("../../../tmp/test_email.html", []byte(msg.Body), 0777)
2927
}
3028

pkg/services/sqlstore/alert_notification.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ func getAlertNotifications(query *m.GetAlertNotificationQuery, sess *xorm.Sessio
4646
alert_notification.type,
4747
alert_notification.created,
4848
alert_notification.updated,
49-
alert_notification.settings
49+
alert_notification.settings,
50+
alert_notification.always_execute
5051
FROM alert_notification
5152
`)
5253

@@ -77,18 +78,43 @@ func getAlertNotifications(query *m.GetAlertNotificationQuery, sess *xorm.Sessio
7778
sql.WriteString(`)`)
7879
}
7980

80-
var result []*m.AlertNotification
81-
if err := sess.Sql(sql.String(), params...).Find(&result); err != nil {
81+
var searches []*m.AlertNotification
82+
if err := sess.Sql(sql.String(), params...).Find(&searches); err != nil {
8283
return err
8384
}
8485

86+
var result []*m.AlertNotification
87+
var def []*m.AlertNotification
88+
if query.IncludeAlwaysExecute {
89+
90+
if err := sess.Where("org_id = ? AND always_execute = 1", query.OrgID).Find(&def); err != nil {
91+
return err
92+
}
93+
94+
result = append(result, def...)
95+
}
96+
97+
for _, s := range searches {
98+
canAppend := true
99+
for _, d := range result {
100+
if d.Id == s.Id {
101+
canAppend = false
102+
break
103+
}
104+
}
105+
106+
if canAppend {
107+
result = append(result, s)
108+
}
109+
}
110+
85111
query.Result = result
86112
return nil
87113
}
88114

89115
func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error {
90116
return inTransaction(func(sess *xorm.Session) error {
91-
existingQuery := &m.GetAlertNotificationQuery{OrgID: cmd.OrgID, Name: cmd.Name}
117+
existingQuery := &m.GetAlertNotificationQuery{OrgID: cmd.OrgID, Name: cmd.Name, IncludeAlwaysExecute: false}
92118
err := getAlertNotifications(existingQuery, sess)
93119

94120
if err != nil {
@@ -100,12 +126,13 @@ func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error
100126
}
101127

102128
alertNotification := &m.AlertNotification{
103-
OrgId: cmd.OrgID,
104-
Name: cmd.Name,
105-
Type: cmd.Type,
106-
Created: time.Now(),
107-
Settings: cmd.Settings,
108-
Updated: time.Now(),
129+
OrgId: cmd.OrgID,
130+
Name: cmd.Name,
131+
Type: cmd.Type,
132+
Created: time.Now(),
133+
Settings: cmd.Settings,
134+
Updated: time.Now(),
135+
AlwaysExecute: cmd.AlwaysExecute,
109136
}
110137

111138
_, err = sess.Insert(alertNotification)
@@ -114,7 +141,6 @@ func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error
114141
return err
115142
}
116143

117-
//alertNotification.Id = int(id)
118144
cmd.Result = alertNotification
119145
return nil
120146
})
@@ -137,9 +163,9 @@ func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
137163
alertNotification.Settings = cmd.Settings
138164
alertNotification.Updated = time.Now()
139165
alertNotification.Created = current.Created
166+
alertNotification.AlwaysExecute = cmd.AlwaysExecute
140167

141168
var affected int64
142-
//affected, err = sess.Id(alertNotification.Id).Cols("name", "type", "settings", "updated").Update(alertNotification)
143169
affected, err = sess.Id(alertNotification.Id).Update(alertNotification)
144170

145171
if err != nil {

pkg/services/sqlstore/alert_notification_test.go

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,19 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
2828

2929
Convey("Can save Alert Notification", func() {
3030
cmd := &m.CreateAlertNotificationCommand{
31-
Name: "ops",
32-
Type: "email",
33-
OrgID: 1,
34-
Settings: simplejson.New(),
31+
Name: "ops",
32+
Type: "email",
33+
OrgID: 1,
34+
Settings: simplejson.New(),
35+
AlwaysExecute: true,
3536
}
3637

3738
err = CreateAlertNotificationCommand(cmd)
3839
So(err, ShouldBeNil)
3940
So(cmd.Result.Id, ShouldNotEqual, 0)
4041
So(cmd.Result.OrgId, ShouldNotEqual, 0)
4142
So(cmd.Result.Type, ShouldEqual, "email")
43+
So(cmd.Result.AlwaysExecute, ShouldEqual, true)
4244

4345
Convey("Cannot save Alert Notification with the same name", func() {
4446
err = CreateAlertNotificationCommand(cmd)
@@ -47,11 +49,12 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
4749

4850
Convey("Can update alert notification", func() {
4951
newCmd := &m.UpdateAlertNotificationCommand{
50-
Name: "NewName",
51-
Type: "webhook",
52-
OrgID: cmd.Result.OrgId,
53-
Settings: simplejson.New(),
54-
Id: cmd.Result.Id,
52+
Name: "NewName",
53+
Type: "webhook",
54+
OrgID: cmd.Result.OrgId,
55+
Settings: simplejson.New(),
56+
Id: cmd.Result.Id,
57+
AlwaysExecute: true,
5558
}
5659
err := UpdateAlertNotification(newCmd)
5760
So(err, ShouldBeNil)
@@ -60,6 +63,14 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
6063
})
6164

6265
Convey("Can search using an array of ids", func() {
66+
So(CreateAlertNotificationCommand(&m.CreateAlertNotificationCommand{
67+
Name: "nagios",
68+
Type: "webhook",
69+
OrgID: 1,
70+
Settings: simplejson.New(),
71+
AlwaysExecute: true,
72+
}), ShouldBeNil)
73+
6374
So(CreateAlertNotificationCommand(&m.CreateAlertNotificationCommand{
6475
Name: "ops2",
6576
Type: "email",
@@ -75,14 +86,26 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
7586
}), ShouldBeNil)
7687

7788
Convey("search", func() {
89+
existingNotification := int64(2)
90+
missingThatSholdNotCauseerrors := int64(99)
91+
7892
query := &m.GetAlertNotificationQuery{
79-
Ids: []int64{1, 2, 3},
80-
OrgID: 1,
93+
Ids: []int64{existingNotification, missingThatSholdNotCauseerrors},
94+
OrgID: 1,
95+
IncludeAlwaysExecute: true,
8196
}
8297

8398
err := AlertNotificationQuery(query)
8499
So(err, ShouldBeNil)
85100
So(len(query.Result), ShouldEqual, 2)
101+
defaultNotifications := 0
102+
for _, not := range query.Result {
103+
if not.AlwaysExecute {
104+
defaultNotifications++
105+
}
106+
}
107+
108+
So(defaultNotifications, ShouldEqual, 1)
86109
})
87110
})
88111
})

pkg/services/sqlstore/migrations/alert_mig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func addAlertMigrations(mg *Migrator) {
7474
{Name: "org_id", Type: DB_BigInt, Nullable: false},
7575
{Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false},
7676
{Name: "type", Type: DB_NVarchar, Length: 255, Nullable: false},
77+
{Name: "always_execute", Type: DB_Bool, Nullable: false},
7778
{Name: "settings", Type: DB_Text, Nullable: false},
7879
{Name: "created", Type: DB_DateTime, Nullable: false},
7980
{Name: "updated", Type: DB_DateTime, Nullable: false},

0 commit comments

Comments
 (0)