Alerting: Fix send resolved notifications (#54793)

This commit fixes a bug where we did not send resolved alerts to Alertmanager for resolved alert instances. This meant that resolved notifications did not have the annotations from the resolved state, and a result did not also have the resolved screenshot.
pull/55260/head
George Robinson 3 years ago committed by GitHub
parent 099d3cdf72
commit 5561f935e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      pkg/services/ngalert/state/state.go
  2. 15
      pkg/services/ngalert/state/state_test.go

@ -174,12 +174,18 @@ func (a *State) resultNoData(alertRule *models.AlertRule, result eval.Result) {
} }
func (a *State) NeedsSending(resendDelay time.Duration) bool { func (a *State) NeedsSending(resendDelay time.Duration) bool {
if a.State == eval.Pending || a.State == eval.Normal && !a.Resolved { switch a.State {
case eval.Pending:
// We do not send notifications for pending states
return false return false
case eval.Normal:
// We should send a notification if the state is Normal because it was resolved
return a.Resolved
default:
// We should send, and re-send notifications, each time LastSentAt is <= LastEvaluationTime + resendDelay
nextSent := a.LastSentAt.Add(resendDelay)
return nextSent.Before(a.LastEvaluationTime) || nextSent.Equal(a.LastEvaluationTime)
} }
// if LastSentAt is before or equal to LastEvaluationTime + resendDelay, send again
nextSent := a.LastSentAt.Add(resendDelay)
return nextSent.Before(a.LastEvaluationTime) || nextSent.Equal(a.LastEvaluationTime)
} }
func (a *State) Equals(b *State) bool { func (a *State) Equals(b *State) bool {

@ -71,25 +71,14 @@ func TestNeedsSending(t *testing.T) {
}, },
}, },
{ {
name: "state: normal + resolved sends after a minute", name: "state: normal + resolved should send without waiting",
resendDelay: 1 * time.Minute, resendDelay: 1 * time.Minute,
expected: true, expected: true,
testState: &State{ testState: &State{
State: eval.Normal, State: eval.Normal,
Resolved: true, Resolved: true,
LastEvaluationTime: evaluationTime, LastEvaluationTime: evaluationTime,
LastSentAt: evaluationTime.Add(-1 * time.Minute), LastSentAt: evaluationTime,
},
},
{
name: "state: normal + resolved does _not_ send after 30 seconds (before one minute)",
resendDelay: 1 * time.Minute,
expected: false,
testState: &State{
State: eval.Normal,
Resolved: true,
LastEvaluationTime: evaluationTime,
LastSentAt: evaluationTime.Add(-30 * time.Second),
}, },
}, },
{ {

Loading…
Cancel
Save