Просмотр исходного кода

notifications: make journaling ctx aware

bergquist 7 лет назад
Родитель
Сommit
f4b089d551

+ 50 - 42
pkg/services/alerting/notifiers/base_test.go

@@ -11,56 +11,64 @@ import (
 	. "github.com/smartystreets/goconvey/convey"
 	. "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)
 		})
 		})
 	})
 	})
 }
 }

+ 1 - 1
pkg/services/alerting/result_handler.go

@@ -95,7 +95,7 @@ func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error {
 				NotifierId: notifierId,
 				NotifierId: notifierId,
 				OrgId:      evalContext.Rule.OrgId,
 				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)
 				handler.log.Error("Failed to clean up old notification records", "notifier", notifierId, "alert", evalContext.Rule.Id, "Error", err)
 			}
 			}
 		}
 		}

+ 10 - 9
pkg/services/sqlstore/alert_notification.go

@@ -2,6 +2,7 @@ package sqlstore
 
 
 import (
 import (
 	"bytes"
 	"bytes"
+	"context"
 	"fmt"
 	"fmt"
 	"strings"
 	"strings"
 	"time"
 	"time"
@@ -17,9 +18,9 @@ func init() {
 	bus.AddHandler("sql", DeleteAlertNotification)
 	bus.AddHandler("sql", DeleteAlertNotification)
 	bus.AddHandler("sql", GetAlertNotificationsToSend)
 	bus.AddHandler("sql", GetAlertNotificationsToSend)
 	bus.AddHandler("sql", GetAllAlertNotifications)
 	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 {
 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{
 		journalEntry := &m.AlertNotificationJournal{
 			OrgId:      cmd.OrgId,
 			OrgId:      cmd.OrgId,
 			AlertId:    cmd.AlertId,
 			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{}
 		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)
 		_, 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 {
 		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 = ?"
 		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)
 		_, err := sess.Exec(sql, cmd.OrgId, cmd.AlertId, cmd.NotifierId)
 		return err
 		return err

+ 4 - 0
pkg/services/sqlstore/transactions.go

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