From 08be6a4149cb512d9f91ab942eacfdc049c6fc10 Mon Sep 17 00:00:00 2001 From: Max Leonard Inden Date: Mon, 29 May 2017 18:03:31 +0200 Subject: [PATCH] Expire pending silence and move to expired state Instead of setting endsAt to startsAt we can set both to now. Thereby the Silence will get the expired state by default. --- silence/silence.go | 4 +++- silence/silence_test.go | 9 +++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/silence/silence.go b/silence/silence.go index 8ae8ba5b..120c2df7 100644 --- a/silence/silence.go +++ b/silence/silence.go @@ -455,7 +455,9 @@ func (s *Silences) expire(id string) error { case StateActive: sil.EndsAt = now case StatePending: - sil.EndsAt = sil.StartsAt + // Set both to now to make Silence move to "expired" state + sil.StartsAt = now + sil.EndsAt = now } return s.setSilence(sil) diff --git a/silence/silence_test.go b/silence/silence_test.go index 50062b67..4c612a60 100644 --- a/silence/silence_test.go +++ b/silence/silence_test.go @@ -23,6 +23,7 @@ import ( "time" pb "github.com/prometheus/alertmanager/silence/silencepb" + "github.com/prometheus/alertmanager/types" "github.com/prometheus/common/model" "github.com/stretchr/testify/require" "github.com/weaveworks/mesh" @@ -752,10 +753,14 @@ func TestSilenceExpire(t *testing.T) { require.Equal(t, &pb.Silence{ Id: "pending", Matchers: []*pb.Matcher{m}, - StartsAt: now.Add(time.Minute), - EndsAt: now.Add(time.Minute), + StartsAt: now, + EndsAt: now, UpdatedAt: now, }, sil) + // Expiring a pending Silence should make the API return the + // SilenceStateExpired Silence state. + silenceState := types.CalcSilenceState(sil.StartsAt, sil.EndsAt) + require.Equal(t, silenceState, types.SilenceStateExpired) sil, err = s.QueryOne(QIDs("active")) require.NoError(t, err)