2016-08-15 14:10:35 +00:00
|
|
|
// Copyright 2016 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package silence
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-06-24 14:11:10 +00:00
|
|
|
"fmt"
|
2016-08-15 14:10:35 +00:00
|
|
|
"os"
|
2022-03-28 04:46:58 +00:00
|
|
|
"runtime"
|
2016-08-15 14:10:35 +00:00
|
|
|
"sort"
|
2024-05-31 16:52:44 +00:00
|
|
|
"strings"
|
2023-02-24 11:46:20 +00:00
|
|
|
"sync"
|
2016-08-15 14:10:35 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-08-27 10:21:50 +00:00
|
|
|
"github.com/coder/quartz"
|
2021-07-30 08:11:43 +00:00
|
|
|
"github.com/go-kit/log"
|
2018-03-22 12:53:00 +00:00
|
|
|
"github.com/matttproud/golang_protobuf_extensions/pbutil"
|
2021-05-26 17:33:52 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2023-03-08 12:32:59 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/testutil"
|
2016-09-08 14:02:27 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2016-08-15 14:10:35 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-02-24 12:11:50 +00:00
|
|
|
"go.uber.org/atomic"
|
2022-03-25 16:59:51 +00:00
|
|
|
|
2023-11-24 10:01:40 +00:00
|
|
|
"github.com/prometheus/alertmanager/featurecontrol"
|
2024-06-21 14:17:27 +00:00
|
|
|
"github.com/prometheus/alertmanager/matcher/compat"
|
2022-03-25 16:59:51 +00:00
|
|
|
pb "github.com/prometheus/alertmanager/silence/silencepb"
|
|
|
|
"github.com/prometheus/alertmanager/types"
|
2016-08-15 14:10:35 +00:00
|
|
|
)
|
|
|
|
|
2019-04-29 08:54:40 +00:00
|
|
|
func checkErr(t *testing.T, expected string, got error) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
if expected == "" {
|
|
|
|
require.NoError(t, got)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if got == nil {
|
|
|
|
t.Errorf("expected error containing %q but got none", expected)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Contains(t, got.Error(), expected)
|
|
|
|
}
|
|
|
|
|
2016-08-15 14:10:35 +00:00
|
|
|
func TestOptionsValidate(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
options *Options
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
options: &Options{
|
|
|
|
SnapshotReader: &bytes.Buffer{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
options: &Options{
|
|
|
|
SnapshotFile: "test.bkp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
options: &Options{
|
|
|
|
SnapshotFile: "test bkp",
|
|
|
|
SnapshotReader: &bytes.Buffer{},
|
|
|
|
},
|
|
|
|
err: "only one of SnapshotFile and SnapshotReader must be set",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2019-04-29 08:54:40 +00:00
|
|
|
checkErr(t, c.err, c.options.validate())
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-21 16:10:15 +00:00
|
|
|
func TestSilenceGCOverTime(t *testing.T) {
|
|
|
|
t.Run("GC does not remove active silences", func(t *testing.T) {
|
|
|
|
s, err := New(Options{})
|
|
|
|
require.NoError(t, err)
|
2024-08-27 10:21:50 +00:00
|
|
|
s.clock = quartz.NewMock(t)
|
2024-08-21 16:10:15 +00:00
|
|
|
now := s.nowUTC()
|
|
|
|
s.st = state{
|
|
|
|
"1": &pb.MeshSilence{Silence: &pb.Silence{Id: "1"}, ExpiresAt: now},
|
|
|
|
"2": &pb.MeshSilence{Silence: &pb.Silence{Id: "2"}, ExpiresAt: now.Add(-time.Second)},
|
|
|
|
"3": &pb.MeshSilence{Silence: &pb.Silence{Id: "3"}, ExpiresAt: now.Add(time.Second)},
|
|
|
|
}
|
|
|
|
want := state{
|
|
|
|
"3": &pb.MeshSilence{Silence: &pb.Silence{Id: "3"}, ExpiresAt: now.Add(time.Second)},
|
|
|
|
}
|
|
|
|
n, err := s.GC()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, n)
|
|
|
|
require.Equal(t, want, s.st)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("GC does not leak cache entries", func(t *testing.T) {
|
|
|
|
s, err := New(Options{})
|
|
|
|
require.NoError(t, err)
|
2024-08-27 10:21:50 +00:00
|
|
|
clock := quartz.NewMock(t)
|
2024-08-21 16:10:15 +00:00
|
|
|
s.clock = clock
|
|
|
|
sil1 := &pb.Silence{
|
|
|
|
Matchers: []*pb.Matcher{{
|
|
|
|
Type: pb.Matcher_EQUAL,
|
|
|
|
Name: "foo",
|
|
|
|
Pattern: "bar",
|
|
|
|
}},
|
|
|
|
StartsAt: clock.Now(),
|
|
|
|
EndsAt: clock.Now().Add(time.Minute),
|
|
|
|
}
|
|
|
|
require.NoError(t, s.Set(sil1))
|
|
|
|
// Need to query the silence to populate the matcher cache.
|
|
|
|
s.Query(QMatches(model.LabelSet{"foo": "bar"}))
|
|
|
|
require.Len(t, s.st, 1)
|
|
|
|
require.Len(t, s.mc, 1)
|
|
|
|
// Move time forward and both silence and cache entry should be garbage
|
|
|
|
// collected.
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(time.Minute)
|
2024-08-21 16:10:15 +00:00
|
|
|
n, err := s.GC()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, n)
|
|
|
|
require.Empty(t, s.st)
|
|
|
|
require.Empty(t, s.mc)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("replacing a silences does not leak cache entries", func(t *testing.T) {
|
|
|
|
s, err := New(Options{})
|
|
|
|
require.NoError(t, err)
|
2024-08-27 10:21:50 +00:00
|
|
|
clock := quartz.NewMock(t)
|
2024-08-21 16:10:15 +00:00
|
|
|
s.clock = clock
|
|
|
|
sil1 := &pb.Silence{
|
|
|
|
Matchers: []*pb.Matcher{{
|
|
|
|
Type: pb.Matcher_EQUAL,
|
|
|
|
Name: "foo",
|
|
|
|
Pattern: "bar",
|
|
|
|
}},
|
|
|
|
StartsAt: clock.Now(),
|
|
|
|
EndsAt: clock.Now().Add(time.Minute),
|
|
|
|
}
|
|
|
|
require.NoError(t, s.Set(sil1))
|
|
|
|
// Need to query the silence to populate the matcher cache.
|
|
|
|
s.Query(QMatches(model.LabelSet{"foo": "bar"}))
|
|
|
|
require.Len(t, s.st, 1)
|
|
|
|
require.Len(t, s.mc, 1)
|
|
|
|
// must clone sil1 before replacing it.
|
|
|
|
sil2 := cloneSilence(sil1)
|
|
|
|
sil2.Matchers = []*pb.Matcher{{
|
|
|
|
Type: pb.Matcher_EQUAL,
|
|
|
|
Name: "bar",
|
|
|
|
Pattern: "baz",
|
|
|
|
}}
|
|
|
|
require.NoError(t, s.Set(sil2))
|
|
|
|
// Need to query the silence to populate the matcher cache.
|
|
|
|
s.Query(QMatches(model.LabelSet{"bar": "baz"}))
|
|
|
|
require.Len(t, s.st, 2)
|
|
|
|
require.Len(t, s.mc, 2)
|
|
|
|
// Move time forward and both silence and cache entry should be garbage
|
|
|
|
// collected.
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(time.Minute)
|
2024-08-21 16:10:15 +00:00
|
|
|
n, err := s.GC()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, n)
|
|
|
|
require.Empty(t, s.st)
|
|
|
|
require.Empty(t, s.mc)
|
|
|
|
})
|
|
|
|
|
|
|
|
// This test checks for a memory leak that occurred in the matcher cache when
|
|
|
|
// updating an existing silence.
|
|
|
|
t.Run("updating a silences does not leak cache entries", func(t *testing.T) {
|
|
|
|
s, err := New(Options{})
|
|
|
|
require.NoError(t, err)
|
2024-08-27 10:21:50 +00:00
|
|
|
clock := quartz.NewMock(t)
|
2024-08-21 16:10:15 +00:00
|
|
|
s.clock = clock
|
|
|
|
sil1 := &pb.Silence{
|
|
|
|
Id: "1",
|
|
|
|
Matchers: []*pb.Matcher{{
|
|
|
|
Type: pb.Matcher_EQUAL,
|
|
|
|
Name: "foo",
|
|
|
|
Pattern: "bar",
|
|
|
|
}},
|
|
|
|
StartsAt: clock.Now(),
|
|
|
|
EndsAt: clock.Now().Add(time.Minute),
|
|
|
|
}
|
|
|
|
s.st["1"] = &pb.MeshSilence{Silence: sil1, ExpiresAt: clock.Now().Add(time.Minute)}
|
|
|
|
// Need to query the silence to populate the matcher cache.
|
|
|
|
s.Query(QMatches(model.LabelSet{"foo": "bar"}))
|
|
|
|
require.Len(t, s.mc, 1)
|
|
|
|
// must clone sil1 before updating it.
|
|
|
|
sil2 := cloneSilence(sil1)
|
|
|
|
require.NoError(t, s.Set(sil2))
|
|
|
|
// The memory leak occurred because updating a silence would add a new
|
|
|
|
// entry in the matcher cache even though no new silence was created.
|
|
|
|
// This check asserts that this no longer happens.
|
|
|
|
s.Query(QMatches(model.LabelSet{"foo": "bar"}))
|
|
|
|
require.Len(t, s.st, 1)
|
|
|
|
require.Len(t, s.mc, 1)
|
|
|
|
// Move time forward and both silence and cache entry should be garbage
|
|
|
|
// collected.
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(time.Minute)
|
2024-08-21 16:10:15 +00:00
|
|
|
n, err := s.GC()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, n)
|
|
|
|
require.Empty(t, s.st)
|
|
|
|
require.Empty(t, s.mc)
|
|
|
|
})
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSilencesSnapshot(t *testing.T) {
|
|
|
|
// Check whether storing and loading the snapshot is symmetric.
|
2024-08-27 10:21:50 +00:00
|
|
|
now := quartz.NewMock(t).Now().UTC()
|
2016-08-15 14:10:35 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
entries []*pb.MeshSilence
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
entries: []*pb.MeshSilence{
|
|
|
|
{
|
|
|
|
Silence: &pb.Silence{
|
|
|
|
Id: "3be80475-e219-4ee7-b6fc-4b65114e362f",
|
|
|
|
Matchers: []*pb.Matcher{
|
|
|
|
{Name: "label1", Pattern: "val1", Type: pb.Matcher_EQUAL},
|
|
|
|
{Name: "label2", Pattern: "val.+", Type: pb.Matcher_REGEXP},
|
|
|
|
},
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: now,
|
|
|
|
EndsAt: now,
|
|
|
|
UpdatedAt: now,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-04-18 08:46:40 +00:00
|
|
|
ExpiresAt: now,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2021-01-18 20:58:20 +00:00
|
|
|
{
|
|
|
|
Silence: &pb.Silence{
|
|
|
|
Id: "3dfb2528-59ce-41eb-b465-f875a4e744a4",
|
|
|
|
Matchers: []*pb.Matcher{
|
|
|
|
{Name: "label1", Pattern: "val1", Type: pb.Matcher_NOT_EQUAL},
|
|
|
|
{Name: "label2", Pattern: "val.+", Type: pb.Matcher_NOT_REGEXP},
|
|
|
|
},
|
|
|
|
StartsAt: now,
|
|
|
|
EndsAt: now,
|
|
|
|
UpdatedAt: now,
|
|
|
|
},
|
|
|
|
ExpiresAt: now,
|
|
|
|
},
|
2016-08-15 14:10:35 +00:00
|
|
|
{
|
|
|
|
Silence: &pb.Silence{
|
|
|
|
Id: "4b1e760d-182c-4980-b873-c1a6827c9817",
|
|
|
|
Matchers: []*pb.Matcher{
|
|
|
|
{Name: "label1", Pattern: "val1", Type: pb.Matcher_EQUAL},
|
|
|
|
},
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: now.Add(time.Hour),
|
|
|
|
EndsAt: now.Add(2 * time.Hour),
|
|
|
|
UpdatedAt: now,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-04-18 08:46:40 +00:00
|
|
|
ExpiresAt: now.Add(24 * time.Hour),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2022-07-18 12:25:32 +00:00
|
|
|
f, err := os.CreateTemp("", "snapshot")
|
2016-08-15 14:10:35 +00:00
|
|
|
require.NoError(t, err, "creating temp file failed")
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
s1 := &Silences{st: state{}, metrics: newMetrics(nil, nil)}
|
2016-08-15 14:10:35 +00:00
|
|
|
// Setup internal state manually.
|
|
|
|
for _, e := range c.entries {
|
2018-02-07 15:36:47 +00:00
|
|
|
s1.st[e.Silence.Id] = e
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
_, err = s1.Snapshot(f)
|
|
|
|
require.NoError(t, err, "creating snapshot failed")
|
|
|
|
|
|
|
|
require.NoError(t, f.Close(), "closing snapshot file failed")
|
|
|
|
|
|
|
|
f, err = os.Open(f.Name())
|
|
|
|
require.NoError(t, err, "opening snapshot file failed")
|
|
|
|
|
|
|
|
// Check again against new nlog instance.
|
2018-02-07 15:36:47 +00:00
|
|
|
s2 := &Silences{mc: matcherCache{}, st: state{}}
|
2016-08-15 14:10:35 +00:00
|
|
|
err = s2.loadSnapshot(f)
|
|
|
|
require.NoError(t, err, "error loading snapshot")
|
2018-02-07 15:36:47 +00:00
|
|
|
require.Equal(t, s1.st, s2.st, "state after loading snapshot did not match snapshotted state")
|
2016-08-15 14:10:35 +00:00
|
|
|
|
|
|
|
require.NoError(t, f.Close(), "closing snapshot file failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 14:12:48 +00:00
|
|
|
// This tests a regression introduced by https://github.com/prometheus/alertmanager/pull/2689.
|
|
|
|
func TestSilences_Maintenance_DefaultMaintenanceFuncDoesntCrash(t *testing.T) {
|
2022-07-18 12:25:32 +00:00
|
|
|
f, err := os.CreateTemp("", "snapshot")
|
2021-09-13 14:12:48 +00:00
|
|
|
require.NoError(t, err, "creating temp file failed")
|
2024-08-27 10:21:50 +00:00
|
|
|
clock := quartz.NewMock(t)
|
2022-03-28 04:46:58 +00:00
|
|
|
s := &Silences{st: state{}, logger: log.NewNopLogger(), clock: clock, metrics: newMetrics(nil, nil)}
|
2021-09-13 14:12:48 +00:00
|
|
|
stopc := make(chan struct{})
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
s.Maintenance(100*time.Millisecond, f.Name(), stopc, nil)
|
|
|
|
close(done)
|
|
|
|
}()
|
2022-03-28 04:46:58 +00:00
|
|
|
runtime.Gosched()
|
2021-09-13 14:12:48 +00:00
|
|
|
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(100 * time.Millisecond)
|
2021-09-13 14:12:48 +00:00
|
|
|
close(stopc)
|
|
|
|
|
|
|
|
<-done
|
|
|
|
}
|
|
|
|
|
2021-09-06 10:49:39 +00:00
|
|
|
func TestSilences_Maintenance_SupportsCustomCallback(t *testing.T) {
|
2022-07-18 12:25:32 +00:00
|
|
|
f, err := os.CreateTemp("", "snapshot")
|
2021-09-06 10:49:39 +00:00
|
|
|
require.NoError(t, err, "creating temp file failed")
|
2024-08-27 10:21:50 +00:00
|
|
|
clock := quartz.NewMock(t)
|
2023-03-08 12:32:59 +00:00
|
|
|
reg := prometheus.NewRegistry()
|
|
|
|
s := &Silences{st: state{}, logger: log.NewNopLogger(), clock: clock}
|
|
|
|
s.metrics = newMetrics(reg, s)
|
2021-09-06 10:49:39 +00:00
|
|
|
stopc := make(chan struct{})
|
|
|
|
|
2023-02-24 11:46:20 +00:00
|
|
|
var calls atomic.Int32
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(1)
|
2022-07-05 14:00:13 +00:00
|
|
|
go func() {
|
2023-02-24 11:46:20 +00:00
|
|
|
defer wg.Done()
|
|
|
|
s.Maintenance(10*time.Second, f.Name(), stopc, func() (int64, error) {
|
|
|
|
calls.Add(1)
|
2022-07-05 14:00:13 +00:00
|
|
|
return 0, nil
|
|
|
|
})
|
|
|
|
}()
|
2023-02-24 11:46:20 +00:00
|
|
|
gosched()
|
2022-03-28 04:46:58 +00:00
|
|
|
|
2023-02-24 11:46:20 +00:00
|
|
|
// Before the first tick, no maintenance executed.
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(9 * time.Second)
|
2023-02-24 11:46:20 +00:00
|
|
|
require.EqualValues(t, 0, calls.Load())
|
|
|
|
|
|
|
|
// Tick once.
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(1 * time.Second)
|
|
|
|
require.Eventually(t, func() bool { return calls.Load() == 1 }, 5*time.Second, time.Second)
|
2021-09-06 10:49:39 +00:00
|
|
|
|
2022-03-28 04:46:58 +00:00
|
|
|
// Stop the maintenance loop. We should get exactly one more execution of the maintenance func.
|
2021-09-06 10:49:39 +00:00
|
|
|
close(stopc)
|
2023-02-24 11:46:20 +00:00
|
|
|
wg.Wait()
|
2021-09-06 10:49:39 +00:00
|
|
|
|
2023-02-24 11:46:20 +00:00
|
|
|
require.EqualValues(t, 2, calls.Load())
|
2023-03-08 12:32:59 +00:00
|
|
|
|
|
|
|
// Check the maintenance metrics.
|
|
|
|
require.NoError(t, testutil.GatherAndCompare(reg, bytes.NewBufferString(`
|
|
|
|
# HELP alertmanager_silences_maintenance_errors_total How many maintenances were executed for silences that failed.
|
|
|
|
# TYPE alertmanager_silences_maintenance_errors_total counter
|
|
|
|
alertmanager_silences_maintenance_errors_total 0
|
|
|
|
# HELP alertmanager_silences_maintenance_total How many maintenances were executed for silences.
|
|
|
|
# TYPE alertmanager_silences_maintenance_total counter
|
|
|
|
alertmanager_silences_maintenance_total 2
|
|
|
|
`), "alertmanager_silences_maintenance_total", "alertmanager_silences_maintenance_errors_total"))
|
2021-09-06 10:49:39 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 14:10:35 +00:00
|
|
|
func TestSilencesSetSilence(t *testing.T) {
|
|
|
|
s, err := New(Options{
|
|
|
|
Retention: time.Minute,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-08-27 10:21:50 +00:00
|
|
|
clock := quartz.NewMock(t)
|
2022-03-28 04:46:58 +00:00
|
|
|
s.clock = clock
|
|
|
|
|
|
|
|
nowpb := s.nowUTC()
|
2016-08-15 14:10:35 +00:00
|
|
|
|
|
|
|
sil := &pb.Silence{
|
2017-05-16 14:48:25 +00:00
|
|
|
Id: "some_id",
|
|
|
|
Matchers: []*pb.Matcher{{Name: "abc", Pattern: "def"}},
|
|
|
|
StartsAt: nowpb,
|
|
|
|
EndsAt: nowpb,
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
want := state{
|
|
|
|
"some_id": &pb.MeshSilence{
|
|
|
|
Silence: sil,
|
2022-03-28 04:46:58 +00:00
|
|
|
ExpiresAt: nowpb.Add(time.Minute),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-03-28 04:46:58 +00:00
|
|
|
done := make(chan struct{})
|
2018-02-07 15:36:47 +00:00
|
|
|
s.broadcast = func(b []byte) {
|
|
|
|
var e pb.MeshSilence
|
2018-03-22 12:53:00 +00:00
|
|
|
r := bytes.NewReader(b)
|
|
|
|
_, err := pbutil.ReadDelimited(r, &e)
|
2018-02-07 15:36:47 +00:00
|
|
|
require.NoError(t, err)
|
2017-09-27 09:48:28 +00:00
|
|
|
|
2023-12-10 08:33:13 +00:00
|
|
|
require.Equal(t, &e, want["some_id"])
|
2018-02-07 15:36:47 +00:00
|
|
|
close(done)
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2017-09-27 09:48:28 +00:00
|
|
|
|
2022-03-28 04:46:58 +00:00
|
|
|
// setSilence() is always called with s.mtx locked() in the application code
|
|
|
|
func() {
|
2017-09-27 09:48:28 +00:00
|
|
|
s.mtx.Lock()
|
2022-03-28 04:46:58 +00:00
|
|
|
defer s.mtx.Unlock()
|
2024-06-25 12:23:09 +00:00
|
|
|
require.NoError(t, s.setSilence(s.toMeshSilence(sil), nowpb))
|
2017-09-27 09:48:28 +00:00
|
|
|
}()
|
|
|
|
|
2022-03-28 04:46:58 +00:00
|
|
|
// Ensure broadcast was called.
|
|
|
|
if _, isOpen := <-done; isOpen {
|
|
|
|
t.Fatal("broadcast was not called")
|
2017-09-27 09:48:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
require.Equal(t, want, s.st, "Unexpected silence state")
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 14:48:25 +00:00
|
|
|
func TestSilenceSet(t *testing.T) {
|
2016-08-15 14:10:35 +00:00
|
|
|
s, err := New(Options{
|
|
|
|
Retention: time.Hour,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-08-27 10:21:50 +00:00
|
|
|
clock := quartz.NewMock(t)
|
2022-03-28 04:46:58 +00:00
|
|
|
s.clock = clock
|
|
|
|
start1 := s.nowUTC()
|
2016-08-15 14:10:35 +00:00
|
|
|
|
|
|
|
// Insert silence with fixed start time.
|
|
|
|
sil1 := &pb.Silence{
|
|
|
|
Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}},
|
2022-03-28 04:46:58 +00:00
|
|
|
StartsAt: start1.Add(2 * time.Minute),
|
|
|
|
EndsAt: start1.Add(5 * time.Minute),
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2024-06-17 10:45:31 +00:00
|
|
|
require.NoError(t, s.Set(sil1))
|
|
|
|
require.NotEqual(t, "", sil1.Id)
|
2016-08-15 14:10:35 +00:00
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
want := state{
|
2024-06-17 10:45:31 +00:00
|
|
|
sil1.Id: &pb.MeshSilence{
|
2018-02-07 15:36:47 +00:00
|
|
|
Silence: &pb.Silence{
|
2024-06-17 10:45:31 +00:00
|
|
|
Id: sil1.Id,
|
2018-02-07 15:36:47 +00:00
|
|
|
Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}},
|
2022-03-28 04:46:58 +00:00
|
|
|
StartsAt: start1.Add(2 * time.Minute),
|
|
|
|
EndsAt: start1.Add(5 * time.Minute),
|
|
|
|
UpdatedAt: start1,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2022-03-28 04:46:58 +00:00
|
|
|
ExpiresAt: start1.Add(5*time.Minute + s.retention),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
require.Equal(t, want, s.st, "unexpected state after silence creation")
|
2016-08-15 14:10:35 +00:00
|
|
|
|
|
|
|
// Insert silence with unset start time. Must be set to now.
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(time.Minute)
|
2022-03-28 04:46:58 +00:00
|
|
|
start2 := s.nowUTC()
|
2017-05-16 14:48:25 +00:00
|
|
|
|
2016-08-15 14:10:35 +00:00
|
|
|
sil2 := &pb.Silence{
|
|
|
|
Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}},
|
2022-03-28 04:46:58 +00:00
|
|
|
EndsAt: start2.Add(1 * time.Minute),
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2024-06-17 10:45:31 +00:00
|
|
|
require.NoError(t, s.Set(sil2))
|
|
|
|
require.NotEqual(t, "", sil2.Id)
|
2016-08-15 14:10:35 +00:00
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
want = state{
|
2024-06-17 10:45:31 +00:00
|
|
|
sil1.Id: want[sil1.Id],
|
|
|
|
sil2.Id: &pb.MeshSilence{
|
2018-02-07 15:36:47 +00:00
|
|
|
Silence: &pb.Silence{
|
2024-06-17 10:45:31 +00:00
|
|
|
Id: sil2.Id,
|
2018-02-07 15:36:47 +00:00
|
|
|
Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}},
|
2022-03-28 04:46:58 +00:00
|
|
|
StartsAt: start2,
|
|
|
|
EndsAt: start2.Add(1 * time.Minute),
|
|
|
|
UpdatedAt: start2,
|
2018-02-07 15:36:47 +00:00
|
|
|
},
|
2022-03-28 04:46:58 +00:00
|
|
|
ExpiresAt: start2.Add(1*time.Minute + s.retention),
|
2017-05-16 14:48:25 +00:00
|
|
|
},
|
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
require.Equal(t, want, s.st, "unexpected state after silence creation")
|
2017-05-16 14:48:25 +00:00
|
|
|
|
2024-06-24 14:32:16 +00:00
|
|
|
// Should be able to update silence without modifications. It is expected to
|
|
|
|
// keep the same ID.
|
2017-05-16 14:48:25 +00:00
|
|
|
sil3 := cloneSilence(sil2)
|
2024-06-17 10:45:31 +00:00
|
|
|
require.NoError(t, s.Set(sil3))
|
|
|
|
require.Equal(t, sil2.Id, sil3.Id)
|
2017-05-16 14:48:25 +00:00
|
|
|
|
2024-06-24 14:32:16 +00:00
|
|
|
// Should be able to update silence with comment. It is also expected to
|
|
|
|
// keep the same ID.
|
|
|
|
sil4 := cloneSilence(sil3)
|
|
|
|
sil4.Comment = "c"
|
|
|
|
require.NoError(t, s.Set(sil4))
|
|
|
|
require.Equal(t, sil3.Id, sil4.Id)
|
|
|
|
|
|
|
|
// Extend sil4 to expire at a later time. This should not expire the
|
|
|
|
// existing silence, and so should also keep the same ID.
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(time.Minute)
|
2024-06-24 14:32:16 +00:00
|
|
|
start5 := s.nowUTC()
|
|
|
|
sil5 := cloneSilence(sil4)
|
|
|
|
sil5.EndsAt = start5.Add(100 * time.Minute)
|
|
|
|
require.NoError(t, s.Set(sil5))
|
|
|
|
require.Equal(t, sil4.Id, sil5.Id)
|
2018-02-07 15:36:47 +00:00
|
|
|
want = state{
|
2024-06-17 10:45:31 +00:00
|
|
|
sil1.Id: want[sil1.Id],
|
|
|
|
sil2.Id: &pb.MeshSilence{
|
2018-02-07 15:36:47 +00:00
|
|
|
Silence: &pb.Silence{
|
2024-06-17 10:45:31 +00:00
|
|
|
Id: sil2.Id,
|
2018-02-07 15:36:47 +00:00
|
|
|
Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}},
|
2022-03-28 04:46:58 +00:00
|
|
|
StartsAt: start2,
|
2024-06-24 14:32:16 +00:00
|
|
|
EndsAt: start5.Add(100 * time.Minute),
|
|
|
|
UpdatedAt: start5,
|
|
|
|
Comment: "c",
|
2018-02-07 15:36:47 +00:00
|
|
|
},
|
2024-06-24 14:32:16 +00:00
|
|
|
ExpiresAt: start5.Add(100*time.Minute + s.retention),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-05-16 14:48:25 +00:00
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
require.Equal(t, want, s.st, "unexpected state after silence creation")
|
2022-03-28 04:46:58 +00:00
|
|
|
|
2024-06-24 14:32:16 +00:00
|
|
|
// Replace the silence sil5 with another silence with different matchers.
|
|
|
|
// Unlike previous updates, changing the matchers for an existing silence
|
|
|
|
// will expire the existing silence and create a new silence. The new
|
|
|
|
// silence is expected to have a different ID to preserve the history of
|
|
|
|
// the previous silence.
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(time.Minute)
|
2024-06-24 14:32:16 +00:00
|
|
|
start6 := s.nowUTC()
|
2017-05-16 14:48:25 +00:00
|
|
|
|
2024-06-24 14:32:16 +00:00
|
|
|
sil6 := cloneSilence(sil5)
|
|
|
|
sil6.Matchers = []*pb.Matcher{{Name: "a", Pattern: "c"}}
|
|
|
|
require.NoError(t, s.Set(sil6))
|
|
|
|
require.NotEqual(t, sil5.Id, sil6.Id)
|
2018-02-07 15:36:47 +00:00
|
|
|
want = state{
|
2024-06-17 10:45:31 +00:00
|
|
|
sil1.Id: want[sil1.Id],
|
|
|
|
sil2.Id: &pb.MeshSilence{
|
2018-02-07 15:36:47 +00:00
|
|
|
Silence: &pb.Silence{
|
2024-06-17 10:45:31 +00:00
|
|
|
Id: sil2.Id,
|
2018-02-07 15:36:47 +00:00
|
|
|
Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}},
|
2022-03-28 04:46:58 +00:00
|
|
|
StartsAt: start2,
|
2024-06-24 14:32:16 +00:00
|
|
|
EndsAt: start6, // Expired
|
|
|
|
UpdatedAt: start6,
|
|
|
|
Comment: "c",
|
2018-02-07 15:36:47 +00:00
|
|
|
},
|
2024-06-24 14:32:16 +00:00
|
|
|
ExpiresAt: start6.Add(s.retention),
|
2018-02-07 15:36:47 +00:00
|
|
|
},
|
2024-06-24 14:32:16 +00:00
|
|
|
sil6.Id: &pb.MeshSilence{
|
2018-02-07 15:36:47 +00:00
|
|
|
Silence: &pb.Silence{
|
2024-06-24 14:32:16 +00:00
|
|
|
Id: sil6.Id,
|
2018-02-07 15:36:47 +00:00
|
|
|
Matchers: []*pb.Matcher{{Name: "a", Pattern: "c"}},
|
2024-06-24 14:32:16 +00:00
|
|
|
StartsAt: start6,
|
|
|
|
EndsAt: start5.Add(100 * time.Minute),
|
|
|
|
UpdatedAt: start6,
|
|
|
|
Comment: "c",
|
2018-02-07 15:36:47 +00:00
|
|
|
},
|
2024-06-24 14:32:16 +00:00
|
|
|
ExpiresAt: start5.Add(100*time.Minute + s.retention),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
require.Equal(t, want, s.st, "unexpected state after silence creation")
|
2016-08-15 14:10:35 +00:00
|
|
|
|
2024-06-24 14:32:16 +00:00
|
|
|
// Re-create the silence that we just replaced. Changing the start time,
|
|
|
|
// just like changing the matchers, creates a new silence with a different
|
|
|
|
// ID. This is again to preserve the history of the original silence.
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(time.Minute)
|
2024-06-24 14:32:16 +00:00
|
|
|
start7 := s.nowUTC()
|
|
|
|
sil7 := cloneSilence(sil5)
|
|
|
|
sil7.StartsAt = start1
|
|
|
|
sil7.EndsAt = start1.Add(5 * time.Minute)
|
|
|
|
require.NoError(t, s.Set(sil7))
|
|
|
|
require.NotEqual(t, sil2.Id, sil7.Id)
|
2018-02-07 15:36:47 +00:00
|
|
|
want = state{
|
2024-06-17 10:45:31 +00:00
|
|
|
sil1.Id: want[sil1.Id],
|
|
|
|
sil2.Id: want[sil2.Id],
|
2024-06-24 14:32:16 +00:00
|
|
|
sil6.Id: want[sil6.Id],
|
|
|
|
sil7.Id: &pb.MeshSilence{
|
2018-02-07 15:36:47 +00:00
|
|
|
Silence: &pb.Silence{
|
2024-06-24 14:32:16 +00:00
|
|
|
Id: sil7.Id,
|
2018-02-07 15:36:47 +00:00
|
|
|
Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}},
|
2024-06-24 14:32:16 +00:00
|
|
|
StartsAt: start7, // New silences have their start time set to "now" when created.
|
2022-03-28 04:46:58 +00:00
|
|
|
EndsAt: start1.Add(5 * time.Minute),
|
2024-06-24 14:32:16 +00:00
|
|
|
UpdatedAt: start7,
|
|
|
|
Comment: "c",
|
2018-02-07 15:36:47 +00:00
|
|
|
},
|
2022-03-28 04:46:58 +00:00
|
|
|
ExpiresAt: start1.Add(5*time.Minute + s.retention),
|
2017-05-16 14:48:25 +00:00
|
|
|
},
|
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
require.Equal(t, want, s.st, "unexpected state after silence creation")
|
2024-06-25 11:38:33 +00:00
|
|
|
|
|
|
|
// Updating an existing silence with an invalid silence should not expire
|
|
|
|
// the original silence.
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(time.Millisecond)
|
2024-06-25 11:38:33 +00:00
|
|
|
sil8 := cloneSilence(sil7)
|
|
|
|
sil8.EndsAt = time.Time{}
|
|
|
|
require.EqualError(t, s.Set(sil8), "invalid silence: invalid zero end timestamp")
|
|
|
|
|
|
|
|
// sil7 should not be expired because the update failed.
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(time.Millisecond)
|
2024-06-25 11:38:33 +00:00
|
|
|
sil7, err = s.QueryOne(QIDs(sil7.Id))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, types.SilenceStateActive, getState(sil7, s.nowUTC()))
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 16:52:44 +00:00
|
|
|
func TestSilenceLimits(t *testing.T) {
|
|
|
|
s, err := New(Options{
|
|
|
|
Limits: Limits{
|
2024-06-20 14:20:52 +00:00
|
|
|
MaxSilences: func() int { return 1 },
|
|
|
|
MaxSilenceSizeBytes: func() int { return 2 << 11 }, // 4KB
|
2024-05-31 16:52:44 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Insert sil1 should succeed without error.
|
|
|
|
sil1 := &pb.Silence{
|
|
|
|
Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}},
|
|
|
|
StartsAt: time.Now(),
|
|
|
|
EndsAt: time.Now().Add(5 * time.Minute),
|
|
|
|
}
|
2024-06-17 10:45:31 +00:00
|
|
|
require.NoError(t, s.Set(sil1))
|
2024-05-31 16:52:44 +00:00
|
|
|
|
2024-06-24 14:11:10 +00:00
|
|
|
// Insert sil2 should fail because maximum number of silences has been
|
|
|
|
// exceeded.
|
2024-05-31 16:52:44 +00:00
|
|
|
sil2 := &pb.Silence{
|
2024-06-24 14:11:10 +00:00
|
|
|
Matchers: []*pb.Matcher{{Name: "c", Pattern: "d"}},
|
2024-05-31 16:52:44 +00:00
|
|
|
StartsAt: time.Now(),
|
|
|
|
EndsAt: time.Now().Add(5 * time.Minute),
|
|
|
|
}
|
2024-06-17 10:45:31 +00:00
|
|
|
require.EqualError(t, s.Set(sil2), "exceeded maximum number of silences: 1 (limit: 1)")
|
2024-05-31 16:52:44 +00:00
|
|
|
|
2024-06-24 14:11:10 +00:00
|
|
|
// Expire sil1 and run the GC. This should allow sil2 to be inserted.
|
2024-06-17 10:45:31 +00:00
|
|
|
require.NoError(t, s.Expire(sil1.Id))
|
2024-06-05 14:03:00 +00:00
|
|
|
n, err := s.GC()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, n)
|
2024-06-17 10:45:31 +00:00
|
|
|
require.NoError(t, s.Set(sil2))
|
2024-05-31 16:52:44 +00:00
|
|
|
|
2024-06-24 14:11:10 +00:00
|
|
|
// Expire sil2 and run the GC.
|
2024-06-17 10:45:31 +00:00
|
|
|
require.NoError(t, s.Expire(sil2.Id))
|
2024-06-05 14:03:00 +00:00
|
|
|
n, err = s.GC()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, n)
|
2024-05-31 16:52:44 +00:00
|
|
|
|
|
|
|
// Insert sil3 should fail because it exceeds maximum size.
|
|
|
|
sil3 := &pb.Silence{
|
|
|
|
Matchers: []*pb.Matcher{
|
|
|
|
{
|
2024-06-24 14:11:10 +00:00
|
|
|
Name: strings.Repeat("e", 2<<9),
|
|
|
|
Pattern: strings.Repeat("f", 2<<9),
|
2024-05-31 16:52:44 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-24 14:11:10 +00:00
|
|
|
Name: strings.Repeat("g", 2<<9),
|
|
|
|
Pattern: strings.Repeat("h", 2<<9),
|
2024-05-31 16:52:44 +00:00
|
|
|
},
|
|
|
|
},
|
2024-06-24 14:11:10 +00:00
|
|
|
CreatedBy: strings.Repeat("i", 2<<9),
|
|
|
|
Comment: strings.Repeat("j", 2<<9),
|
2024-05-31 16:52:44 +00:00
|
|
|
StartsAt: time.Now(),
|
|
|
|
EndsAt: time.Now().Add(5 * time.Minute),
|
|
|
|
}
|
2024-06-24 14:11:10 +00:00
|
|
|
require.EqualError(t, s.Set(sil3), fmt.Sprintf("silence exceeded maximum size: %d bytes (limit: 4096 bytes)", s.toMeshSilence(sil3).Size()))
|
|
|
|
|
|
|
|
// Should be able to insert sil4.
|
|
|
|
sil4 := &pb.Silence{
|
|
|
|
Matchers: []*pb.Matcher{{Name: "k", Pattern: "l"}},
|
|
|
|
StartsAt: time.Now(),
|
|
|
|
EndsAt: time.Now().Add(5 * time.Minute),
|
|
|
|
}
|
|
|
|
require.NoError(t, s.Set(sil4))
|
|
|
|
|
|
|
|
// Should be able to update sil4 without modifications. It is expected to
|
|
|
|
// keep the same ID.
|
|
|
|
sil5 := cloneSilence(sil4)
|
|
|
|
require.NoError(t, s.Set(sil5))
|
|
|
|
require.Equal(t, sil4.Id, sil5.Id)
|
|
|
|
|
|
|
|
// Should be able to update the comment. It is also expected to keep the
|
|
|
|
// same ID.
|
|
|
|
sil6 := cloneSilence(sil5)
|
|
|
|
sil6.Comment = "m"
|
|
|
|
require.NoError(t, s.Set(sil6))
|
|
|
|
require.Equal(t, sil5.Id, sil6.Id)
|
|
|
|
|
|
|
|
// Should not be able to update the start and end time as this requires
|
|
|
|
// sil6 to be expired and a new silence to be created. However, this would
|
|
|
|
// exceed the maximum number of silences, which counts both active and
|
|
|
|
// expired silences.
|
|
|
|
sil7 := cloneSilence(sil6)
|
|
|
|
sil7.StartsAt = time.Now().Add(5 * time.Minute)
|
|
|
|
sil7.EndsAt = time.Now().Add(10 * time.Minute)
|
|
|
|
require.EqualError(t, s.Set(sil7), "exceeded maximum number of silences: 1 (limit: 1)")
|
|
|
|
|
|
|
|
// sil6 should not be expired because the update failed.
|
|
|
|
sil6, err = s.QueryOne(QIDs(sil6.Id))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, types.SilenceStateActive, getState(sil6, s.nowUTC()))
|
2024-06-25 12:23:09 +00:00
|
|
|
|
|
|
|
// Should not be able to update with a comment that exceeds maximum size.
|
|
|
|
// Need to increase the maximum number of silences to test this.
|
|
|
|
s.limits.MaxSilences = func() int { return 2 }
|
|
|
|
sil8 := cloneSilence(sil6)
|
|
|
|
sil8.Comment = strings.Repeat("m", 2<<11)
|
|
|
|
require.EqualError(t, s.Set(sil8), fmt.Sprintf("silence exceeded maximum size: %d bytes (limit: 4096 bytes)", s.toMeshSilence(sil8).Size()))
|
|
|
|
|
|
|
|
// sil6 should not be expired because the update failed.
|
|
|
|
sil6, err = s.QueryOne(QIDs(sil6.Id))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, types.SilenceStateActive, getState(sil6, s.nowUTC()))
|
|
|
|
|
|
|
|
// Should not be able to replace with a silence that exceeds maximum size.
|
|
|
|
// This is different from the previous assertion as unlike when adding or
|
|
|
|
// updating a comment, changing the matchers for a silence should expire
|
|
|
|
// the existing silence, unless the silence that is replacing it exceeds
|
|
|
|
// limits, in which case the operation should fail and the existing silence
|
|
|
|
// should still be active.
|
|
|
|
sil9 := cloneSilence(sil8)
|
|
|
|
sil9.Matchers = []*pb.Matcher{{Name: "n", Pattern: "o"}}
|
|
|
|
require.EqualError(t, s.Set(sil9), fmt.Sprintf("silence exceeded maximum size: %d bytes (limit: 4096 bytes)", s.toMeshSilence(sil9).Size()))
|
|
|
|
|
|
|
|
// sil6 should not be expired because the update failed.
|
|
|
|
sil6, err = s.QueryOne(QIDs(sil6.Id))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, types.SilenceStateActive, getState(sil6, s.nowUTC()))
|
2024-05-31 16:52:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-20 13:50:53 +00:00
|
|
|
func TestSilenceNoLimits(t *testing.T) {
|
|
|
|
s, err := New(Options{
|
|
|
|
Limits: Limits{},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Insert sil should succeed without error.
|
|
|
|
sil := &pb.Silence{
|
|
|
|
Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}},
|
|
|
|
StartsAt: time.Now(),
|
|
|
|
EndsAt: time.Now().Add(5 * time.Minute),
|
|
|
|
Comment: strings.Repeat("c", 2<<9),
|
|
|
|
}
|
2024-06-17 10:45:31 +00:00
|
|
|
require.NoError(t, s.Set(sil))
|
|
|
|
require.NotEqual(t, "", sil.Id)
|
2024-06-20 13:50:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 11:48:31 +00:00
|
|
|
func TestSetActiveSilence(t *testing.T) {
|
|
|
|
s, err := New(Options{
|
|
|
|
Retention: time.Hour,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-08-27 10:21:50 +00:00
|
|
|
clock := quartz.NewMock(t)
|
2022-03-28 04:46:58 +00:00
|
|
|
s.clock = clock
|
|
|
|
now := clock.Now()
|
2022-01-12 11:48:31 +00:00
|
|
|
|
|
|
|
startsAt := now.Add(-1 * time.Minute)
|
|
|
|
endsAt := now.Add(5 * time.Minute)
|
|
|
|
// Insert silence with fixed start time.
|
|
|
|
sil1 := &pb.Silence{
|
|
|
|
Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}},
|
|
|
|
StartsAt: startsAt,
|
|
|
|
EndsAt: endsAt,
|
|
|
|
}
|
2024-06-17 10:45:31 +00:00
|
|
|
require.NoError(t, s.Set(sil1))
|
2022-01-12 11:48:31 +00:00
|
|
|
|
|
|
|
// Update silence with 2 extra nanoseconds so the "seconds" part should not change
|
|
|
|
|
|
|
|
newStartsAt := now.Add(2 * time.Nanosecond)
|
|
|
|
newEndsAt := endsAt.Add(2 * time.Minute)
|
|
|
|
|
|
|
|
sil2 := cloneSilence(sil1)
|
2024-06-17 10:45:31 +00:00
|
|
|
sil2.Id = sil1.Id
|
2022-01-12 11:48:31 +00:00
|
|
|
sil2.StartsAt = newStartsAt
|
|
|
|
sil2.EndsAt = newEndsAt
|
|
|
|
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(time.Minute)
|
2022-03-28 04:46:58 +00:00
|
|
|
now = s.nowUTC()
|
2024-06-17 10:45:31 +00:00
|
|
|
require.NoError(t, s.Set(sil2))
|
|
|
|
require.Equal(t, sil1.Id, sil2.Id)
|
2022-01-12 11:48:31 +00:00
|
|
|
|
|
|
|
want := state{
|
2024-06-17 10:45:31 +00:00
|
|
|
sil2.Id: &pb.MeshSilence{
|
2022-01-12 11:48:31 +00:00
|
|
|
Silence: &pb.Silence{
|
2024-06-17 10:45:31 +00:00
|
|
|
Id: sil1.Id,
|
2022-01-12 11:48:31 +00:00
|
|
|
Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}},
|
|
|
|
StartsAt: newStartsAt,
|
|
|
|
EndsAt: newEndsAt,
|
|
|
|
UpdatedAt: now,
|
|
|
|
},
|
|
|
|
ExpiresAt: newEndsAt.Add(s.retention),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Equal(t, want, s.st, "unexpected state after silence creation")
|
|
|
|
}
|
|
|
|
|
2017-05-16 14:48:25 +00:00
|
|
|
func TestSilencesSetFail(t *testing.T) {
|
2016-08-15 14:10:35 +00:00
|
|
|
s, err := New(Options{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-08-27 10:21:50 +00:00
|
|
|
clock := quartz.NewMock(t)
|
2022-03-28 04:46:58 +00:00
|
|
|
s.clock = clock
|
2016-08-15 14:10:35 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
s *pb.Silence
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{
|
2024-06-25 11:38:33 +00:00
|
|
|
s: &pb.Silence{
|
|
|
|
Id: "some_id",
|
|
|
|
Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}},
|
|
|
|
EndsAt: clock.Now().Add(5 * time.Minute),
|
|
|
|
},
|
2017-05-16 14:48:25 +00:00
|
|
|
err: ErrNotFound.Error(),
|
2016-08-15 14:10:35 +00:00
|
|
|
}, {
|
|
|
|
s: &pb.Silence{}, // Silence without matcher.
|
2024-06-25 11:38:33 +00:00
|
|
|
err: "invalid silence",
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
2024-06-17 10:45:31 +00:00
|
|
|
checkErr(t, c.err, s.Set(c.s))
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQState(t *testing.T) {
|
2022-03-28 04:46:58 +00:00
|
|
|
now := time.Now().UTC()
|
2016-08-15 14:10:35 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
sil *pb.Silence
|
2017-11-07 10:36:30 +00:00
|
|
|
states []types.SilenceState
|
2016-08-30 09:58:27 +00:00
|
|
|
keep bool
|
2016-08-15 14:10:35 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
sil: &pb.Silence{
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: now.Add(time.Minute),
|
|
|
|
EndsAt: now.Add(time.Hour),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-11-07 10:36:30 +00:00
|
|
|
states: []types.SilenceState{types.SilenceStateActive, types.SilenceStateExpired},
|
2016-08-30 09:58:27 +00:00
|
|
|
keep: false,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
sil: &pb.Silence{
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: now.Add(time.Minute),
|
|
|
|
EndsAt: now.Add(time.Hour),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-11-07 10:36:30 +00:00
|
|
|
states: []types.SilenceState{types.SilenceStatePending},
|
2016-08-30 09:58:27 +00:00
|
|
|
keep: true,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
sil: &pb.Silence{
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: now.Add(time.Minute),
|
|
|
|
EndsAt: now.Add(time.Hour),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-11-07 10:36:30 +00:00
|
|
|
states: []types.SilenceState{types.SilenceStateExpired, types.SilenceStatePending},
|
2016-08-30 09:58:27 +00:00
|
|
|
keep: true,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, c := range cases {
|
|
|
|
q := &query{}
|
|
|
|
QState(c.states...)(q)
|
|
|
|
f := q.filters[0]
|
|
|
|
|
2017-04-18 08:46:40 +00:00
|
|
|
keep, err := f(c.sil, nil, now)
|
2016-08-15 14:10:35 +00:00
|
|
|
require.NoError(t, err)
|
2016-08-30 09:58:27 +00:00
|
|
|
require.Equal(t, c.keep, keep, "unexpected filter result for case %d", i)
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQMatches(t *testing.T) {
|
2016-09-08 14:02:27 +00:00
|
|
|
qp := QMatches(model.LabelSet{
|
2016-08-15 14:10:35 +00:00
|
|
|
"job": "test",
|
|
|
|
"instance": "web-1",
|
|
|
|
"path": "/user/profile",
|
|
|
|
"method": "GET",
|
|
|
|
})
|
|
|
|
|
|
|
|
q := &query{}
|
|
|
|
qp(q)
|
|
|
|
f := q.filters[0]
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
sil *pb.Silence
|
|
|
|
drop bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
sil: &pb.Silence{
|
|
|
|
Matchers: []*pb.Matcher{
|
|
|
|
{Name: "job", Pattern: "test", Type: pb.Matcher_EQUAL},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
drop: true,
|
|
|
|
},
|
2021-01-18 20:58:20 +00:00
|
|
|
{
|
|
|
|
sil: &pb.Silence{
|
|
|
|
Matchers: []*pb.Matcher{
|
|
|
|
{Name: "job", Pattern: "test", Type: pb.Matcher_NOT_EQUAL},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
drop: false,
|
|
|
|
},
|
2016-08-15 14:10:35 +00:00
|
|
|
{
|
|
|
|
sil: &pb.Silence{
|
|
|
|
Matchers: []*pb.Matcher{
|
|
|
|
{Name: "job", Pattern: "test", Type: pb.Matcher_EQUAL},
|
|
|
|
{Name: "method", Pattern: "POST", Type: pb.Matcher_EQUAL},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
drop: false,
|
|
|
|
},
|
2021-01-18 20:58:20 +00:00
|
|
|
{
|
|
|
|
sil: &pb.Silence{
|
|
|
|
Matchers: []*pb.Matcher{
|
|
|
|
{Name: "job", Pattern: "test", Type: pb.Matcher_EQUAL},
|
|
|
|
{Name: "method", Pattern: "POST", Type: pb.Matcher_NOT_EQUAL},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
drop: true,
|
|
|
|
},
|
2016-08-15 14:10:35 +00:00
|
|
|
{
|
|
|
|
sil: &pb.Silence{
|
|
|
|
Matchers: []*pb.Matcher{
|
|
|
|
{Name: "path", Pattern: "/user/.+", Type: pb.Matcher_REGEXP},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
drop: true,
|
|
|
|
},
|
2021-01-18 20:58:20 +00:00
|
|
|
{
|
|
|
|
sil: &pb.Silence{
|
|
|
|
Matchers: []*pb.Matcher{
|
|
|
|
{Name: "path", Pattern: "/user/.+", Type: pb.Matcher_NOT_REGEXP},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
drop: false,
|
|
|
|
},
|
2016-08-15 14:10:35 +00:00
|
|
|
{
|
|
|
|
sil: &pb.Silence{
|
|
|
|
Matchers: []*pb.Matcher{
|
|
|
|
{Name: "path", Pattern: "/user/.+", Type: pb.Matcher_REGEXP},
|
|
|
|
{Name: "path", Pattern: "/nothing/.+", Type: pb.Matcher_REGEXP},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
drop: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
2018-02-07 15:36:47 +00:00
|
|
|
drop, err := f(c.sil, &Silences{mc: matcherCache{}, st: state{}}, time.Time{})
|
2016-08-15 14:10:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, c.drop, drop, "unexpected filter result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSilencesQuery(t *testing.T) {
|
|
|
|
s, err := New(Options{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
s.st = state{
|
|
|
|
"1": &pb.MeshSilence{Silence: &pb.Silence{Id: "1"}},
|
|
|
|
"2": &pb.MeshSilence{Silence: &pb.Silence{Id: "2"}},
|
|
|
|
"3": &pb.MeshSilence{Silence: &pb.Silence{Id: "3"}},
|
|
|
|
"4": &pb.MeshSilence{Silence: &pb.Silence{Id: "4"}},
|
|
|
|
"5": &pb.MeshSilence{Silence: &pb.Silence{Id: "5"}},
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
cases := []struct {
|
|
|
|
q *query
|
|
|
|
exp []*pb.Silence
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// Default query of retrieving all silences.
|
|
|
|
q: &query{},
|
|
|
|
exp: []*pb.Silence{
|
|
|
|
{Id: "1"},
|
|
|
|
{Id: "2"},
|
|
|
|
{Id: "3"},
|
|
|
|
{Id: "4"},
|
|
|
|
{Id: "5"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Retrieve by IDs.
|
|
|
|
q: &query{
|
|
|
|
ids: []string{"2", "5"},
|
|
|
|
},
|
|
|
|
exp: []*pb.Silence{
|
|
|
|
{Id: "2"},
|
|
|
|
{Id: "5"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Retrieve all and filter
|
|
|
|
q: &query{
|
|
|
|
filters: []silenceFilter{
|
2017-04-18 08:46:40 +00:00
|
|
|
func(sil *pb.Silence, _ *Silences, _ time.Time) (bool, error) {
|
2016-08-15 14:10:35 +00:00
|
|
|
return sil.Id == "1" || sil.Id == "2", nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
exp: []*pb.Silence{
|
|
|
|
{Id: "1"},
|
|
|
|
{Id: "2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Retrieve by IDs and filter
|
|
|
|
q: &query{
|
|
|
|
ids: []string{"2", "5"},
|
|
|
|
filters: []silenceFilter{
|
2017-04-18 08:46:40 +00:00
|
|
|
func(sil *pb.Silence, _ *Silences, _ time.Time) (bool, error) {
|
2016-08-15 14:10:35 +00:00
|
|
|
return sil.Id == "1" || sil.Id == "2", nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
exp: []*pb.Silence{
|
|
|
|
{Id: "2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
// Run default query of retrieving all silences.
|
2019-02-27 11:33:46 +00:00
|
|
|
res, _, err := s.query(c.q, time.Time{})
|
2016-08-15 14:10:35 +00:00
|
|
|
require.NoError(t, err, "unexpected error on querying")
|
|
|
|
|
|
|
|
// Currently there are no sorting guarantees in the querying API.
|
|
|
|
sort.Sort(silencesByID(c.exp))
|
|
|
|
sort.Sort(silencesByID(res))
|
|
|
|
require.Equal(t, c.exp, res, "unexpected silences in result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type silencesByID []*pb.Silence
|
|
|
|
|
|
|
|
func (s silencesByID) Len() int { return len(s) }
|
|
|
|
func (s silencesByID) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
func (s silencesByID) Less(i, j int) bool { return s[i].Id < s[j].Id }
|
|
|
|
|
2017-05-16 14:48:25 +00:00
|
|
|
func TestSilenceCanUpdate(t *testing.T) {
|
2022-03-28 04:46:58 +00:00
|
|
|
now := time.Now().UTC()
|
2016-08-15 14:10:35 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
2017-05-16 14:48:25 +00:00
|
|
|
a, b *pb.Silence
|
|
|
|
ok bool
|
2016-08-15 14:10:35 +00:00
|
|
|
}{
|
|
|
|
// Bad arguments.
|
|
|
|
{
|
2017-05-16 14:48:25 +00:00
|
|
|
a: &pb.Silence{},
|
|
|
|
b: &pb.Silence{
|
|
|
|
StartsAt: now,
|
|
|
|
EndsAt: now.Add(-time.Minute),
|
|
|
|
},
|
|
|
|
ok: false,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
// Expired silence.
|
|
|
|
{
|
2017-05-16 14:48:25 +00:00
|
|
|
a: &pb.Silence{
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: now.Add(-time.Hour),
|
|
|
|
EndsAt: now.Add(-time.Second),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-05-16 14:48:25 +00:00
|
|
|
b: &pb.Silence{
|
|
|
|
StartsAt: now,
|
|
|
|
EndsAt: now,
|
|
|
|
},
|
|
|
|
ok: false,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
// Pending silences.
|
|
|
|
{
|
2017-05-16 14:48:25 +00:00
|
|
|
a: &pb.Silence{
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: now.Add(time.Hour),
|
|
|
|
EndsAt: now.Add(2 * time.Hour),
|
|
|
|
UpdatedAt: now.Add(-time.Hour),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-05-16 14:48:25 +00:00
|
|
|
b: &pb.Silence{
|
|
|
|
StartsAt: now.Add(-time.Minute),
|
|
|
|
EndsAt: now.Add(time.Hour),
|
|
|
|
},
|
|
|
|
ok: false,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-16 14:48:25 +00:00
|
|
|
a: &pb.Silence{
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: now.Add(time.Hour),
|
|
|
|
EndsAt: now.Add(2 * time.Hour),
|
|
|
|
UpdatedAt: now.Add(-time.Hour),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-05-16 14:48:25 +00:00
|
|
|
b: &pb.Silence{
|
|
|
|
StartsAt: now.Add(time.Minute),
|
|
|
|
EndsAt: now.Add(time.Minute),
|
|
|
|
},
|
|
|
|
ok: true,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-16 14:48:25 +00:00
|
|
|
a: &pb.Silence{
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: now.Add(time.Hour),
|
|
|
|
EndsAt: now.Add(2 * time.Hour),
|
|
|
|
UpdatedAt: now.Add(-time.Hour),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-05-16 14:48:25 +00:00
|
|
|
b: &pb.Silence{
|
|
|
|
StartsAt: now, // set to exactly start now.
|
|
|
|
EndsAt: now.Add(2 * time.Hour),
|
|
|
|
},
|
|
|
|
ok: true,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
// Active silences.
|
|
|
|
{
|
2017-05-16 14:48:25 +00:00
|
|
|
a: &pb.Silence{
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: now.Add(-time.Hour),
|
|
|
|
EndsAt: now.Add(2 * time.Hour),
|
|
|
|
UpdatedAt: now.Add(-time.Hour),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-05-16 14:48:25 +00:00
|
|
|
b: &pb.Silence{
|
|
|
|
StartsAt: now.Add(-time.Minute),
|
|
|
|
EndsAt: now.Add(2 * time.Hour),
|
|
|
|
},
|
|
|
|
ok: false,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-16 14:48:25 +00:00
|
|
|
a: &pb.Silence{
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: now.Add(-time.Hour),
|
|
|
|
EndsAt: now.Add(2 * time.Hour),
|
|
|
|
UpdatedAt: now.Add(-time.Hour),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-05-16 14:48:25 +00:00
|
|
|
b: &pb.Silence{
|
|
|
|
StartsAt: now.Add(-time.Hour),
|
|
|
|
EndsAt: now.Add(-time.Second),
|
|
|
|
},
|
|
|
|
ok: false,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-16 14:48:25 +00:00
|
|
|
a: &pb.Silence{
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: now.Add(-time.Hour),
|
|
|
|
EndsAt: now.Add(2 * time.Hour),
|
|
|
|
UpdatedAt: now.Add(-time.Hour),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-05-16 14:48:25 +00:00
|
|
|
b: &pb.Silence{
|
|
|
|
StartsAt: now.Add(-time.Hour),
|
|
|
|
EndsAt: now,
|
|
|
|
},
|
|
|
|
ok: true,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-16 14:48:25 +00:00
|
|
|
a: &pb.Silence{
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: now.Add(-time.Hour),
|
|
|
|
EndsAt: now.Add(2 * time.Hour),
|
|
|
|
UpdatedAt: now.Add(-time.Hour),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-05-16 14:48:25 +00:00
|
|
|
b: &pb.Silence{
|
|
|
|
StartsAt: now.Add(-time.Hour),
|
|
|
|
EndsAt: now.Add(3 * time.Hour),
|
|
|
|
},
|
|
|
|
ok: true,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
2017-05-16 14:48:25 +00:00
|
|
|
ok := canUpdate(c.a, c.b, now)
|
|
|
|
if ok && !c.ok {
|
|
|
|
t.Errorf("expected not-updateable but was: %v, %v", c.a, c.b)
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2017-05-16 14:48:25 +00:00
|
|
|
if ok && !c.ok {
|
|
|
|
t.Errorf("expected updateable but was not: %v, %v", c.a, c.b)
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 07:27:57 +00:00
|
|
|
func TestSilenceExpire(t *testing.T) {
|
2019-02-27 17:00:09 +00:00
|
|
|
s, err := New(Options{Retention: time.Hour})
|
2017-05-22 07:27:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-08-27 10:21:50 +00:00
|
|
|
clock := quartz.NewMock(t)
|
2022-03-28 04:46:58 +00:00
|
|
|
s.clock = clock
|
|
|
|
now := s.nowUTC()
|
2017-05-22 07:27:57 +00:00
|
|
|
|
|
|
|
m := &pb.Matcher{Type: pb.Matcher_EQUAL, Name: "a", Pattern: "b"}
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
s.st = state{
|
|
|
|
"pending": &pb.MeshSilence{Silence: &pb.Silence{
|
|
|
|
Id: "pending",
|
|
|
|
Matchers: []*pb.Matcher{m},
|
|
|
|
StartsAt: now.Add(time.Minute),
|
|
|
|
EndsAt: now.Add(time.Hour),
|
|
|
|
UpdatedAt: now.Add(-time.Hour),
|
|
|
|
}},
|
|
|
|
"active": &pb.MeshSilence{Silence: &pb.Silence{
|
|
|
|
Id: "active",
|
|
|
|
Matchers: []*pb.Matcher{m},
|
|
|
|
StartsAt: now.Add(-time.Minute),
|
|
|
|
EndsAt: now.Add(time.Hour),
|
|
|
|
UpdatedAt: now.Add(-time.Hour),
|
|
|
|
}},
|
|
|
|
"expired": &pb.MeshSilence{Silence: &pb.Silence{
|
|
|
|
Id: "expired",
|
|
|
|
Matchers: []*pb.Matcher{m},
|
|
|
|
StartsAt: now.Add(-time.Hour),
|
|
|
|
EndsAt: now.Add(-time.Minute),
|
|
|
|
UpdatedAt: now.Add(-time.Hour),
|
|
|
|
}},
|
2017-05-22 07:27:57 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 10:36:30 +00:00
|
|
|
count, err := s.CountState(types.SilenceStatePending)
|
2017-09-15 13:33:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, count)
|
|
|
|
|
2019-02-27 17:00:09 +00:00
|
|
|
count, err = s.CountState(types.SilenceStateExpired)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, count)
|
|
|
|
|
|
|
|
require.NoError(t, s.Expire("pending"))
|
|
|
|
require.NoError(t, s.Expire("active"))
|
2017-05-22 07:27:57 +00:00
|
|
|
|
2022-02-22 12:34:21 +00:00
|
|
|
require.NoError(t, s.Expire("expired"))
|
2017-05-22 07:27:57 +00:00
|
|
|
|
|
|
|
sil, err := s.QueryOne(QIDs("pending"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &pb.Silence{
|
|
|
|
Id: "pending",
|
|
|
|
Matchers: []*pb.Matcher{m},
|
2017-05-29 16:03:31 +00:00
|
|
|
StartsAt: now,
|
|
|
|
EndsAt: now,
|
2017-05-22 07:27:57 +00:00
|
|
|
UpdatedAt: now,
|
|
|
|
}, sil)
|
2017-09-15 13:33:47 +00:00
|
|
|
|
2019-02-27 17:00:09 +00:00
|
|
|
// Let time pass...
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(time.Second)
|
2019-02-27 17:00:09 +00:00
|
|
|
|
2017-11-07 10:36:30 +00:00
|
|
|
count, err = s.CountState(types.SilenceStatePending)
|
2017-09-15 13:33:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, count)
|
|
|
|
|
2019-02-27 17:00:09 +00:00
|
|
|
count, err = s.CountState(types.SilenceStateExpired)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 3, count)
|
|
|
|
|
2017-05-29 16:03:31 +00:00
|
|
|
// Expiring a pending Silence should make the API return the
|
|
|
|
// SilenceStateExpired Silence state.
|
|
|
|
silenceState := types.CalcSilenceState(sil.StartsAt, sil.EndsAt)
|
2023-12-10 08:33:13 +00:00
|
|
|
require.Equal(t, types.SilenceStateExpired, silenceState)
|
2017-05-22 07:27:57 +00:00
|
|
|
|
|
|
|
sil, err = s.QueryOne(QIDs("active"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &pb.Silence{
|
|
|
|
Id: "active",
|
|
|
|
Matchers: []*pb.Matcher{m},
|
|
|
|
StartsAt: now.Add(-time.Minute),
|
|
|
|
EndsAt: now,
|
|
|
|
UpdatedAt: now,
|
|
|
|
}, sil)
|
|
|
|
|
|
|
|
sil, err = s.QueryOne(QIDs("expired"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &pb.Silence{
|
|
|
|
Id: "expired",
|
|
|
|
Matchers: []*pb.Matcher{m},
|
|
|
|
StartsAt: now.Add(-time.Hour),
|
|
|
|
EndsAt: now.Add(-time.Minute),
|
|
|
|
UpdatedAt: now.Add(-time.Hour),
|
|
|
|
}, sil)
|
2019-02-27 17:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestSilenceExpireWithZeroRetention covers the problem that, with zero
|
|
|
|
// retention time, a silence explicitly set to expired will also immediately
|
|
|
|
// expire from the silence storage.
|
|
|
|
func TestSilenceExpireWithZeroRetention(t *testing.T) {
|
2022-03-28 04:46:58 +00:00
|
|
|
s, err := New(Options{Retention: 0})
|
2019-02-27 17:00:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-08-27 10:21:50 +00:00
|
|
|
clock := quartz.NewMock(t)
|
2022-03-28 04:46:58 +00:00
|
|
|
s.clock = clock
|
|
|
|
now := s.nowUTC()
|
2019-02-27 17:00:09 +00:00
|
|
|
|
|
|
|
m := &pb.Matcher{Type: pb.Matcher_EQUAL, Name: "a", Pattern: "b"}
|
|
|
|
|
|
|
|
s.st = state{
|
|
|
|
"pending": &pb.MeshSilence{Silence: &pb.Silence{
|
|
|
|
Id: "pending",
|
|
|
|
Matchers: []*pb.Matcher{m},
|
|
|
|
StartsAt: now.Add(time.Minute),
|
|
|
|
EndsAt: now.Add(time.Hour),
|
|
|
|
UpdatedAt: now.Add(-time.Hour),
|
|
|
|
}},
|
|
|
|
"active": &pb.MeshSilence{Silence: &pb.Silence{
|
|
|
|
Id: "active",
|
|
|
|
Matchers: []*pb.Matcher{m},
|
|
|
|
StartsAt: now.Add(-time.Minute),
|
|
|
|
EndsAt: now.Add(time.Hour),
|
|
|
|
UpdatedAt: now.Add(-time.Hour),
|
|
|
|
}},
|
|
|
|
"expired": &pb.MeshSilence{Silence: &pb.Silence{
|
|
|
|
Id: "expired",
|
|
|
|
Matchers: []*pb.Matcher{m},
|
|
|
|
StartsAt: now.Add(-time.Hour),
|
|
|
|
EndsAt: now.Add(-time.Minute),
|
|
|
|
UpdatedAt: now.Add(-time.Hour),
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := s.CountState(types.SilenceStatePending)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, count)
|
|
|
|
|
2022-03-28 04:46:58 +00:00
|
|
|
count, err = s.CountState(types.SilenceStateActive)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, count)
|
|
|
|
|
2019-02-27 17:00:09 +00:00
|
|
|
count, err = s.CountState(types.SilenceStateExpired)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, count)
|
|
|
|
|
2022-03-28 04:46:58 +00:00
|
|
|
// Advance time. The silence state management code uses update time when
|
|
|
|
// merging, and the logic is "first write wins". So we must advance the clock
|
|
|
|
// one tick for updates to take effect.
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(1 * time.Millisecond)
|
2022-03-28 04:46:58 +00:00
|
|
|
|
2019-02-27 17:00:09 +00:00
|
|
|
require.NoError(t, s.Expire("pending"))
|
|
|
|
require.NoError(t, s.Expire("active"))
|
2022-02-22 12:34:21 +00:00
|
|
|
require.NoError(t, s.Expire("expired"))
|
2019-02-27 17:00:09 +00:00
|
|
|
|
2022-03-28 04:46:58 +00:00
|
|
|
// Advance time again. Despite what the function name says, s.Expire() does
|
|
|
|
// not expire a silence. It sets the silence to EndAt the current time. This
|
|
|
|
// means that the silence is active immediately after calling Expire.
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(1 * time.Millisecond)
|
2022-03-28 04:46:58 +00:00
|
|
|
|
2022-05-05 03:28:24 +00:00
|
|
|
// Verify all silences have expired.
|
2019-02-27 17:00:09 +00:00
|
|
|
count, err = s.CountState(types.SilenceStatePending)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, count)
|
|
|
|
|
2022-03-28 04:46:58 +00:00
|
|
|
count, err = s.CountState(types.SilenceStateActive)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, count)
|
|
|
|
|
2019-02-27 17:00:09 +00:00
|
|
|
count, err = s.CountState(types.SilenceStateExpired)
|
|
|
|
require.NoError(t, err)
|
2019-02-27 17:00:27 +00:00
|
|
|
require.Equal(t, 3, count)
|
2017-05-22 07:27:57 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 10:01:40 +00:00
|
|
|
// This test checks that invalid silences can be expired.
|
|
|
|
func TestSilenceExpireInvalid(t *testing.T) {
|
|
|
|
s, err := New(Options{Retention: time.Hour})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-08-27 10:21:50 +00:00
|
|
|
clock := quartz.NewMock(t)
|
2023-11-24 10:01:40 +00:00
|
|
|
s.clock = clock
|
|
|
|
now := s.nowUTC()
|
|
|
|
|
|
|
|
// In this test the matcher has an invalid type.
|
|
|
|
silence := pb.Silence{
|
|
|
|
Id: "active",
|
|
|
|
Matchers: []*pb.Matcher{{Type: -1, Name: "a", Pattern: "b"}},
|
|
|
|
StartsAt: now.Add(-time.Minute),
|
|
|
|
EndsAt: now.Add(time.Hour),
|
|
|
|
UpdatedAt: now.Add(-time.Hour),
|
|
|
|
}
|
|
|
|
// Assert that this silence is invalid.
|
2024-01-15 10:03:51 +00:00
|
|
|
require.EqualError(t, validateSilence(&silence), "invalid label matcher 0: unknown matcher type \"-1\"")
|
2023-11-24 10:01:40 +00:00
|
|
|
|
|
|
|
s.st = state{"active": &pb.MeshSilence{Silence: &silence}}
|
|
|
|
|
|
|
|
// The silence should be active.
|
|
|
|
count, err := s.CountState(types.SilenceStateActive)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, count)
|
|
|
|
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(time.Millisecond)
|
2023-11-24 10:01:40 +00:00
|
|
|
require.NoError(t, s.Expire("active"))
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(time.Millisecond)
|
2023-11-24 10:01:40 +00:00
|
|
|
|
|
|
|
// The silence should be expired.
|
|
|
|
count, err = s.CountState(types.SilenceStateActive)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, count)
|
|
|
|
count, err = s.CountState(types.SilenceStateExpired)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, count)
|
|
|
|
}
|
|
|
|
|
2021-05-26 17:33:52 +00:00
|
|
|
func TestSilencer(t *testing.T) {
|
|
|
|
ss, err := New(Options{Retention: time.Hour})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-08-27 10:21:50 +00:00
|
|
|
clock := quartz.NewMock(t)
|
2022-03-28 04:46:58 +00:00
|
|
|
ss.clock = clock
|
|
|
|
now := ss.nowUTC()
|
2021-05-26 17:33:52 +00:00
|
|
|
|
|
|
|
m := types.NewMarker(prometheus.NewRegistry())
|
2021-05-20 22:13:16 +00:00
|
|
|
s := NewSilencer(ss, m, log.NewNopLogger())
|
2021-05-26 17:33:52 +00:00
|
|
|
|
|
|
|
require.False(t, s.Mutes(model.LabelSet{"foo": "bar"}), "expected alert not silenced without any silences")
|
|
|
|
|
2024-06-17 10:45:31 +00:00
|
|
|
sil1 := &pb.Silence{
|
2021-05-26 17:33:52 +00:00
|
|
|
Matchers: []*pb.Matcher{{Name: "foo", Pattern: "baz"}},
|
|
|
|
StartsAt: now.Add(-time.Hour),
|
|
|
|
EndsAt: now.Add(5 * time.Minute),
|
2024-06-17 10:45:31 +00:00
|
|
|
}
|
|
|
|
require.NoError(t, ss.Set(sil1))
|
2021-05-26 17:33:52 +00:00
|
|
|
|
|
|
|
require.False(t, s.Mutes(model.LabelSet{"foo": "bar"}), "expected alert not silenced by non-matching silence")
|
|
|
|
|
2024-06-17 10:45:31 +00:00
|
|
|
sil2 := &pb.Silence{
|
2021-05-26 17:33:52 +00:00
|
|
|
Matchers: []*pb.Matcher{{Name: "foo", Pattern: "bar"}},
|
|
|
|
StartsAt: now.Add(-time.Hour),
|
|
|
|
EndsAt: now.Add(5 * time.Minute),
|
2024-06-17 10:45:31 +00:00
|
|
|
}
|
|
|
|
require.NoError(t, ss.Set(sil2))
|
|
|
|
require.NotEmpty(t, sil2.Id)
|
2021-05-26 17:33:52 +00:00
|
|
|
|
|
|
|
require.True(t, s.Mutes(model.LabelSet{"foo": "bar"}), "expected alert silenced by matching silence")
|
|
|
|
|
2022-03-28 04:46:58 +00:00
|
|
|
// One hour passes, silence expires.
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(time.Hour)
|
2022-03-28 04:46:58 +00:00
|
|
|
now = ss.nowUTC()
|
2021-05-26 17:33:52 +00:00
|
|
|
|
|
|
|
require.False(t, s.Mutes(model.LabelSet{"foo": "bar"}), "expected alert not silenced by expired silence")
|
|
|
|
|
|
|
|
// Update silence to start in the future.
|
2024-06-17 10:45:31 +00:00
|
|
|
err = ss.Set(&pb.Silence{
|
|
|
|
Id: sil2.Id,
|
2021-05-26 17:33:52 +00:00
|
|
|
Matchers: []*pb.Matcher{{Name: "foo", Pattern: "bar"}},
|
|
|
|
StartsAt: now.Add(time.Hour),
|
|
|
|
EndsAt: now.Add(3 * time.Hour),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.False(t, s.Mutes(model.LabelSet{"foo": "bar"}), "expected alert not silenced by future silence")
|
|
|
|
|
2022-03-28 04:46:58 +00:00
|
|
|
// Two hours pass, silence becomes active.
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(2 * time.Hour)
|
2022-03-28 04:46:58 +00:00
|
|
|
now = ss.nowUTC()
|
2021-05-26 17:33:52 +00:00
|
|
|
|
|
|
|
// Exposes issue #2426.
|
|
|
|
require.True(t, s.Mutes(model.LabelSet{"foo": "bar"}), "expected alert silenced by activated silence")
|
|
|
|
|
2024-06-17 10:45:31 +00:00
|
|
|
err = ss.Set(&pb.Silence{
|
2021-05-26 17:33:52 +00:00
|
|
|
Matchers: []*pb.Matcher{{Name: "foo", Pattern: "b..", Type: pb.Matcher_REGEXP}},
|
|
|
|
StartsAt: now.Add(time.Hour),
|
|
|
|
EndsAt: now.Add(3 * time.Hour),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Note that issue #2426 doesn't apply anymore because we added a new silence.
|
|
|
|
require.True(t, s.Mutes(model.LabelSet{"foo": "bar"}), "expected alert still silenced by activated silence")
|
|
|
|
|
2022-03-28 04:46:58 +00:00
|
|
|
// Two hours pass, first silence expires, overlapping second silence becomes active.
|
2024-08-27 10:21:50 +00:00
|
|
|
clock.Advance(2 * time.Hour)
|
2021-05-26 17:33:52 +00:00
|
|
|
|
|
|
|
// Another variant of issue #2426 (overlapping silences).
|
|
|
|
require.True(t, s.Mutes(model.LabelSet{"foo": "bar"}), "expected alert silenced by activated second silence")
|
|
|
|
}
|
|
|
|
|
2023-11-24 10:01:40 +00:00
|
|
|
func TestValidateClassicMatcher(t *testing.T) {
|
2016-08-15 14:10:35 +00:00
|
|
|
cases := []struct {
|
|
|
|
m *pb.Matcher
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "b",
|
|
|
|
Type: pb.Matcher_EQUAL,
|
|
|
|
},
|
|
|
|
err: "",
|
2021-01-18 20:58:20 +00:00
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "b",
|
|
|
|
Type: pb.Matcher_NOT_EQUAL,
|
|
|
|
},
|
|
|
|
err: "",
|
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "b",
|
|
|
|
Type: pb.Matcher_REGEXP,
|
|
|
|
},
|
|
|
|
err: "",
|
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "b",
|
|
|
|
Type: pb.Matcher_NOT_REGEXP,
|
|
|
|
},
|
|
|
|
err: "",
|
2016-08-15 14:10:35 +00:00
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "00",
|
|
|
|
Pattern: "a",
|
|
|
|
Type: pb.Matcher_EQUAL,
|
|
|
|
},
|
|
|
|
err: "invalid label name",
|
2023-11-24 10:01:40 +00:00
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "\xf0\x9f\x99\x82", // U+1F642
|
|
|
|
Pattern: "a",
|
|
|
|
Type: pb.Matcher_EQUAL,
|
|
|
|
},
|
|
|
|
err: "invalid label name",
|
2016-08-15 14:10:35 +00:00
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "((",
|
|
|
|
Type: pb.Matcher_REGEXP,
|
|
|
|
},
|
|
|
|
err: "invalid regular expression",
|
2021-01-18 20:58:20 +00:00
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "))",
|
|
|
|
Type: pb.Matcher_NOT_REGEXP,
|
|
|
|
},
|
|
|
|
err: "invalid regular expression",
|
2016-08-15 14:10:35 +00:00
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "\xff",
|
|
|
|
Type: pb.Matcher_EQUAL,
|
|
|
|
},
|
|
|
|
err: "invalid label value",
|
2023-11-24 10:01:40 +00:00
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "\xf0\x9f\x99\x82", // U+1F642
|
|
|
|
Type: pb.Matcher_EQUAL,
|
|
|
|
},
|
|
|
|
err: "",
|
2016-08-15 14:10:35 +00:00
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "b",
|
|
|
|
Type: 333,
|
|
|
|
},
|
|
|
|
err: "unknown matcher type",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2024-01-15 10:03:51 +00:00
|
|
|
checkErr(t, c.err, validateMatcher(c.m))
|
2023-11-24 10:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateUTF8Matcher(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
m *pb.Matcher
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "b",
|
|
|
|
Type: pb.Matcher_EQUAL,
|
|
|
|
},
|
|
|
|
err: "",
|
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "b",
|
|
|
|
Type: pb.Matcher_NOT_EQUAL,
|
|
|
|
},
|
|
|
|
err: "",
|
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "b",
|
|
|
|
Type: pb.Matcher_REGEXP,
|
|
|
|
},
|
|
|
|
err: "",
|
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "b",
|
|
|
|
Type: pb.Matcher_NOT_REGEXP,
|
|
|
|
},
|
|
|
|
err: "",
|
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "00",
|
|
|
|
Pattern: "a",
|
|
|
|
Type: pb.Matcher_EQUAL,
|
|
|
|
},
|
|
|
|
err: "",
|
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "\xf0\x9f\x99\x82", // U+1F642
|
|
|
|
Pattern: "a",
|
|
|
|
Type: pb.Matcher_EQUAL,
|
|
|
|
},
|
|
|
|
err: "",
|
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "((",
|
|
|
|
Type: pb.Matcher_REGEXP,
|
|
|
|
},
|
|
|
|
err: "invalid regular expression",
|
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "))",
|
|
|
|
Type: pb.Matcher_NOT_REGEXP,
|
|
|
|
},
|
|
|
|
err: "invalid regular expression",
|
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "\xff",
|
|
|
|
Type: pb.Matcher_EQUAL,
|
|
|
|
},
|
|
|
|
err: "invalid label value",
|
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "\xf0\x9f\x99\x82", // U+1F642
|
|
|
|
Type: pb.Matcher_EQUAL,
|
|
|
|
},
|
|
|
|
err: "",
|
|
|
|
}, {
|
|
|
|
m: &pb.Matcher{
|
|
|
|
Name: "a",
|
|
|
|
Pattern: "b",
|
|
|
|
Type: 333,
|
|
|
|
},
|
|
|
|
err: "unknown matcher type",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-01-15 10:03:51 +00:00
|
|
|
// Change the mode to UTF-8 mode.
|
|
|
|
ff, err := featurecontrol.NewFlags(log.NewNopLogger(), featurecontrol.FeatureUTF8StrictMode)
|
|
|
|
require.NoError(t, err)
|
Remove metrics from compat package (#3714)
This commit removes the metrics from the compat package
in favour of the existing logging and the additional tools
at hand, such as amtool, to validate Alertmanager configurations.
Due to the global nature of the compat package, a consequence
of config.Load, these metrics have proven to be less useful
in practice than expected, both in Alertmanager and other projects
such as Mimir.
There are a number of reasons for this:
1. Because the compat package is global, these metrics cannot be
reset each time config.Load is called, as in multi-tenant
projects like Mimir loading a config for one tenant would reset
the metrics for all tenants. This is also the reason the metrics
are counters and not gauges.
2. Since the metrics are counters, it is difficult to create
meaningful dashboards for Alertmanager as, unlike in Mimir,
configurations are not reloaded at fixed intervals, and as such,
operators cannot use rate to track configuration changes
over time.
In Alertmanager, there are much better tools available to validate
that an Alertmanager configuration is compatible with the UTF-8
parser, including both the existing logging from Alertmanager
server and amtool check-config.
In other projects like Mimir, we can track configurations for
individual tenants using log aggregation and storage systems
such as Loki. This gives operators far more information than
what is possible with the metrics, including the timestamp,
input and ID of tenant configurations that are incompatible
or have disagreement.
Signed-off-by: George Robinson <george.robinson@grafana.com>
2024-02-08 09:59:03 +00:00
|
|
|
compat.InitFromFlags(log.NewNopLogger(), ff)
|
2024-01-15 10:03:51 +00:00
|
|
|
|
|
|
|
// Restore the mode to classic at the end of the test.
|
|
|
|
ff, err = featurecontrol.NewFlags(log.NewNopLogger(), featurecontrol.FeatureClassicMode)
|
|
|
|
require.NoError(t, err)
|
Remove metrics from compat package (#3714)
This commit removes the metrics from the compat package
in favour of the existing logging and the additional tools
at hand, such as amtool, to validate Alertmanager configurations.
Due to the global nature of the compat package, a consequence
of config.Load, these metrics have proven to be less useful
in practice than expected, both in Alertmanager and other projects
such as Mimir.
There are a number of reasons for this:
1. Because the compat package is global, these metrics cannot be
reset each time config.Load is called, as in multi-tenant
projects like Mimir loading a config for one tenant would reset
the metrics for all tenants. This is also the reason the metrics
are counters and not gauges.
2. Since the metrics are counters, it is difficult to create
meaningful dashboards for Alertmanager as, unlike in Mimir,
configurations are not reloaded at fixed intervals, and as such,
operators cannot use rate to track configuration changes
over time.
In Alertmanager, there are much better tools available to validate
that an Alertmanager configuration is compatible with the UTF-8
parser, including both the existing logging from Alertmanager
server and amtool check-config.
In other projects like Mimir, we can track configurations for
individual tenants using log aggregation and storage systems
such as Loki. This gives operators far more information than
what is possible with the metrics, including the timestamp,
input and ID of tenant configurations that are incompatible
or have disagreement.
Signed-off-by: George Robinson <george.robinson@grafana.com>
2024-02-08 09:59:03 +00:00
|
|
|
defer compat.InitFromFlags(log.NewNopLogger(), ff)
|
2024-01-15 10:03:51 +00:00
|
|
|
|
2023-11-24 10:01:40 +00:00
|
|
|
for _, c := range cases {
|
2024-01-15 10:03:51 +00:00
|
|
|
checkErr(t, c.err, validateMatcher(c.m))
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateSilence(t *testing.T) {
|
|
|
|
var (
|
2022-03-28 04:46:58 +00:00
|
|
|
now = time.Now().UTC()
|
2017-04-18 08:46:40 +00:00
|
|
|
zeroTimestamp = time.Time{}
|
|
|
|
validTimestamp = now
|
2016-08-15 14:10:35 +00:00
|
|
|
)
|
|
|
|
cases := []struct {
|
|
|
|
s *pb.Silence
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
s: &pb.Silence{
|
|
|
|
Id: "some_id",
|
|
|
|
Matchers: []*pb.Matcher{
|
2022-03-25 16:59:51 +00:00
|
|
|
{Name: "a", Pattern: "b"},
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
StartsAt: validTimestamp,
|
|
|
|
EndsAt: validTimestamp,
|
|
|
|
UpdatedAt: validTimestamp,
|
|
|
|
},
|
|
|
|
err: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
s: &pb.Silence{
|
|
|
|
Id: "some_id",
|
|
|
|
Matchers: []*pb.Matcher{},
|
|
|
|
StartsAt: validTimestamp,
|
|
|
|
EndsAt: validTimestamp,
|
|
|
|
UpdatedAt: validTimestamp,
|
|
|
|
},
|
|
|
|
err: "at least one matcher required",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
s: &pb.Silence{
|
|
|
|
Id: "some_id",
|
|
|
|
Matchers: []*pb.Matcher{
|
2022-03-25 16:59:51 +00:00
|
|
|
{Name: "a", Pattern: "b"},
|
|
|
|
{Name: "00", Pattern: "b"},
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
StartsAt: validTimestamp,
|
|
|
|
EndsAt: validTimestamp,
|
|
|
|
UpdatedAt: validTimestamp,
|
|
|
|
},
|
|
|
|
err: "invalid label matcher",
|
|
|
|
},
|
2019-10-31 14:42:03 +00:00
|
|
|
{
|
|
|
|
s: &pb.Silence{
|
|
|
|
Id: "some_id",
|
|
|
|
Matchers: []*pb.Matcher{
|
2022-03-25 16:59:51 +00:00
|
|
|
{Name: "a", Pattern: ""},
|
|
|
|
{Name: "b", Pattern: ".*", Type: pb.Matcher_REGEXP},
|
2019-10-31 14:42:03 +00:00
|
|
|
},
|
|
|
|
StartsAt: validTimestamp,
|
|
|
|
EndsAt: validTimestamp,
|
|
|
|
UpdatedAt: validTimestamp,
|
|
|
|
},
|
|
|
|
err: "at least one matcher must not match the empty string",
|
|
|
|
},
|
2016-08-15 14:10:35 +00:00
|
|
|
{
|
|
|
|
s: &pb.Silence{
|
|
|
|
Id: "some_id",
|
|
|
|
Matchers: []*pb.Matcher{
|
2022-03-25 16:59:51 +00:00
|
|
|
{Name: "a", Pattern: "b"},
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: now,
|
|
|
|
EndsAt: now.Add(-time.Second),
|
2016-08-15 14:10:35 +00:00
|
|
|
UpdatedAt: validTimestamp,
|
|
|
|
},
|
|
|
|
err: "end time must not be before start time",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
s: &pb.Silence{
|
|
|
|
Id: "some_id",
|
|
|
|
Matchers: []*pb.Matcher{
|
2022-03-25 16:59:51 +00:00
|
|
|
{Name: "a", Pattern: "b"},
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: zeroTimestamp,
|
2016-08-15 14:10:35 +00:00
|
|
|
EndsAt: validTimestamp,
|
|
|
|
UpdatedAt: validTimestamp,
|
|
|
|
},
|
2017-04-18 08:46:40 +00:00
|
|
|
err: "invalid zero start timestamp",
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
s: &pb.Silence{
|
|
|
|
Id: "some_id",
|
|
|
|
Matchers: []*pb.Matcher{
|
2022-03-25 16:59:51 +00:00
|
|
|
{Name: "a", Pattern: "b"},
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
StartsAt: validTimestamp,
|
2017-04-18 08:46:40 +00:00
|
|
|
EndsAt: zeroTimestamp,
|
2016-08-15 14:10:35 +00:00
|
|
|
UpdatedAt: validTimestamp,
|
|
|
|
},
|
2017-04-18 08:46:40 +00:00
|
|
|
err: "invalid zero end timestamp",
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
2024-01-15 10:03:51 +00:00
|
|
|
checkErr(t, c.err, validateSilence(c.s))
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 09:17:48 +00:00
|
|
|
func TestStateMerge(t *testing.T) {
|
2022-03-28 04:46:58 +00:00
|
|
|
now := time.Now().UTC()
|
2016-08-15 14:10:35 +00:00
|
|
|
|
|
|
|
// We only care about key names and timestamps for the
|
|
|
|
// merging logic.
|
2018-11-21 10:40:57 +00:00
|
|
|
newSilence := func(id string, ts, exp time.Time) *pb.MeshSilence {
|
2016-08-15 14:10:35 +00:00
|
|
|
return &pb.MeshSilence{
|
2018-11-21 10:40:57 +00:00
|
|
|
Silence: &pb.Silence{Id: id, UpdatedAt: ts},
|
|
|
|
ExpiresAt: exp,
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-28 08:25:35 +00:00
|
|
|
|
2018-11-21 10:40:57 +00:00
|
|
|
exp := now.Add(time.Minute)
|
|
|
|
|
2016-08-15 14:10:35 +00:00
|
|
|
cases := []struct {
|
2018-02-27 09:17:48 +00:00
|
|
|
a, b state
|
|
|
|
final state
|
2016-08-15 14:10:35 +00:00
|
|
|
}{
|
|
|
|
{
|
2018-02-07 15:36:47 +00:00
|
|
|
a: state{
|
2018-11-21 10:40:57 +00:00
|
|
|
"a1": newSilence("a1", now, exp),
|
|
|
|
"a2": newSilence("a2", now, exp),
|
|
|
|
"a3": newSilence("a3", now, exp),
|
2018-02-07 15:36:47 +00:00
|
|
|
},
|
|
|
|
b: state{
|
2018-11-21 10:40:57 +00:00
|
|
|
"b1": newSilence("b1", now, exp), // new key, should be added
|
|
|
|
"a2": newSilence("a2", now.Add(-time.Minute), exp), // older timestamp, should be dropped
|
|
|
|
"a3": newSilence("a3", now.Add(time.Minute), exp), // newer timestamp, should overwrite
|
|
|
|
"a4": newSilence("a4", now.Add(-time.Minute), now.Add(-time.Millisecond)), // new key, expired, should not be added
|
2018-02-07 15:36:47 +00:00
|
|
|
},
|
|
|
|
final: state{
|
2018-11-21 10:40:57 +00:00
|
|
|
"a1": newSilence("a1", now, exp),
|
|
|
|
"a2": newSilence("a2", now, exp),
|
|
|
|
"a3": newSilence("a3", now.Add(time.Minute), exp),
|
|
|
|
"b1": newSilence("b1", now, exp),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2018-02-27 09:17:48 +00:00
|
|
|
for _, e := range c.b {
|
2018-11-21 10:40:57 +00:00
|
|
|
c.a.merge(e, now)
|
2017-09-28 08:25:35 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 09:17:48 +00:00
|
|
|
require.Equal(t, c.final, c.a, "Merge result should match expectation")
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
func TestStateCoding(t *testing.T) {
|
2016-08-15 14:10:35 +00:00
|
|
|
// Check whether encoding and decoding the data is symmetric.
|
2022-03-28 04:46:58 +00:00
|
|
|
now := time.Now().UTC()
|
2016-08-15 14:10:35 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
entries []*pb.MeshSilence
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
entries: []*pb.MeshSilence{
|
|
|
|
{
|
|
|
|
Silence: &pb.Silence{
|
|
|
|
Id: "3be80475-e219-4ee7-b6fc-4b65114e362f",
|
|
|
|
Matchers: []*pb.Matcher{
|
|
|
|
{Name: "label1", Pattern: "val1", Type: pb.Matcher_EQUAL},
|
|
|
|
{Name: "label2", Pattern: "val.+", Type: pb.Matcher_REGEXP},
|
|
|
|
},
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: now,
|
|
|
|
EndsAt: now,
|
|
|
|
UpdatedAt: now,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-04-18 08:46:40 +00:00
|
|
|
ExpiresAt: now,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Silence: &pb.Silence{
|
|
|
|
Id: "4b1e760d-182c-4980-b873-c1a6827c9817",
|
|
|
|
Matchers: []*pb.Matcher{
|
|
|
|
{Name: "label1", Pattern: "val1", Type: pb.Matcher_EQUAL},
|
|
|
|
},
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: now.Add(time.Hour),
|
|
|
|
EndsAt: now.Add(2 * time.Hour),
|
|
|
|
UpdatedAt: now,
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2017-04-18 08:46:40 +00:00
|
|
|
ExpiresAt: now.Add(24 * time.Hour),
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
2021-01-18 20:58:20 +00:00
|
|
|
{
|
|
|
|
Silence: &pb.Silence{
|
|
|
|
Id: "3dfb2528-59ce-41eb-b465-f875a4e744a4",
|
|
|
|
Matchers: []*pb.Matcher{
|
|
|
|
{Name: "label1", Pattern: "val1", Type: pb.Matcher_NOT_EQUAL},
|
|
|
|
{Name: "label2", Pattern: "val.+", Type: pb.Matcher_NOT_REGEXP},
|
|
|
|
},
|
|
|
|
StartsAt: now,
|
|
|
|
EndsAt: now,
|
|
|
|
UpdatedAt: now,
|
|
|
|
},
|
|
|
|
ExpiresAt: now,
|
|
|
|
},
|
2016-08-15 14:10:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
// Create gossip data from input.
|
2018-02-07 15:36:47 +00:00
|
|
|
in := state{}
|
2016-08-15 14:10:35 +00:00
|
|
|
for _, e := range c.entries {
|
2018-02-07 15:36:47 +00:00
|
|
|
in[e.Silence.Id] = e
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
msg, err := in.MarshalBinary()
|
|
|
|
require.NoError(t, err)
|
2016-08-15 14:10:35 +00:00
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
out, err := decodeState(bytes.NewReader(msg))
|
2016-08-15 14:10:35 +00:00
|
|
|
require.NoError(t, err, "decoding message failed")
|
|
|
|
|
|
|
|
require.Equal(t, in, out, "decoded data doesn't match encoded data")
|
|
|
|
}
|
|
|
|
}
|
2018-04-10 08:12:05 +00:00
|
|
|
|
|
|
|
func TestStateDecodingError(t *testing.T) {
|
|
|
|
// Check whether decoding copes with erroneous data.
|
|
|
|
s := state{"": &pb.MeshSilence{}}
|
|
|
|
|
|
|
|
msg, err := s.MarshalBinary()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = decodeState(bytes.NewReader(msg))
|
|
|
|
require.Equal(t, ErrInvalidState, err)
|
|
|
|
}
|
2019-02-26 18:20:13 +00:00
|
|
|
|
2023-02-24 11:46:20 +00:00
|
|
|
// runtime.Gosched() does not "suspend" the current goroutine so there's no guarantee that the main goroutine won't
|
|
|
|
// be able to continue. For more see https://pkg.go.dev/runtime#Gosched.
|
|
|
|
func gosched() {
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
}
|