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 (
|
2015-09-28 19:43:28 +00:00
|
|
|
"fmt"
|
2015-09-28 12:13:18 +00:00
|
|
|
"reflect"
|
2016-05-29 22:35:36 +00:00
|
|
|
"sync"
|
2015-09-28 12:13:18 +00:00
|
|
|
"testing"
|
2015-09-28 16:28:13 +00:00
|
|
|
"time"
|
2015-09-28 12:13:18 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/prometheus/alertmanager/types"
|
2016-05-29 23:12:05 +00:00
|
|
|
"github.com/satori/go.uuid"
|
2015-09-28 12:13:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type recordNotifier struct {
|
2015-09-28 16:28:13 +00:00
|
|
|
ctx context.Context
|
2015-09-28 12:13:18 +00:00
|
|
|
alerts []*types.Alert
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *recordNotifier) Notify(ctx context.Context, as ...*types.Alert) error {
|
2015-09-28 16:28:13 +00:00
|
|
|
n.ctx = ctx
|
2015-09-28 12:13:18 +00:00
|
|
|
n.alerts = append(n.alerts, as...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-28 19:43:28 +00:00
|
|
|
type failNotifier struct{}
|
|
|
|
|
|
|
|
func (n *failNotifier) Notify(ctx context.Context, as ...*types.Alert) error {
|
2015-10-09 07:37:32 +00:00
|
|
|
return fmt.Errorf("some error")
|
2015-09-28 19:43:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 14:15:14 +00:00
|
|
|
func TestDedupingNotifierHasUpdate(t *testing.T) {
|
|
|
|
var (
|
2016-08-11 13:04:03 +00:00
|
|
|
n = &Deduplicator{}
|
2016-01-08 14:15:14 +00:00
|
|
|
now = time.Now()
|
|
|
|
interval = 100 * time.Minute
|
|
|
|
)
|
|
|
|
cases := []struct {
|
|
|
|
inAlert *types.Alert
|
2016-07-06 13:18:31 +00:00
|
|
|
inNotifyInfo *types.NotificationInfo
|
2016-01-08 14:15:14 +00:00
|
|
|
result bool
|
|
|
|
}{
|
|
|
|
// A new alert about which there's no previous notification information.
|
|
|
|
{
|
|
|
|
inAlert: &types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"alertname": "a"},
|
|
|
|
StartsAt: now.Add(-10 * time.Minute),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inNotifyInfo: nil,
|
|
|
|
result: true,
|
|
|
|
},
|
|
|
|
// A new alert about which there's no previous notification information.
|
|
|
|
// It is already resolved, so there's no use in sending a notification.
|
|
|
|
{
|
|
|
|
inAlert: &types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"alertname": "a"},
|
|
|
|
StartsAt: now.Add(-10 * time.Minute),
|
|
|
|
EndsAt: now,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inNotifyInfo: nil,
|
|
|
|
result: false,
|
|
|
|
},
|
|
|
|
// An alert that has been firing is now resolved for the first time.
|
|
|
|
{
|
|
|
|
inAlert: &types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"alertname": "a"},
|
|
|
|
StartsAt: now.Add(-10 * time.Minute),
|
|
|
|
EndsAt: now,
|
|
|
|
},
|
|
|
|
},
|
2016-07-06 13:18:31 +00:00
|
|
|
inNotifyInfo: &types.NotificationInfo{
|
2016-01-08 14:15:14 +00:00
|
|
|
Alert: model.LabelSet{"alertname": "a"}.Fingerprint(),
|
|
|
|
Resolved: false,
|
|
|
|
Timestamp: now.Add(-time.Minute),
|
|
|
|
},
|
|
|
|
result: true,
|
|
|
|
},
|
|
|
|
// A resolved alert for which we have already sent a resolved notification.
|
|
|
|
{
|
|
|
|
inAlert: &types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"alertname": "a"},
|
|
|
|
StartsAt: now.Add(-10 * time.Minute),
|
|
|
|
EndsAt: now,
|
|
|
|
},
|
|
|
|
},
|
2016-07-06 13:18:31 +00:00
|
|
|
inNotifyInfo: &types.NotificationInfo{
|
2016-01-08 14:15:14 +00:00
|
|
|
Alert: model.LabelSet{"alertname": "a"}.Fingerprint(),
|
|
|
|
Resolved: true,
|
|
|
|
Timestamp: now.Add(-time.Minute),
|
|
|
|
},
|
|
|
|
result: false,
|
|
|
|
},
|
|
|
|
// An alert that was resolved last time but is now firing again.
|
|
|
|
{
|
|
|
|
inAlert: &types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"alertname": "a"},
|
|
|
|
StartsAt: now.Add(-3 * time.Minute),
|
|
|
|
},
|
|
|
|
},
|
2016-07-06 13:18:31 +00:00
|
|
|
inNotifyInfo: &types.NotificationInfo{
|
2016-01-08 14:15:14 +00:00
|
|
|
Alert: model.LabelSet{"alertname": "a"}.Fingerprint(),
|
|
|
|
Resolved: true,
|
|
|
|
Timestamp: now.Add(-4 * time.Minute),
|
|
|
|
},
|
|
|
|
result: true,
|
|
|
|
},
|
|
|
|
// A firing alert about which we already notified. The last notification
|
|
|
|
// is less than the repeat interval ago.
|
|
|
|
{
|
|
|
|
inAlert: &types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"alertname": "a"},
|
|
|
|
StartsAt: now.Add(-10 * time.Minute),
|
|
|
|
},
|
|
|
|
},
|
2016-07-06 13:18:31 +00:00
|
|
|
inNotifyInfo: &types.NotificationInfo{
|
2016-01-08 14:15:14 +00:00
|
|
|
Alert: model.LabelSet{"alertname": "a"}.Fingerprint(),
|
|
|
|
Resolved: false,
|
|
|
|
Timestamp: now.Add(-15 * time.Minute),
|
|
|
|
},
|
|
|
|
result: false,
|
|
|
|
},
|
|
|
|
// A firing alert about which we already notified. The last notification
|
|
|
|
// is more than the repeat interval ago.
|
|
|
|
{
|
|
|
|
inAlert: &types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"alertname": "a"},
|
|
|
|
StartsAt: now.Add(-10 * time.Minute),
|
|
|
|
},
|
|
|
|
},
|
2016-07-06 13:18:31 +00:00
|
|
|
inNotifyInfo: &types.NotificationInfo{
|
2016-01-08 14:15:14 +00:00
|
|
|
Alert: model.LabelSet{"alertname": "a"}.Fingerprint(),
|
|
|
|
Resolved: false,
|
|
|
|
Timestamp: now.Add(-115 * time.Minute),
|
|
|
|
},
|
|
|
|
result: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range cases {
|
|
|
|
if n.hasUpdate(c.inAlert, c.inNotifyInfo, now, interval) != c.result {
|
|
|
|
t.Errorf("unexpected hasUpdates result for case %d", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-11 13:04:03 +00:00
|
|
|
func TestDedupingNewNotifiesExtraction(t *testing.T) {
|
2015-09-28 19:43:28 +00:00
|
|
|
var (
|
2016-05-29 23:12:05 +00:00
|
|
|
notifies = newTestInfos()
|
2016-08-11 13:04:03 +00:00
|
|
|
deduper = Dedup(notifies)
|
2015-09-28 19:43:28 +00:00
|
|
|
ctx = context.Background()
|
|
|
|
)
|
2015-10-09 06:26:41 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
2015-11-10 12:47:04 +00:00
|
|
|
ctx = WithReceiver(ctx, "name")
|
2015-10-09 06:43:39 +00:00
|
|
|
ctx = WithRepeatInterval(ctx, time.Duration(100*time.Minute))
|
|
|
|
ctx = WithNow(ctx, now)
|
2015-09-28 19:43:28 +00:00
|
|
|
|
|
|
|
alerts := []*types.Alert{
|
2015-10-01 12:55:55 +00:00
|
|
|
{
|
2015-11-06 09:09:39 +00:00
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"alertname": "0"},
|
|
|
|
},
|
|
|
|
}, {
|
2015-10-01 12:55:55 +00:00
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"alertname": "1"},
|
2015-11-06 09:09:39 +00:00
|
|
|
EndsAt: now.Add(-5 * time.Minute),
|
2015-10-01 12:55:55 +00:00
|
|
|
},
|
|
|
|
},
|
2015-09-28 19:43:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 14:15:14 +00:00
|
|
|
// Set an initial NotifyInfo to ensure that on notification failure
|
|
|
|
// nothing changes.
|
2016-07-06 13:18:31 +00:00
|
|
|
nsBefore := []*types.NotificationInfo{
|
2015-11-06 09:09:39 +00:00
|
|
|
nil,
|
2015-09-28 19:43:28 +00:00
|
|
|
{
|
2016-01-08 14:15:14 +00:00
|
|
|
Alert: alerts[1].Fingerprint(),
|
2015-11-10 12:47:04 +00:00
|
|
|
Receiver: "name",
|
2015-09-28 19:43:28 +00:00
|
|
|
Resolved: false,
|
|
|
|
Timestamp: now.Add(-10 * time.Minute),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-10-06 19:11:02 +00:00
|
|
|
if err := notifies.Set(nsBefore...); err != nil {
|
2015-09-28 19:43:28 +00:00
|
|
|
t.Fatalf("Setting notifies failed: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-08-11 13:04:03 +00:00
|
|
|
newNotifies, err := deduper.ExtractNewNotifies(ctx, alerts...)
|
2015-09-28 19:43:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Notify failed: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-07-06 13:18:31 +00:00
|
|
|
nsAfter := []*types.NotificationInfo{
|
2015-09-28 19:43:28 +00:00
|
|
|
{
|
2016-01-08 14:15:14 +00:00
|
|
|
Alert: alerts[0].Fingerprint(),
|
|
|
|
Receiver: "name",
|
|
|
|
Resolved: false,
|
|
|
|
Timestamp: now,
|
2015-09-28 19:43:28 +00:00
|
|
|
},
|
|
|
|
{
|
2016-01-08 14:15:14 +00:00
|
|
|
Alert: alerts[1].Fingerprint(),
|
|
|
|
Receiver: "name",
|
|
|
|
Resolved: true,
|
|
|
|
Timestamp: now,
|
2015-09-28 19:43:28 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-08-11 13:04:03 +00:00
|
|
|
if !reflect.DeepEqual(nsAfter, newNotifies) {
|
|
|
|
t.Errorf("Unexpected notifies, expected: %v, got: %v", nsAfter, newNotifies)
|
2015-09-28 20:02:41 +00:00
|
|
|
}
|
2015-09-28 19:43:28 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 13:22:17 +00:00
|
|
|
func TestSilenceStage(t *testing.T) {
|
2015-09-28 12:13:18 +00:00
|
|
|
// Mute all label sets that have a "mute" key.
|
|
|
|
muter := types.MuteFunc(func(lset model.LabelSet) bool {
|
2015-09-28 16:28:13 +00:00
|
|
|
_, ok := lset["mute"]
|
|
|
|
return ok
|
2015-09-28 12:13:18 +00:00
|
|
|
})
|
|
|
|
|
2015-12-03 16:27:36 +00:00
|
|
|
marker := types.NewMarker()
|
2016-08-12 13:22:17 +00:00
|
|
|
silencer := NewSilenceStage(muter, 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
|
|
|
}
|
|
|
|
|
2015-12-03 16:27:36 +00:00
|
|
|
// Set the second alert als previously silenced. It is expected to have
|
|
|
|
// the WasSilenced flag set to true afterwards.
|
2016-05-29 23:12:05 +00:00
|
|
|
marker.SetSilenced(inAlerts[1].Fingerprint(), uuid.NewV4())
|
2015-12-03 16:27:36 +00:00
|
|
|
|
2016-08-12 13:22:17 +00:00
|
|
|
alerts, err := silencer.Exec(nil, inAlerts...)
|
2016-08-11 13:04:03 +00:00
|
|
|
if err != nil {
|
2015-12-03 16:27:36 +00:00
|
|
|
t.Fatalf("Notifying failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var got []model.LabelSet
|
2016-08-11 13:04:03 +00:00
|
|
|
for i, a := range alerts {
|
2015-12-03 16:27:36 +00:00
|
|
|
got = append(got, a.Labels)
|
|
|
|
if a.WasSilenced != (i == 1) {
|
|
|
|
t.Errorf("Expected WasSilenced to be %v for %d, was %v", i == 1, i, a.WasSilenced)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
marker.SetInhibited(inAlerts[1].Fingerprint(), true)
|
|
|
|
|
2016-08-12 13:22:17 +00:00
|
|
|
alerts, err := inhibitor.Exec(nil, inAlerts...)
|
2016-08-11 13:04:03 +00:00
|
|
|
if err != nil {
|
2015-09-28 12:13:18 +00:00
|
|
|
t.Fatalf("Notifying failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var got []model.LabelSet
|
2016-08-11 13:04:03 +00:00
|
|
|
for i, a := range alerts {
|
2015-09-28 12:13:18 +00:00
|
|
|
got = append(got, a.Labels)
|
2015-12-03 16:27:36 +00:00
|
|
|
if a.WasInhibited != (i == 1) {
|
|
|
|
t.Errorf("Expected WasInhibited to be %v for %d, was %v", i == 1, i, a.WasInhibited)
|
|
|
|
}
|
2015-09-28 12:13:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, out) {
|
|
|
|
t.Fatalf("Muting failed, expected: %v\ngot %v", out, got)
|
|
|
|
}
|
|
|
|
}
|
2016-05-29 22:35:36 +00:00
|
|
|
|
|
|
|
type testInfos struct {
|
|
|
|
mtx sync.RWMutex
|
2016-07-06 13:18:31 +00:00
|
|
|
m map[string]map[model.Fingerprint]*types.NotificationInfo
|
2016-05-29 22:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTestInfos() *testInfos {
|
|
|
|
return &testInfos{
|
2016-07-06 13:18:31 +00:00
|
|
|
m: map[string]map[model.Fingerprint]*types.NotificationInfo{},
|
2016-05-29 22:35:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set implements the Notifies interface.
|
2016-07-06 13:18:31 +00:00
|
|
|
func (n *testInfos) Set(ns ...*types.NotificationInfo) error {
|
2016-05-29 22:35:36 +00:00
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
|
|
|
|
for _, v := range ns {
|
|
|
|
|
|
|
|
if v == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
am, ok := n.m[v.Receiver]
|
|
|
|
if !ok {
|
2016-07-06 13:18:31 +00:00
|
|
|
am = map[model.Fingerprint]*types.NotificationInfo{}
|
2016-05-29 22:35:36 +00:00
|
|
|
n.m[v.Receiver] = am
|
|
|
|
}
|
|
|
|
am[v.Alert] = v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get implements the Notifies interface.
|
2016-07-06 13:18:31 +00:00
|
|
|
func (n *testInfos) Get(dest string, fps ...model.Fingerprint) ([]*types.NotificationInfo, error) {
|
2016-05-29 22:35:36 +00:00
|
|
|
n.mtx.RLock()
|
|
|
|
defer n.mtx.RUnlock()
|
|
|
|
|
2016-07-06 13:18:31 +00:00
|
|
|
res := make([]*types.NotificationInfo, len(fps))
|
2016-05-29 22:35:36 +00:00
|
|
|
|
|
|
|
ns, ok := n.m[dest]
|
|
|
|
if !ok {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, fp := range fps {
|
|
|
|
res[i] = ns[fp]
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|