notifications: make journaling ctx aware

pull/12145/head
bergquist 8 years ago
parent 7632983c62
commit f4b089d551
  1. 92
      pkg/services/alerting/notifiers/base_test.go
  2. 2
      pkg/services/alerting/result_handler.go
  3. 19
      pkg/services/sqlstore/alert_notification.go
  4. 4
      pkg/services/sqlstore/transactions.go

@ -11,56 +11,64 @@ import (
. "github.com/smartystreets/goconvey/convey"
)
func TestBaseNotifier(t *testing.T) {
Convey("Base notifier tests", t, func() {
Convey("default constructor for notifiers", func() {
bJson := simplejson.New()
model := &m.AlertNotification{
Id: 1,
Name: "name",
Type: "email",
Settings: bJson,
}
func TestShouldSendAlertNotification(t *testing.T) {
tcs := []struct {
prevState m.AlertStateType
newState m.AlertStateType
expected bool
}{
{
newState: m.AlertStatePending,
prevState: m.AlertStateOK,
expected: false,
},
{
newState: m.AlertStateOK,
prevState: m.AlertStateAlerting,
expected: true,
},
}
Convey("can parse false value", func() {
bJson.Set("uploadImage", false)
for _, tc := range tcs {
context := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
State: tc.newState,
})
context.Rule.State = tc.prevState
timeNow := time.Now()
if defaultShouldNotify(context, true, 0, &timeNow) != tc.expected {
t.Errorf("expected %v to return %v", tc, tc.expected)
}
}
}
base := NewNotifierBase(model)
So(base.UploadImage, ShouldBeFalse)
})
func TestBaseNotifier(t *testing.T) {
Convey("default constructor for notifiers", t, func() {
bJson := simplejson.New()
Convey("can parse true value", func() {
bJson.Set("uploadImage", true)
model := &m.AlertNotification{
Id: 1,
Name: "name",
Type: "email",
Settings: bJson,
}
base := NewNotifierBase(model)
So(base.UploadImage, ShouldBeTrue)
})
Convey("can parse false value", func() {
bJson.Set("uploadImage", false)
Convey("default value should be true for backwards compatibility", func() {
base := NewNotifierBase(model)
So(base.UploadImage, ShouldBeTrue)
})
base := NewNotifierBase(model)
So(base.UploadImage, ShouldBeFalse)
})
Convey("should notify", func() {
Convey("pending -> ok", func() {
context := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
State: m.AlertStatePending,
})
context.Rule.State = m.AlertStateOK
timeNow := time.Now()
So(defaultShouldNotify(context, true, 0, &timeNow), ShouldBeFalse)
})
Convey("can parse true value", func() {
bJson.Set("uploadImage", true)
base := NewNotifierBase(model)
So(base.UploadImage, ShouldBeTrue)
})
Convey("ok -> alerting", func() {
context := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
State: m.AlertStateOK,
})
context.Rule.State = m.AlertStateAlerting
timeNow := time.Now()
So(defaultShouldNotify(context, true, 0, &timeNow), ShouldBeTrue)
})
Convey("default value should be true for backwards compatibility", func() {
base := NewNotifierBase(model)
So(base.UploadImage, ShouldBeTrue)
})
})
}

@ -95,7 +95,7 @@ func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error {
NotifierId: notifierId,
OrgId: evalContext.Rule.OrgId,
}
if err := bus.Dispatch(cmd); err != nil {
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
handler.log.Error("Failed to clean up old notification records", "notifier", notifierId, "alert", evalContext.Rule.Id, "Error", err)
}
}

@ -2,6 +2,7 @@ package sqlstore
import (
"bytes"
"context"
"fmt"
"strings"
"time"
@ -17,9 +18,9 @@ func init() {
bus.AddHandler("sql", DeleteAlertNotification)
bus.AddHandler("sql", GetAlertNotificationsToSend)
bus.AddHandler("sql", GetAllAlertNotifications)
bus.AddHandler("sql", RecordNotificationJournal)
bus.AddHandler("sql", GetLatestNotification)
bus.AddHandler("sql", CleanNotificationJournal)
bus.AddHandlerCtx("sql", RecordNotificationJournal)
bus.AddHandlerCtx("sql", GetLatestNotification)
bus.AddHandlerCtx("sql", CleanNotificationJournal)
}
func DeleteAlertNotification(cmd *m.DeleteAlertNotificationCommand) error {
@ -228,8 +229,8 @@ func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
})
}
func RecordNotificationJournal(cmd *m.RecordNotificationJournalCommand) error {
return inTransaction(func(sess *DBSession) error {
func RecordNotificationJournal(ctx context.Context, cmd *m.RecordNotificationJournalCommand) error {
return inTransactionCtx(ctx, func(sess *DBSession) error {
journalEntry := &m.AlertNotificationJournal{
OrgId: cmd.OrgId,
AlertId: cmd.AlertId,
@ -246,8 +247,8 @@ func RecordNotificationJournal(cmd *m.RecordNotificationJournalCommand) error {
})
}
func GetLatestNotification(cmd *m.GetLatestNotificationQuery) error {
return inTransaction(func(sess *DBSession) error {
func GetLatestNotification(ctx context.Context, cmd *m.GetLatestNotificationQuery) error {
return inTransactionCtx(ctx, func(sess *DBSession) error {
notificationJournal := &m.AlertNotificationJournal{}
_, err := sess.Desc("alert_notification_journal.sent_at").Limit(1).Where("alert_notification_journal.org_id = ? AND alert_notification_journal.alert_id = ? AND alert_notification_journal.notifier_id = ?", cmd.OrgId, cmd.AlertId, cmd.NotifierId).Get(notificationJournal)
if err != nil {
@ -259,8 +260,8 @@ func GetLatestNotification(cmd *m.GetLatestNotificationQuery) error {
})
}
func CleanNotificationJournal(cmd *m.CleanNotificationJournalCommand) error {
return inTransaction(func(sess *DBSession) error {
func CleanNotificationJournal(ctx context.Context, cmd *m.CleanNotificationJournalCommand) error {
return inTransactionCtx(ctx, func(sess *DBSession) error {
sql := "DELETE FROM alert_notification_journal WHERE notification_journal.org_id = ? AND alert_notification_journal.alert_id = ? AND alert_notification_journal.notifier_id = ?"
_, err := sess.Exec(sql, cmd.OrgId, cmd.AlertId, cmd.NotifierId)
return err

@ -103,3 +103,7 @@ func inTransactionWithRetryCtx(ctx context.Context, callback dbTransactionFunc,
func inTransaction(callback dbTransactionFunc) error {
return inTransactionWithRetry(callback, 0)
}
func inTransactionCtx(ctx context.Context, callback dbTransactionFunc) error {
return inTransactionWithRetryCtx(ctx, callback, 0)
}

Loading…
Cancel
Save