Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejbranch committed Sep 19, 2022
1 parent f4d61d8 commit 373cc9e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dispatch/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ func (ag *aggrGroup) run(nf notifyFunc) {

// Wait the configured interval before calling flush again.
ag.mtx.Lock()
ag.next.Reset(ag.opts.GroupWait)
ag.next.Reset(ag.opts.GroupInterval)
ag.hasFlushed = true
ag.mtx.Unlock()

Expand Down
17 changes: 12 additions & 5 deletions notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,14 +573,15 @@ func hashAlert(a *types.Alert) uint64 {
return hash
}

func (n *DedupStage) needsUpdate(entry *nflogpb.Entry, firing, resolved map[uint64]struct{}, repeat, groupInterval time.Duration) bool {
func (n *DedupStage) needsUpdate(entry *nflogpb.Entry, firing, resolved map[uint64]struct{}, repeat, groupInterval time.Duration, now time.Time) bool {
// If we haven't notified about the alert group before, notify right away
// unless we only have resolved alerts.
if entry == nil {
return len(firing) > 0
}

groupIntervalMuted := len(entry.FiringAlerts) > 0 && entry.Timestamp.After(time.Now().Add(-groupInterval))
groupIntervalExpired := float64(now.UnixMilli()-entry.Timestamp.UnixMilli())/float64(groupInterval.Milliseconds()) >= 0.99
groupIntervalMuted := len(entry.FiringAlerts) > 0 && !groupIntervalExpired

if !entry.IsFiringSubset(firing) && !groupIntervalMuted {
return true
Expand All @@ -589,15 +590,15 @@ func (n *DedupStage) needsUpdate(entry *nflogpb.Entry, firing, resolved map[uint
// Notify about all alerts being resolved.
// This is done irrespective of the send_resolved flag to make sure that
// the firing alerts are cleared from the notification log.
if len(firing) == 0 {
if len(firing) == 0 && !groupIntervalMuted {
// If the current alert group and last notification contain no firing
// alert, it means that some alerts have been fired and resolved during the
// last interval. In this case, there is no need to notify the receiver
// since it doesn't know about them.
return len(entry.FiringAlerts) > 0
}

if n.rs.SendResolved() && !groupIntervalMuted {
if n.rs.SendResolved() && !entry.IsResolvedSubset(resolved) && !groupIntervalMuted {
return true
}

Expand All @@ -622,6 +623,11 @@ func (n *DedupStage) Exec(ctx context.Context, _ log.Logger, alerts ...*types.Al
return ctx, nil, errors.New("repeat interval missing")
}

now, ok := Now(ctx)
if !ok {
return ctx, nil, errors.New("now timestamp missing")
}

firingSet := map[uint64]struct{}{}
resolvedSet := map[uint64]struct{}{}
firing := []uint64{}
Expand Down Expand Up @@ -656,9 +662,10 @@ func (n *DedupStage) Exec(ctx context.Context, _ log.Logger, alerts ...*types.Al
return ctx, nil, errors.Errorf("unexpected entry result size %d", len(entries))
}

if n.needsUpdate(entry, firingSet, resolvedSet, repeatInterval, groupInterval) {
if n.needsUpdate(entry, firingSet, resolvedSet, repeatInterval, groupInterval, now) {
return ctx, alerts, nil
}

return ctx, nil, nil
}

Expand Down
10 changes: 8 additions & 2 deletions notify/notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func TestDedupStageNeedsUpdate(t *testing.T) {
now: func() time.Time { return now },
rs: sendResolved(c.resolve),
}
res := s.needsUpdate(c.entry, c.firingAlerts, c.resolvedAlerts, c.repeat, 2*time.Hour)
res := s.needsUpdate(c.entry, c.firingAlerts, c.resolvedAlerts, c.repeat, 2*time.Hour, now)
require.Equal(t, c.res, res)
}
}
Expand All @@ -234,6 +234,12 @@ func TestDedupStage(t *testing.T) {

ctx = WithGroupKey(ctx, "1")

_, _, err = s.Exec(ctx, log.NewNopLogger())
require.EqualError(t, err, "group interval missing")

ctx = WithGroupKey(ctx, "1")
ctx = WithGroupInterval(ctx, time.Hour)

_, _, err = s.Exec(ctx, log.NewNopLogger())
require.EqualError(t, err, "repeat interval missing")

Expand Down Expand Up @@ -288,7 +294,7 @@ func TestDedupStage(t *testing.T) {
qres: []*nflogpb.Entry{
{
FiringAlerts: []uint64{1, 2, 3, 4},
Timestamp: now,
Timestamp: now.Add(-time.Hour),
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions test/with_api_v2/acceptance/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ receivers:
am.Push(At(1), Alert("alertname", "test1"))

co1.Want(Between(2, 2.5), Alert("alertname", "test1").Active(1))
co1.Want(Between(6, 6.5), Alert("alertname", "test1").Active(1))
co1.Want(Between(5, 6.5), Alert("alertname", "test1").Active(1))

co2.Want(Between(6, 6.5), Alert("alertname", "test1").Active(1))
co2.Want(Between(5, 6.5), Alert("alertname", "test1").Active(1))

at.Run()

Expand Down

0 comments on commit 373cc9e

Please sign in to comment.