alert -> ok with reminders enabled should send

This commit is contained in:
Marcus Efraimsson 2018-09-28 18:34:20 +02:00
parent a0e1a1a1f9
commit 8551ffa0b0
No known key found for this signature in database
GPG Key ID: EBFE0FB04612DD4A
2 changed files with 61 additions and 43 deletions

View File

@ -51,19 +51,26 @@ func defaultShouldNotify(context *alerting.EvalContext, sendReminder bool, frequ
return false return false
} }
// Do not notify if interval has not elapsed if context.PrevAlertState == context.Rule.State && sendReminder {
lastNotify := time.Unix(notificationState.SentAt, 0) // Do not notify if interval has not elapsed
if sendReminder && !lastNotify.IsZero() && lastNotify.Add(frequency).After(time.Now()) { lastNotify := time.Unix(notificationState.SentAt, 0)
return false if !lastNotify.IsZero() && lastNotify.Add(frequency).After(time.Now()) {
} return false
}
// Do not notify if alert state if OK or pending even on repeated notify // Do not notify if alert state is OK or pending even on repeated notify
if sendReminder && (context.Rule.State == models.AlertStateOK || context.Rule.State == models.AlertStatePending) { if context.Rule.State == models.AlertStateOK || context.Rule.State == models.AlertStatePending {
return false return false
}
} }
// Do not notify when we become OK for the first time. // Do not notify when we become OK for the first time.
if (context.PrevAlertState == models.AlertStatePending) && (context.Rule.State == models.AlertStateOK) { if context.PrevAlertState == models.AlertStatePending && context.Rule.State == models.AlertStateOK {
return false
}
// Do not notify when we OK -> Pending
if context.PrevAlertState == models.AlertStateOK && context.Rule.State == models.AlertStatePending {
return false return false
} }

View File

@ -20,34 +20,34 @@ func TestShouldSendAlertNotification(t *testing.T) {
newState m.AlertStateType newState m.AlertStateType
sendReminder bool sendReminder bool
frequency time.Duration frequency time.Duration
journals *m.AlertNotificationState state *m.AlertNotificationState
expect bool expect bool
}{ }{
{ {
name: "pending -> ok should not trigger an notification", name: "pending -> ok should not trigger an notification",
newState: m.AlertStatePending, newState: m.AlertStateOK,
prevState: m.AlertStateOK, prevState: m.AlertStatePending,
sendReminder: false, sendReminder: false,
journals: &m.AlertNotificationState{}, state: &m.AlertNotificationState{},
expect: false, expect: false,
}, },
{ {
name: "ok -> alerting should trigger an notification", name: "ok -> alerting should trigger an notification",
newState: m.AlertStateOK, newState: m.AlertStateAlerting,
prevState: m.AlertStateAlerting, prevState: m.AlertStateOK,
sendReminder: false, sendReminder: false,
journals: &m.AlertNotificationState{}, state: &m.AlertNotificationState{},
expect: true, expect: true,
}, },
{ {
name: "ok -> pending should not trigger an notification", name: "ok -> pending should not trigger an notification",
newState: m.AlertStateOK, newState: m.AlertStatePending,
prevState: m.AlertStatePending, prevState: m.AlertStateOK,
sendReminder: false, sendReminder: false,
journals: &m.AlertNotificationState{}, state: &m.AlertNotificationState{},
expect: false, expect: false,
}, },
@ -56,66 +56,77 @@ func TestShouldSendAlertNotification(t *testing.T) {
newState: m.AlertStateOK, newState: m.AlertStateOK,
prevState: m.AlertStateOK, prevState: m.AlertStateOK,
sendReminder: false, sendReminder: false,
journals: &m.AlertNotificationState{}, state: &m.AlertNotificationState{},
expect: false, expect: false,
}, },
{
name: "ok -> alerting should trigger an notification",
newState: m.AlertStateOK,
prevState: m.AlertStateAlerting,
sendReminder: true,
journals: &m.AlertNotificationState{},
expect: true,
},
{ {
name: "ok -> ok with reminder should not trigger an notification", name: "ok -> ok with reminder should not trigger an notification",
newState: m.AlertStateOK, newState: m.AlertStateOK,
prevState: m.AlertStateOK, prevState: m.AlertStateOK,
sendReminder: true, sendReminder: true,
journals: &m.AlertNotificationState{}, state: &m.AlertNotificationState{},
expect: false, expect: false,
}, },
{ {
name: "alerting -> alerting with reminder and no journaling should trigger", name: "alerting -> ok should trigger an notification",
newState: m.AlertStateAlerting, newState: m.AlertStateOK,
prevState: m.AlertStateAlerting, prevState: m.AlertStateAlerting,
frequency: time.Minute * 10, sendReminder: false,
sendReminder: true, state: &m.AlertNotificationState{},
journals: &m.AlertNotificationState{},
expect: true, expect: true,
}, },
{ {
name: "alerting -> alerting with reminder and successful recent journal event should not trigger", name: "alerting -> ok should trigger an notification when reminders enabled",
newState: m.AlertStateOK,
prevState: m.AlertStateAlerting,
frequency: time.Minute * 10,
sendReminder: true,
state: &m.AlertNotificationState{SentAt: tnow.Add(-time.Minute).Unix()},
expect: true,
},
{
name: "alerting -> alerting with reminder and no state should trigger",
newState: m.AlertStateAlerting, newState: m.AlertStateAlerting,
prevState: m.AlertStateAlerting, prevState: m.AlertStateAlerting,
frequency: time.Minute * 10, frequency: time.Minute * 10,
sendReminder: true, sendReminder: true,
journals: &m.AlertNotificationState{SentAt: tnow.Add(-time.Minute).Unix()}, state: &m.AlertNotificationState{},
expect: true,
},
{
name: "alerting -> alerting with reminder and last notification sent 1 minute ago should not trigger",
newState: m.AlertStateAlerting,
prevState: m.AlertStateAlerting,
frequency: time.Minute * 10,
sendReminder: true,
state: &m.AlertNotificationState{SentAt: tnow.Add(-time.Minute).Unix()},
expect: false, expect: false,
}, },
{ {
name: "alerting -> alerting with reminder and failed recent journal event should trigger", name: "alerting -> alerting with reminder and last notifciation sent 11 minutes ago should trigger",
newState: m.AlertStateAlerting, newState: m.AlertStateAlerting,
prevState: m.AlertStateAlerting, prevState: m.AlertStateAlerting,
frequency: time.Minute * 10, frequency: time.Minute * 10,
sendReminder: true, sendReminder: true,
expect: true, state: &m.AlertNotificationState{SentAt: tnow.Add(-11 * time.Minute).Unix()},
journals: &m.AlertNotificationState{SentAt: tnow.Add(-time.Hour).Unix()},
expect: true,
}, },
} }
for _, tc := range tcs { for _, tc := range tcs {
evalContext := alerting.NewEvalContext(context.TODO(), &alerting.Rule{ evalContext := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
State: tc.newState, State: tc.prevState,
}) })
evalContext.Rule.State = tc.prevState evalContext.Rule.State = tc.newState
if defaultShouldNotify(evalContext, true, tc.frequency, tc.journals) != tc.expect { if defaultShouldNotify(evalContext, tc.sendReminder, tc.frequency, tc.state) != tc.expect {
t.Errorf("failed test %s.\n expected \n%+v \nto return: %v", tc.name, tc, tc.expect) t.Errorf("failed test %s.\n expected \n%+v \nto return: %v", tc.name, tc, tc.expect)
} }
} }