2015-10-11 15:24:49 +00:00
|
|
|
// Copyright 2015 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.
|
2015-09-29 13:12:31 +00:00
|
|
|
package notify
|
2015-09-28 12:13:18 +00:00
|
|
|
|
|
|
|
import (
|
2016-08-16 12:09:06 +00:00
|
|
|
"errors"
|
2015-09-28 19:43:28 +00:00
|
|
|
"fmt"
|
2016-08-16 12:09:06 +00:00
|
|
|
"io"
|
2015-09-28 12:13:18 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2015-09-28 16:28:13 +00:00
|
|
|
"time"
|
2015-09-28 12:13:18 +00:00
|
|
|
|
2017-10-22 05:59:33 +00:00
|
|
|
"github.com/go-kit/kit/log"
|
2016-08-16 12:09:06 +00:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
|
|
|
"github.com/golang/protobuf/ptypes/timestamp"
|
2015-09-28 12:13:18 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2016-08-16 12:09:06 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-09-28 12:13:18 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-08-16 12:09:06 +00:00
|
|
|
"github.com/prometheus/alertmanager/nflog"
|
2016-08-16 12:32:24 +00:00
|
|
|
"github.com/prometheus/alertmanager/nflog/nflogpb"
|
2016-08-30 09:58:27 +00:00
|
|
|
"github.com/prometheus/alertmanager/silence"
|
|
|
|
"github.com/prometheus/alertmanager/silence/silencepb"
|
2015-09-28 12:13:18 +00:00
|
|
|
"github.com/prometheus/alertmanager/types"
|
|
|
|
)
|
|
|
|
|
2016-10-05 14:28:04 +00:00
|
|
|
type notifierConfigFunc func() bool
|
|
|
|
|
|
|
|
func (f notifierConfigFunc) SendResolved() bool {
|
|
|
|
return f()
|
|
|
|
}
|
|
|
|
|
|
|
|
type notifierFunc func(ctx context.Context, alerts ...*types.Alert) (bool, error)
|
|
|
|
|
|
|
|
func (f notifierFunc) Notify(ctx context.Context, alerts ...*types.Alert) (bool, error) {
|
|
|
|
return f(ctx, alerts...)
|
|
|
|
}
|
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
type failStage struct{}
|
2015-09-28 12:13:18 +00:00
|
|
|
|
2017-10-22 05:59:33 +00:00
|
|
|
func (s failStage) Exec(ctx context.Context, l log.Logger, as ...*types.Alert) (context.Context, []*types.Alert, error) {
|
2016-08-17 08:54:17 +00:00
|
|
|
return ctx, nil, fmt.Errorf("some error")
|
2015-09-28 19:43:28 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:09:06 +00:00
|
|
|
type testNflog struct {
|
|
|
|
qres []*nflogpb.Entry
|
|
|
|
qerr error
|
|
|
|
|
2017-04-21 09:43:12 +00:00
|
|
|
logFunc func(r *nflogpb.Receiver, gkey string, firingAlerts, resolvedAlerts []uint64) error
|
2016-08-16 12:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *testNflog) Query(p ...nflog.QueryParam) ([]*nflogpb.Entry, error) {
|
|
|
|
return l.qres, l.qerr
|
|
|
|
}
|
|
|
|
|
2017-04-21 09:43:12 +00:00
|
|
|
func (l *testNflog) Log(r *nflogpb.Receiver, gkey string, firingAlerts, resolvedAlerts []uint64) error {
|
2017-03-13 12:44:36 +00:00
|
|
|
return l.logFunc(r, gkey, firingAlerts, resolvedAlerts)
|
2016-08-16 12:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *testNflog) GC() (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *testNflog) Snapshot(w io.Writer) (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustTimestampProto(ts time.Time) *timestamp.Timestamp {
|
|
|
|
tspb, err := ptypes.TimestampProto(ts)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return tspb
|
|
|
|
}
|
|
|
|
|
2017-03-13 12:44:36 +00:00
|
|
|
func alertHashSet(hashes ...uint64) map[uint64]struct{} {
|
|
|
|
res := map[uint64]struct{}{}
|
|
|
|
|
|
|
|
for _, h := range hashes {
|
|
|
|
res[h] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:09:06 +00:00
|
|
|
func TestDedupStageNeedsUpdate(t *testing.T) {
|
|
|
|
now := utcNow()
|
|
|
|
|
2016-01-08 14:15:14 +00:00
|
|
|
cases := []struct {
|
2017-03-13 12:44:36 +00:00
|
|
|
entry *nflogpb.Entry
|
|
|
|
firingAlerts map[uint64]struct{}
|
|
|
|
repeat time.Duration
|
2016-08-16 12:09:06 +00:00
|
|
|
|
|
|
|
res bool
|
|
|
|
resErr bool
|
2016-01-08 14:15:14 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-03-13 12:44:36 +00:00
|
|
|
entry: nil,
|
|
|
|
firingAlerts: alertHashSet(2, 3, 4),
|
|
|
|
res: true,
|
2016-08-16 12:09:06 +00:00
|
|
|
}, {
|
2017-03-13 12:44:36 +00:00
|
|
|
entry: &nflogpb.Entry{FiringAlerts: []uint64{1, 2, 3}},
|
|
|
|
firingAlerts: alertHashSet(2, 3, 4),
|
|
|
|
res: true,
|
2016-08-16 12:09:06 +00:00
|
|
|
}, {
|
|
|
|
entry: &nflogpb.Entry{
|
2017-03-13 12:44:36 +00:00
|
|
|
FiringAlerts: []uint64{1, 2, 3},
|
2017-04-18 08:03:57 +00:00
|
|
|
Timestamp: time.Time{}, // zero timestamp should always update
|
2016-01-08 14:15:14 +00:00
|
|
|
},
|
2017-03-13 12:44:36 +00:00
|
|
|
firingAlerts: alertHashSet(1, 2, 3),
|
2017-04-18 08:03:57 +00:00
|
|
|
res: true,
|
2016-08-16 12:09:06 +00:00
|
|
|
}, {
|
|
|
|
entry: &nflogpb.Entry{
|
2017-03-13 12:44:36 +00:00
|
|
|
FiringAlerts: []uint64{1, 2, 3},
|
2017-04-18 08:03:57 +00:00
|
|
|
Timestamp: now.Add(-9 * time.Minute),
|
2016-01-08 14:15:14 +00:00
|
|
|
},
|
2017-03-13 12:44:36 +00:00
|
|
|
repeat: 10 * time.Minute,
|
|
|
|
firingAlerts: alertHashSet(1, 2, 3),
|
|
|
|
res: false,
|
2016-08-16 12:09:06 +00:00
|
|
|
}, {
|
|
|
|
entry: &nflogpb.Entry{
|
2017-03-13 12:44:36 +00:00
|
|
|
FiringAlerts: []uint64{1, 2, 3},
|
2017-04-18 08:03:57 +00:00
|
|
|
Timestamp: now.Add(-11 * time.Minute),
|
2016-01-08 14:15:14 +00:00
|
|
|
},
|
2017-03-13 12:44:36 +00:00
|
|
|
repeat: 10 * time.Minute,
|
|
|
|
firingAlerts: alertHashSet(1, 2, 3),
|
|
|
|
res: true,
|
2016-01-08 14:15:14 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, c := range cases {
|
2016-08-16 12:09:06 +00:00
|
|
|
t.Log("case", i)
|
|
|
|
|
|
|
|
s := &DedupStage{
|
|
|
|
now: func() time.Time { return now },
|
2016-01-08 14:15:14 +00:00
|
|
|
}
|
2017-03-13 12:44:36 +00:00
|
|
|
ok, err := s.needsUpdate(c.entry, c.firingAlerts, nil, c.repeat)
|
2016-08-16 12:09:06 +00:00
|
|
|
if c.resErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
require.Equal(t, c.res, ok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDedupStage(t *testing.T) {
|
2017-03-13 12:44:36 +00:00
|
|
|
i := 0
|
|
|
|
now := utcNow()
|
2016-08-16 12:09:06 +00:00
|
|
|
s := &DedupStage{
|
2017-03-13 12:44:36 +00:00
|
|
|
hash: func(a *types.Alert) uint64 {
|
|
|
|
res := uint64(i)
|
|
|
|
i++
|
|
|
|
return res
|
|
|
|
},
|
|
|
|
now: func() time.Time {
|
|
|
|
return now
|
|
|
|
},
|
2016-08-16 12:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2017-10-22 05:59:33 +00:00
|
|
|
_, _, err := s.Exec(ctx, log.NewNopLogger())
|
2016-08-16 12:09:06 +00:00
|
|
|
require.EqualError(t, err, "group key missing")
|
|
|
|
|
2017-04-21 09:43:12 +00:00
|
|
|
ctx = WithGroupKey(ctx, "1")
|
2016-08-16 12:09:06 +00:00
|
|
|
|
2017-10-22 05:59:33 +00:00
|
|
|
_, _, err = s.Exec(ctx, log.NewNopLogger())
|
2016-08-16 12:09:06 +00:00
|
|
|
require.EqualError(t, err, "repeat interval missing")
|
|
|
|
|
|
|
|
ctx = WithRepeatInterval(ctx, time.Hour)
|
|
|
|
|
|
|
|
alerts := []*types.Alert{{}, {}, {}}
|
|
|
|
|
|
|
|
// Must catch notification log query errors.
|
|
|
|
s.nflog = &testNflog{
|
|
|
|
qerr: errors.New("bad things"),
|
|
|
|
}
|
2017-10-22 05:59:33 +00:00
|
|
|
ctx, res, err := s.Exec(ctx, log.NewNopLogger(), alerts...)
|
2016-08-16 12:09:06 +00:00
|
|
|
require.EqualError(t, err, "bad things")
|
|
|
|
|
|
|
|
// ... but skip ErrNotFound.
|
|
|
|
s.nflog = &testNflog{
|
|
|
|
qerr: nflog.ErrNotFound,
|
|
|
|
}
|
2017-10-22 05:59:33 +00:00
|
|
|
ctx, res, err = s.Exec(ctx, log.NewNopLogger(), alerts...)
|
2016-08-16 12:09:06 +00:00
|
|
|
require.NoError(t, err, "unexpected error on not found log entry")
|
|
|
|
require.Equal(t, alerts, res, "input alerts differ from result alerts")
|
|
|
|
|
|
|
|
s.nflog = &testNflog{
|
|
|
|
qerr: nil,
|
|
|
|
qres: []*nflogpb.Entry{
|
2017-03-13 12:44:36 +00:00
|
|
|
{FiringAlerts: []uint64{0, 1, 2}},
|
|
|
|
{FiringAlerts: []uint64{1, 2, 3}},
|
2016-08-16 12:09:06 +00:00
|
|
|
},
|
|
|
|
}
|
2017-10-22 05:59:33 +00:00
|
|
|
ctx, res, err = s.Exec(ctx, log.NewNopLogger(), alerts...)
|
2016-08-16 12:09:06 +00:00
|
|
|
require.Contains(t, err.Error(), "result size")
|
|
|
|
|
|
|
|
// Must return no error and no alerts no need to update.
|
2017-03-13 12:44:36 +00:00
|
|
|
i = 0
|
2016-08-16 12:09:06 +00:00
|
|
|
s.nflog = &testNflog{
|
|
|
|
qerr: nflog.ErrNotFound,
|
2017-03-13 12:44:36 +00:00
|
|
|
qres: []*nflogpb.Entry{
|
|
|
|
{
|
|
|
|
FiringAlerts: []uint64{0, 1, 2},
|
2017-04-18 08:03:57 +00:00
|
|
|
Timestamp: now,
|
2017-03-13 12:44:36 +00:00
|
|
|
},
|
|
|
|
},
|
2016-01-08 14:15:14 +00:00
|
|
|
}
|
2017-10-22 05:59:33 +00:00
|
|
|
ctx, res, err = s.Exec(ctx, log.NewNopLogger(), alerts...)
|
2016-08-16 12:09:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, res, "unexpected alerts returned")
|
|
|
|
|
|
|
|
// Must return no error and all input alerts on changes.
|
2017-03-13 12:44:36 +00:00
|
|
|
i = 0
|
2016-08-16 12:09:06 +00:00
|
|
|
s.nflog = &testNflog{
|
|
|
|
qerr: nil,
|
|
|
|
qres: []*nflogpb.Entry{
|
2017-03-13 12:44:36 +00:00
|
|
|
{
|
|
|
|
FiringAlerts: []uint64{1, 2, 3, 4},
|
2017-04-18 08:03:57 +00:00
|
|
|
Timestamp: now,
|
2017-03-13 12:44:36 +00:00
|
|
|
},
|
2016-08-16 12:09:06 +00:00
|
|
|
},
|
|
|
|
}
|
2017-10-22 05:59:33 +00:00
|
|
|
ctx, res, err = s.Exec(ctx, log.NewNopLogger(), alerts...)
|
2016-08-16 12:09:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, alerts, res, "unexpected alerts returned")
|
2016-01-08 14:15:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
func TestMultiStage(t *testing.T) {
|
|
|
|
var (
|
|
|
|
alerts1 = []*types.Alert{{}}
|
|
|
|
alerts2 = []*types.Alert{{}, {}}
|
|
|
|
alerts3 = []*types.Alert{{}, {}, {}}
|
|
|
|
)
|
|
|
|
|
|
|
|
stage := MultiStage{
|
2017-10-22 05:59:33 +00:00
|
|
|
StageFunc(func(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
|
2016-08-12 17:18:26 +00:00
|
|
|
if !reflect.DeepEqual(alerts, alerts1) {
|
|
|
|
t.Fatal("Input not equal to input of MultiStage")
|
|
|
|
}
|
2016-08-17 08:54:17 +00:00
|
|
|
ctx = context.WithValue(ctx, "key", "value")
|
|
|
|
return ctx, alerts2, nil
|
2016-08-12 17:18:26 +00:00
|
|
|
}),
|
2017-10-22 05:59:33 +00:00
|
|
|
StageFunc(func(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
|
2016-08-12 17:18:26 +00:00
|
|
|
if !reflect.DeepEqual(alerts, alerts2) {
|
|
|
|
t.Fatal("Input not equal to output of previous stage")
|
|
|
|
}
|
2016-08-17 08:54:17 +00:00
|
|
|
v, ok := ctx.Value("key").(string)
|
|
|
|
if !ok || v != "value" {
|
|
|
|
t.Fatalf("Expected value %q for key %q but got %q", "value", "key", v)
|
|
|
|
}
|
|
|
|
return ctx, alerts3, nil
|
2016-08-12 17:18:26 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2017-10-22 05:59:33 +00:00
|
|
|
_, alerts, err := stage.Exec(context.Background(), log.NewNopLogger(), alerts1...)
|
2016-08-12 17:18:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Exec failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(alerts, alerts3) {
|
|
|
|
t.Fatal("Output of MultiStage is not equal to the output of the last stage")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultiStageFailure(t *testing.T) {
|
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
s1 = failStage{}
|
|
|
|
stage = MultiStage{s1}
|
|
|
|
)
|
|
|
|
|
2017-10-22 05:59:33 +00:00
|
|
|
_, _, err := stage.Exec(ctx, log.NewNopLogger(), nil)
|
2016-08-12 17:18:26 +00:00
|
|
|
if err.Error() != "some error" {
|
|
|
|
t.Fatal("Errors were not propagated correctly by MultiStage")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoutingStage(t *testing.T) {
|
|
|
|
var (
|
|
|
|
alerts1 = []*types.Alert{{}}
|
|
|
|
alerts2 = []*types.Alert{{}, {}}
|
|
|
|
)
|
|
|
|
|
|
|
|
stage := RoutingStage{
|
2017-10-22 05:59:33 +00:00
|
|
|
"name": StageFunc(func(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
|
2016-08-12 17:18:26 +00:00
|
|
|
if !reflect.DeepEqual(alerts, alerts1) {
|
|
|
|
t.Fatal("Input not equal to input of RoutingStage")
|
|
|
|
}
|
2016-08-17 08:54:17 +00:00
|
|
|
return ctx, alerts2, nil
|
2016-08-12 17:18:26 +00:00
|
|
|
}),
|
|
|
|
"not": failStage{},
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:22:47 +00:00
|
|
|
ctx := WithReceiverName(context.Background(), "name")
|
2016-08-12 17:18:26 +00:00
|
|
|
|
2017-10-22 05:59:33 +00:00
|
|
|
_, alerts, err := stage.Exec(ctx, log.NewNopLogger(), alerts1...)
|
2016-08-12 17:18:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Exec failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(alerts, alerts2) {
|
|
|
|
t.Fatal("Output of RoutingStage is not equal to the output of the inner stage")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 12:50:40 +00:00
|
|
|
func TestIntegrationNoResolved(t *testing.T) {
|
2016-10-05 14:28:04 +00:00
|
|
|
res := []*types.Alert{}
|
|
|
|
r := notifierFunc(func(ctx context.Context, alerts ...*types.Alert) (bool, error) {
|
|
|
|
res = append(res, alerts...)
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
})
|
2017-01-04 12:50:40 +00:00
|
|
|
i := Integration{
|
2016-10-05 14:28:04 +00:00
|
|
|
notifier: r,
|
|
|
|
conf: notifierConfigFunc(func() bool { return false }),
|
|
|
|
}
|
2017-01-04 12:50:40 +00:00
|
|
|
|
|
|
|
alerts := []*types.Alert{
|
|
|
|
&types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
EndsAt: time.Now().Add(-time.Hour),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
EndsAt: time.Now().Add(time.Hour),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Notify(nil, alerts...)
|
|
|
|
|
|
|
|
require.Equal(t, len(res), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntegrationSendResolved(t *testing.T) {
|
|
|
|
res := []*types.Alert{}
|
|
|
|
r := notifierFunc(func(ctx context.Context, alerts ...*types.Alert) (bool, error) {
|
|
|
|
res = append(res, alerts...)
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
i := Integration{
|
2016-10-05 14:28:04 +00:00
|
|
|
notifier: r,
|
|
|
|
conf: notifierConfigFunc(func() bool { return true }),
|
|
|
|
}
|
|
|
|
|
|
|
|
alerts := []*types.Alert{
|
|
|
|
&types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
EndsAt: time.Now().Add(-time.Hour),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-01-04 12:50:40 +00:00
|
|
|
i.Notify(nil, alerts...)
|
2016-10-05 14:28:04 +00:00
|
|
|
|
2017-01-04 12:50:40 +00:00
|
|
|
require.Equal(t, len(res), 1)
|
2016-10-05 14:28:04 +00:00
|
|
|
require.Equal(t, res, alerts)
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:32:24 +00:00
|
|
|
func TestSetNotifiesStage(t *testing.T) {
|
2016-08-16 12:09:06 +00:00
|
|
|
tnflog := &testNflog{}
|
|
|
|
s := &SetNotifiesStage{
|
|
|
|
recv: &nflogpb.Receiver{GroupName: "test"},
|
|
|
|
nflog: tnflog,
|
|
|
|
}
|
|
|
|
alerts := []*types.Alert{{}, {}, {}}
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2017-10-22 05:59:33 +00:00
|
|
|
resctx, res, err := s.Exec(ctx, log.NewNopLogger(), alerts...)
|
2017-03-13 12:44:36 +00:00
|
|
|
require.EqualError(t, err, "group key missing")
|
2016-08-16 12:09:06 +00:00
|
|
|
require.Nil(t, res)
|
|
|
|
require.NotNil(t, resctx)
|
|
|
|
|
2017-04-21 09:43:12 +00:00
|
|
|
ctx = WithGroupKey(ctx, "1")
|
2016-08-16 12:09:06 +00:00
|
|
|
|
2017-10-22 05:59:33 +00:00
|
|
|
resctx, res, err = s.Exec(ctx, log.NewNopLogger(), alerts...)
|
2017-03-13 12:44:36 +00:00
|
|
|
require.EqualError(t, err, "firing alerts missing")
|
2016-08-16 12:09:06 +00:00
|
|
|
require.Nil(t, res)
|
|
|
|
require.NotNil(t, resctx)
|
|
|
|
|
2017-03-13 12:44:36 +00:00
|
|
|
ctx = WithFiringAlerts(ctx, []uint64{0, 1, 2})
|
|
|
|
|
2017-10-22 05:59:33 +00:00
|
|
|
resctx, res, err = s.Exec(ctx, log.NewNopLogger(), alerts...)
|
2017-03-13 12:44:36 +00:00
|
|
|
require.EqualError(t, err, "resolved alerts missing")
|
|
|
|
require.Nil(t, res)
|
|
|
|
require.NotNil(t, resctx)
|
|
|
|
|
|
|
|
ctx = WithResolvedAlerts(ctx, []uint64{})
|
2016-08-16 12:09:06 +00:00
|
|
|
|
2017-04-21 09:43:12 +00:00
|
|
|
tnflog.logFunc = func(r *nflogpb.Receiver, gkey string, firingAlerts, resolvedAlerts []uint64) error {
|
2016-08-16 12:09:06 +00:00
|
|
|
require.Equal(t, s.recv, r)
|
2017-04-21 09:43:12 +00:00
|
|
|
require.Equal(t, "1", gkey)
|
2017-03-13 12:44:36 +00:00
|
|
|
require.Equal(t, []uint64{0, 1, 2}, firingAlerts)
|
|
|
|
require.Equal(t, []uint64{}, resolvedAlerts)
|
2016-08-16 12:09:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-10-22 05:59:33 +00:00
|
|
|
resctx, res, err = s.Exec(ctx, log.NewNopLogger(), alerts...)
|
2016-08-16 12:09:06 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, alerts, res)
|
|
|
|
require.NotNil(t, resctx)
|
|
|
|
|
2017-03-13 12:44:36 +00:00
|
|
|
ctx = WithFiringAlerts(ctx, []uint64{})
|
|
|
|
ctx = WithResolvedAlerts(ctx, []uint64{0, 1, 2})
|
|
|
|
|
2017-04-21 09:43:12 +00:00
|
|
|
tnflog.logFunc = func(r *nflogpb.Receiver, gkey string, firingAlerts, resolvedAlerts []uint64) error {
|
2016-08-16 12:09:06 +00:00
|
|
|
require.Equal(t, s.recv, r)
|
2017-04-21 09:43:12 +00:00
|
|
|
require.Equal(t, "1", gkey)
|
2017-03-13 12:44:36 +00:00
|
|
|
require.Equal(t, []uint64{}, firingAlerts)
|
|
|
|
require.Equal(t, []uint64{0, 1, 2}, resolvedAlerts)
|
2016-08-16 12:09:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-10-22 05:59:33 +00:00
|
|
|
resctx, res, err = s.Exec(ctx, log.NewNopLogger(), alerts...)
|
2016-08-16 12:09:06 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, alerts, res)
|
|
|
|
require.NotNil(t, resctx)
|
2015-09-28 19:43:28 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 13:22:17 +00:00
|
|
|
func TestSilenceStage(t *testing.T) {
|
2016-08-30 09:58:27 +00:00
|
|
|
silences, err := silence.New(silence.Options{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-16 14:48:25 +00:00
|
|
|
if _, err := silences.Set(&silencepb.Silence{
|
2017-04-18 08:46:40 +00:00
|
|
|
EndsAt: utcNow().Add(time.Hour),
|
2016-08-30 09:58:27 +00:00
|
|
|
Matchers: []*silencepb.Matcher{{Name: "mute", Pattern: "me"}},
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-28 12:13:18 +00:00
|
|
|
|
2015-12-03 16:27:36 +00:00
|
|
|
marker := types.NewMarker()
|
2016-08-30 09:58:27 +00:00
|
|
|
silencer := NewSilenceStage(silences, marker)
|
2015-09-28 12:13:18 +00:00
|
|
|
|
|
|
|
in := []model.LabelSet{
|
|
|
|
{},
|
|
|
|
{"test": "set"},
|
|
|
|
{"mute": "me"},
|
|
|
|
{"foo": "bar", "test": "set"},
|
|
|
|
{"foo": "bar", "mute": "me"},
|
|
|
|
{},
|
|
|
|
{"not": "muted"},
|
|
|
|
}
|
|
|
|
out := []model.LabelSet{
|
|
|
|
{},
|
|
|
|
{"test": "set"},
|
|
|
|
{"foo": "bar", "test": "set"},
|
|
|
|
{},
|
|
|
|
{"not": "muted"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var inAlerts []*types.Alert
|
|
|
|
for _, lset := range in {
|
2015-10-01 12:53:49 +00:00
|
|
|
inAlerts = append(inAlerts, &types.Alert{
|
|
|
|
Alert: model.Alert{Labels: lset},
|
|
|
|
})
|
2015-09-28 12:13:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 12:18:52 +00:00
|
|
|
// Set the second alert as previously silenced. It is expected to have
|
2015-12-03 16:27:36 +00:00
|
|
|
// the WasSilenced flag set to true afterwards.
|
2016-08-30 09:58:27 +00:00
|
|
|
marker.SetSilenced(inAlerts[1].Fingerprint(), "123")
|
2015-12-03 16:27:36 +00:00
|
|
|
|
2017-10-22 05:59:33 +00:00
|
|
|
_, alerts, err := silencer.Exec(nil, log.NewNopLogger(), inAlerts...)
|
2016-08-11 13:04:03 +00:00
|
|
|
if err != nil {
|
2016-08-12 17:18:26 +00:00
|
|
|
t.Fatalf("Exec failed: %s", err)
|
2015-12-03 16:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var got []model.LabelSet
|
2017-10-10 13:49:39 +00:00
|
|
|
for _, a := range alerts {
|
2015-12-03 16:27:36 +00:00
|
|
|
got = append(got, a.Labels)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, out) {
|
|
|
|
t.Fatalf("Muting failed, expected: %v\ngot %v", out, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 13:22:17 +00:00
|
|
|
func TestInhibitStage(t *testing.T) {
|
2015-12-03 16:27:36 +00:00
|
|
|
// Mute all label sets that have a "mute" key.
|
|
|
|
muter := types.MuteFunc(func(lset model.LabelSet) bool {
|
|
|
|
_, ok := lset["mute"]
|
|
|
|
return ok
|
|
|
|
})
|
|
|
|
|
|
|
|
marker := types.NewMarker()
|
2016-08-12 13:22:17 +00:00
|
|
|
inhibitor := NewInhibitStage(muter, marker)
|
2015-12-03 16:27:36 +00:00
|
|
|
|
|
|
|
in := []model.LabelSet{
|
|
|
|
{},
|
|
|
|
{"test": "set"},
|
|
|
|
{"mute": "me"},
|
|
|
|
{"foo": "bar", "test": "set"},
|
|
|
|
{"foo": "bar", "mute": "me"},
|
|
|
|
{},
|
|
|
|
{"not": "muted"},
|
|
|
|
}
|
|
|
|
out := []model.LabelSet{
|
|
|
|
{},
|
|
|
|
{"test": "set"},
|
|
|
|
{"foo": "bar", "test": "set"},
|
|
|
|
{},
|
|
|
|
{"not": "muted"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var inAlerts []*types.Alert
|
|
|
|
for _, lset := range in {
|
|
|
|
inAlerts = append(inAlerts, &types.Alert{
|
|
|
|
Alert: model.Alert{Labels: lset},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the second alert as previously inhibited. It is expected to have
|
|
|
|
// the WasInhibited flag set to true afterwards.
|
2017-04-27 12:18:52 +00:00
|
|
|
marker.SetInhibited(inAlerts[1].Fingerprint(), "123")
|
2015-12-03 16:27:36 +00:00
|
|
|
|
2017-10-22 05:59:33 +00:00
|
|
|
_, alerts, err := inhibitor.Exec(nil, log.NewNopLogger(), inAlerts...)
|
2016-08-11 13:04:03 +00:00
|
|
|
if err != nil {
|
2016-08-12 17:18:26 +00:00
|
|
|
t.Fatalf("Exec failed: %s", err)
|
2015-09-28 12:13:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var got []model.LabelSet
|
2017-10-10 13:49:39 +00:00
|
|
|
for _, a := range alerts {
|
2015-09-28 12:13:18 +00:00
|
|
|
got = append(got, a.Labels)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, out) {
|
|
|
|
t.Fatalf("Muting failed, expected: %v\ngot %v", out, got)
|
|
|
|
}
|
|
|
|
}
|