2020-04-06 13:44:03 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/notifier"
|
2020-04-27 23:18:02 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
2020-04-06 13:44:03 +02:00
|
|
|
)
|
|
|
|
|
2020-06-01 12:46:37 +02:00
|
|
|
func TestAlertingRule_ToTimeSeries(t *testing.T) {
|
2020-04-27 23:18:02 +02:00
|
|
|
timestamp := time.Now()
|
|
|
|
testCases := []struct {
|
2020-06-01 12:46:37 +02:00
|
|
|
rule *AlertingRule
|
2020-04-27 23:18:02 +02:00
|
|
|
alert *notifier.Alert
|
|
|
|
expTS []prompbmarshal.TimeSeries
|
|
|
|
}{
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("instant", 0),
|
2020-04-27 23:18:02 +02:00
|
|
|
¬ifier.Alert{State: notifier.StateFiring},
|
|
|
|
[]prompbmarshal.TimeSeries{
|
|
|
|
newTimeSeries(1, map[string]string{
|
|
|
|
"__name__": alertMetricName,
|
|
|
|
alertStateLabel: notifier.StateFiring.String(),
|
|
|
|
alertNameLabel: "instant",
|
|
|
|
}, timestamp),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("instant extra labels", 0),
|
2020-04-27 23:18:02 +02:00
|
|
|
¬ifier.Alert{State: notifier.StateFiring, Labels: map[string]string{
|
|
|
|
"job": "foo",
|
|
|
|
"instance": "bar",
|
|
|
|
}},
|
|
|
|
[]prompbmarshal.TimeSeries{
|
|
|
|
newTimeSeries(1, map[string]string{
|
|
|
|
"__name__": alertMetricName,
|
|
|
|
alertStateLabel: notifier.StateFiring.String(),
|
|
|
|
alertNameLabel: "instant extra labels",
|
|
|
|
"job": "foo",
|
|
|
|
"instance": "bar",
|
|
|
|
}, timestamp),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("instant labels override", 0),
|
2020-04-27 23:18:02 +02:00
|
|
|
¬ifier.Alert{State: notifier.StateFiring, Labels: map[string]string{
|
|
|
|
alertStateLabel: "foo",
|
|
|
|
"__name__": "bar",
|
|
|
|
}},
|
|
|
|
[]prompbmarshal.TimeSeries{
|
|
|
|
newTimeSeries(1, map[string]string{
|
|
|
|
"__name__": alertMetricName,
|
|
|
|
alertStateLabel: notifier.StateFiring.String(),
|
|
|
|
alertNameLabel: "instant labels override",
|
|
|
|
}, timestamp),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("for", time.Second),
|
2020-04-27 23:18:02 +02:00
|
|
|
¬ifier.Alert{State: notifier.StateFiring, Start: timestamp.Add(time.Second)},
|
|
|
|
[]prompbmarshal.TimeSeries{
|
|
|
|
newTimeSeries(1, map[string]string{
|
|
|
|
"__name__": alertMetricName,
|
|
|
|
alertStateLabel: notifier.StateFiring.String(),
|
|
|
|
alertNameLabel: "for",
|
|
|
|
}, timestamp),
|
|
|
|
newTimeSeries(float64(timestamp.Add(time.Second).Unix()), map[string]string{
|
|
|
|
"__name__": alertForStateMetricName,
|
|
|
|
alertNameLabel: "for",
|
|
|
|
}, timestamp),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("for pending", 10*time.Second),
|
2020-04-27 23:18:02 +02:00
|
|
|
¬ifier.Alert{State: notifier.StatePending, Start: timestamp.Add(time.Second)},
|
|
|
|
[]prompbmarshal.TimeSeries{
|
|
|
|
newTimeSeries(1, map[string]string{
|
|
|
|
"__name__": alertMetricName,
|
|
|
|
alertStateLabel: notifier.StatePending.String(),
|
|
|
|
alertNameLabel: "for pending",
|
|
|
|
}, timestamp),
|
|
|
|
newTimeSeries(float64(timestamp.Add(time.Second).Unix()), map[string]string{
|
|
|
|
"__name__": alertForStateMetricName,
|
|
|
|
alertNameLabel: "for pending",
|
|
|
|
}, timestamp),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.rule.Name, func(t *testing.T) {
|
2020-06-01 12:46:37 +02:00
|
|
|
tc.rule.alerts[tc.alert.ID] = tc.alert
|
|
|
|
tss := tc.rule.toTimeSeries(timestamp)
|
|
|
|
if err := compareTimeSeries(t, tc.expTS, tss); err != nil {
|
|
|
|
t.Fatalf("timeseries missmatch: %s", err)
|
2020-04-27 23:18:02 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:46:37 +02:00
|
|
|
func TestAlertingRule_Exec(t *testing.T) {
|
2020-06-09 14:21:20 +02:00
|
|
|
const defaultStep = 5 * time.Millisecond
|
2020-04-06 13:44:03 +02:00
|
|
|
testCases := []struct {
|
2020-06-01 12:46:37 +02:00
|
|
|
rule *AlertingRule
|
2020-04-06 13:44:03 +02:00
|
|
|
steps [][]datasource.Metric
|
|
|
|
expAlerts map[uint64]*notifier.Alert
|
|
|
|
}{
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("empty", 0),
|
2020-04-06 13:44:03 +02:00
|
|
|
[][]datasource.Metric{},
|
|
|
|
map[uint64]*notifier.Alert{},
|
|
|
|
},
|
2020-05-04 23:51:22 +02:00
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("empty labels", 0),
|
2020-05-04 23:51:22 +02:00
|
|
|
[][]datasource.Metric{
|
|
|
|
{datasource.Metric{}},
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
|
|
|
hash(datasource.Metric{}): {State: notifier.StateFiring},
|
|
|
|
},
|
|
|
|
},
|
2020-04-06 13:44:03 +02:00
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("single-firing", 0),
|
2020-04-06 13:44:03 +02:00
|
|
|
[][]datasource.Metric{
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 23:51:22 +02:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateFiring},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("single-firing=>inactive", 0),
|
2020-04-06 13:44:03 +02:00
|
|
|
[][]datasource.Metric{
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
{},
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 23:51:22 +02:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateInactive},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("single-firing=>inactive=>firing", 0),
|
2020-04-06 13:44:03 +02:00
|
|
|
[][]datasource.Metric{
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
{},
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 23:51:22 +02:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateFiring},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("single-firing=>inactive=>firing=>inactive", 0),
|
2020-04-06 13:44:03 +02:00
|
|
|
[][]datasource.Metric{
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
{},
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
{},
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 23:51:22 +02:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateInactive},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("single-firing=>inactive=>firing=>inactive=>empty", 0),
|
2020-04-06 13:44:03 +02:00
|
|
|
[][]datasource.Metric{
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
{},
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{},
|
|
|
|
},
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("single-firing=>inactive=>firing=>inactive=>empty=>firing", 0),
|
2020-04-06 13:44:03 +02:00
|
|
|
[][]datasource.Metric{
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
{},
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
{},
|
|
|
|
{},
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 23:51:22 +02:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateFiring},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("multiple-firing", 0),
|
2020-04-06 13:44:03 +02:00
|
|
|
[][]datasource.Metric{
|
|
|
|
{
|
2020-05-04 23:51:22 +02:00
|
|
|
metricWithLabels(t, "name", "foo"),
|
|
|
|
metricWithLabels(t, "name", "foo1"),
|
|
|
|
metricWithLabels(t, "name", "foo2"),
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 23:51:22 +02:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateFiring},
|
|
|
|
hash(metricWithLabels(t, "name", "foo1")): {State: notifier.StateFiring},
|
|
|
|
hash(metricWithLabels(t, "name", "foo2")): {State: notifier.StateFiring},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("multiple-steps-firing", 0),
|
2020-04-06 13:44:03 +02:00
|
|
|
[][]datasource.Metric{
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
|
|
|
{metricWithLabels(t, "name", "foo1")},
|
|
|
|
{metricWithLabels(t, "name", "foo2")},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
// 1: fire first alert
|
|
|
|
// 2: fire second alert, set first inactive
|
|
|
|
// 3: fire third alert, set second inactive, delete first one
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 23:51:22 +02:00
|
|
|
hash(metricWithLabels(t, "name", "foo1")): {State: notifier.StateInactive},
|
|
|
|
hash(metricWithLabels(t, "name", "foo2")): {State: notifier.StateFiring},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("duplicate", 0),
|
2020-04-06 13:44:03 +02:00
|
|
|
[][]datasource.Metric{
|
|
|
|
{
|
|
|
|
// metrics with the same labelset should result in one alert
|
2020-05-04 23:51:22 +02:00
|
|
|
metricWithLabels(t, "name", "foo", "type", "bar"),
|
|
|
|
metricWithLabels(t, "type", "bar", "name", "foo"),
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 23:51:22 +02:00
|
|
|
hash(metricWithLabels(t, "name", "foo", "type", "bar")): {State: notifier.StateFiring},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("for-pending", time.Minute),
|
2020-04-06 13:44:03 +02:00
|
|
|
[][]datasource.Metric{
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 23:51:22 +02:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StatePending},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-09 14:21:20 +02:00
|
|
|
newTestAlertingRule("for-fired", defaultStep),
|
2020-04-06 13:44:03 +02:00
|
|
|
[][]datasource.Metric{
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 23:51:22 +02:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateFiring},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
newTestAlertingRule("for-pending=>empty", time.Second),
|
2020-04-06 13:44:03 +02:00
|
|
|
[][]datasource.Metric{
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-05-17 16:13:22 +02:00
|
|
|
// empty step to reset and delete pending alerts
|
2020-04-06 13:44:03 +02:00
|
|
|
{},
|
|
|
|
},
|
2020-05-17 16:13:22 +02:00
|
|
|
map[uint64]*notifier.Alert{},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
{
|
2020-06-09 14:21:20 +02:00
|
|
|
newTestAlertingRule("for-pending=>firing=>inactive", defaultStep),
|
2020-04-06 13:44:03 +02:00
|
|
|
[][]datasource.Metric{
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
// empty step to reset pending alerts
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 23:51:22 +02:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateInactive},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-09 14:21:20 +02:00
|
|
|
newTestAlertingRule("for-pending=>firing=>inactive=>pending", defaultStep),
|
2020-04-06 13:44:03 +02:00
|
|
|
[][]datasource.Metric{
|
2020-06-09 14:21:20 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
// empty step to reset pending alerts
|
|
|
|
{},
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 23:51:22 +02:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StatePending},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-09 14:21:20 +02:00
|
|
|
newTestAlertingRule("for-pending=>firing=>inactive=>pending=>firing", defaultStep),
|
2020-04-06 13:44:03 +02:00
|
|
|
[][]datasource.Metric{
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
// empty step to reset pending alerts
|
|
|
|
{},
|
2020-05-04 23:51:22 +02:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 23:51:22 +02:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateFiring},
|
2020-04-06 13:44:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-05-04 23:51:22 +02:00
|
|
|
fakeGroup := Group{Name: "TestRule_Exec"}
|
2020-04-06 13:44:03 +02:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.rule.Name, func(t *testing.T) {
|
|
|
|
fq := &fakeQuerier{}
|
2020-06-01 12:46:37 +02:00
|
|
|
tc.rule.GroupID = fakeGroup.ID()
|
2020-04-06 13:44:03 +02:00
|
|
|
for _, step := range tc.steps {
|
|
|
|
fq.reset()
|
2020-05-04 23:51:22 +02:00
|
|
|
fq.add(step...)
|
2020-06-01 12:46:37 +02:00
|
|
|
if _, err := tc.rule.Exec(context.TODO(), fq, false); err != nil {
|
2020-04-06 13:44:03 +02:00
|
|
|
t.Fatalf("unexpected err: %s", err)
|
|
|
|
}
|
|
|
|
// artificial delay between applying steps
|
2020-06-09 14:21:20 +02:00
|
|
|
time.Sleep(defaultStep)
|
2020-04-06 13:44:03 +02:00
|
|
|
}
|
|
|
|
if len(tc.rule.alerts) != len(tc.expAlerts) {
|
|
|
|
t.Fatalf("expected %d alerts; got %d", len(tc.expAlerts), len(tc.rule.alerts))
|
|
|
|
}
|
|
|
|
for key, exp := range tc.expAlerts {
|
|
|
|
got, ok := tc.rule.alerts[key]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected to have key %d", key)
|
|
|
|
}
|
|
|
|
if got.State != exp.State {
|
|
|
|
t.Fatalf("expected state %d; got %d", exp.State, got.State)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:46:37 +02:00
|
|
|
func TestAlertingRule_Restore(t *testing.T) {
|
2020-05-04 23:51:22 +02:00
|
|
|
testCases := []struct {
|
2020-06-01 12:46:37 +02:00
|
|
|
rule *AlertingRule
|
2020-05-04 23:51:22 +02:00
|
|
|
metrics []datasource.Metric
|
|
|
|
expAlerts map[uint64]*notifier.Alert
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
newTestRuleWithLabels("no extra labels"),
|
|
|
|
[]datasource.Metric{
|
|
|
|
metricWithValueAndLabels(t, float64(time.Now().Truncate(time.Hour).Unix()),
|
|
|
|
"__name__", alertForStateMetricName,
|
|
|
|
alertNameLabel, "",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
|
|
|
hash(datasource.Metric{}): {State: notifier.StatePending,
|
|
|
|
Start: time.Now().Truncate(time.Hour)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRuleWithLabels("metric labels"),
|
|
|
|
[]datasource.Metric{
|
|
|
|
metricWithValueAndLabels(t, float64(time.Now().Truncate(time.Hour).Unix()),
|
|
|
|
"__name__", alertForStateMetricName,
|
|
|
|
alertNameLabel, "",
|
2020-10-30 09:18:20 +01:00
|
|
|
alertGroupNameLabel, "groupID",
|
2020-05-04 23:51:22 +02:00
|
|
|
"foo", "bar",
|
|
|
|
"namespace", "baz",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
|
|
|
hash(metricWithLabels(t,
|
|
|
|
"foo", "bar",
|
|
|
|
"namespace", "baz",
|
|
|
|
)): {State: notifier.StatePending,
|
|
|
|
Start: time.Now().Truncate(time.Hour)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRuleWithLabels("rule labels", "source", "vm"),
|
|
|
|
[]datasource.Metric{
|
|
|
|
metricWithValueAndLabels(t, float64(time.Now().Truncate(time.Hour).Unix()),
|
|
|
|
"__name__", alertForStateMetricName,
|
|
|
|
alertNameLabel, "",
|
|
|
|
"foo", "bar",
|
|
|
|
"namespace", "baz",
|
|
|
|
// following pair supposed to be dropped
|
|
|
|
"source", "vm",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
|
|
|
hash(metricWithLabels(t,
|
|
|
|
"foo", "bar",
|
|
|
|
"namespace", "baz",
|
|
|
|
)): {State: notifier.StatePending,
|
|
|
|
Start: time.Now().Truncate(time.Hour)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRuleWithLabels("multiple alerts"),
|
|
|
|
[]datasource.Metric{
|
|
|
|
metricWithValueAndLabels(t, float64(time.Now().Truncate(time.Hour).Unix()),
|
|
|
|
"__name__", alertForStateMetricName,
|
|
|
|
"host", "localhost-1",
|
|
|
|
),
|
|
|
|
metricWithValueAndLabels(t, float64(time.Now().Truncate(2*time.Hour).Unix()),
|
|
|
|
"__name__", alertForStateMetricName,
|
|
|
|
"host", "localhost-2",
|
|
|
|
),
|
|
|
|
metricWithValueAndLabels(t, float64(time.Now().Truncate(3*time.Hour).Unix()),
|
|
|
|
"__name__", alertForStateMetricName,
|
|
|
|
"host", "localhost-3",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
|
|
|
hash(metricWithLabels(t, "host", "localhost-1")): {State: notifier.StatePending,
|
|
|
|
Start: time.Now().Truncate(time.Hour)},
|
|
|
|
hash(metricWithLabels(t, "host", "localhost-2")): {State: notifier.StatePending,
|
|
|
|
Start: time.Now().Truncate(2 * time.Hour)},
|
|
|
|
hash(metricWithLabels(t, "host", "localhost-3")): {State: notifier.StatePending,
|
|
|
|
Start: time.Now().Truncate(3 * time.Hour)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fakeGroup := Group{Name: "TestRule_Exec"}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.rule.Name, func(t *testing.T) {
|
|
|
|
fq := &fakeQuerier{}
|
2020-06-01 12:46:37 +02:00
|
|
|
tc.rule.GroupID = fakeGroup.ID()
|
2020-05-04 23:51:22 +02:00
|
|
|
fq.add(tc.metrics...)
|
2020-07-28 13:20:31 +02:00
|
|
|
if err := tc.rule.Restore(context.TODO(), fq, time.Hour, nil); err != nil {
|
2020-05-04 23:51:22 +02:00
|
|
|
t.Fatalf("unexpected err: %s", err)
|
|
|
|
}
|
|
|
|
if len(tc.rule.alerts) != len(tc.expAlerts) {
|
|
|
|
t.Fatalf("expected %d alerts; got %d", len(tc.expAlerts), len(tc.rule.alerts))
|
|
|
|
}
|
|
|
|
for key, exp := range tc.expAlerts {
|
|
|
|
got, ok := tc.rule.alerts[key]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected to have key %d", key)
|
|
|
|
}
|
|
|
|
if got.State != exp.State {
|
|
|
|
t.Fatalf("expected state %d; got %d", exp.State, got.State)
|
|
|
|
}
|
|
|
|
if got.Start != exp.Start {
|
|
|
|
t.Fatalf("expected Start %v; got %v", exp.Start, got.Start)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:46:37 +02:00
|
|
|
func newTestRuleWithLabels(name string, labels ...string) *AlertingRule {
|
|
|
|
r := newTestAlertingRule(name, 0)
|
2020-05-04 23:51:22 +02:00
|
|
|
r.Labels = make(map[string]string)
|
|
|
|
for i := 0; i < len(labels); i += 2 {
|
|
|
|
r.Labels[labels[i]] = labels[i+1]
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:46:37 +02:00
|
|
|
func newTestAlertingRule(name string, waitFor time.Duration) *AlertingRule {
|
|
|
|
return &AlertingRule{Name: name, alerts: make(map[uint64]*notifier.Alert), For: waitFor}
|
2020-05-04 23:51:22 +02:00
|
|
|
}
|