2020-04-06 11:44:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-10 16:58:17 +00:00
|
|
|
"sync"
|
2020-04-06 11:44:03 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/notifier"
|
2020-04-27 21:18:02 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
2020-04-06 11:44:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRule_Validate(t *testing.T) {
|
|
|
|
if err := (&Rule{}).Validate(); err == nil {
|
|
|
|
t.Errorf("exptected empty name error")
|
|
|
|
}
|
|
|
|
if err := (&Rule{Name: "alert"}).Validate(); err == nil {
|
|
|
|
t.Errorf("exptected empty expr error")
|
|
|
|
}
|
|
|
|
if err := (&Rule{Name: "alert", Expr: "test{"}).Validate(); err == nil {
|
|
|
|
t.Errorf("exptected invalid expr error")
|
|
|
|
}
|
|
|
|
if err := (&Rule{Name: "alert", Expr: "test>0"}).Validate(); err != nil {
|
|
|
|
t.Errorf("exptected valid rule got %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 21:18:02 +00:00
|
|
|
func TestRule_AlertToTimeSeries(t *testing.T) {
|
|
|
|
timestamp := time.Now()
|
|
|
|
testCases := []struct {
|
|
|
|
rule *Rule
|
|
|
|
alert *notifier.Alert
|
|
|
|
expTS []prompbmarshal.TimeSeries
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
newTestRule("instant", 0),
|
|
|
|
¬ifier.Alert{State: notifier.StateFiring},
|
|
|
|
[]prompbmarshal.TimeSeries{
|
|
|
|
newTimeSeries(1, map[string]string{
|
|
|
|
"__name__": alertMetricName,
|
|
|
|
alertStateLabel: notifier.StateFiring.String(),
|
|
|
|
alertNameLabel: "instant",
|
|
|
|
}, timestamp),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("instant extra labels", 0),
|
|
|
|
¬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),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("instant labels override", 0),
|
|
|
|
¬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),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("for", time.Second),
|
|
|
|
¬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),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("for pending", 10*time.Second),
|
|
|
|
¬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) {
|
|
|
|
tss := tc.rule.AlertToTimeSeries(tc.alert, timestamp)
|
|
|
|
if len(tc.expTS) != len(tss) {
|
|
|
|
t.Fatalf("expected number of timeseries %d; got %d", len(tc.expTS), len(tss))
|
|
|
|
}
|
|
|
|
for i := range tc.expTS {
|
|
|
|
expTS, gotTS := tc.expTS[i], tss[i]
|
|
|
|
if len(expTS.Samples) != len(gotTS.Samples) {
|
|
|
|
t.Fatalf("expected number of samples %d; got %d", len(expTS.Samples), len(gotTS.Samples))
|
|
|
|
}
|
|
|
|
for i, exp := range expTS.Samples {
|
|
|
|
got := gotTS.Samples[i]
|
|
|
|
if got.Value != exp.Value {
|
|
|
|
t.Errorf("expected value %.2f; got %.2f", exp.Value, got.Value)
|
|
|
|
}
|
|
|
|
if got.Timestamp != exp.Timestamp {
|
|
|
|
t.Errorf("expected timestamp %d; got %d", exp.Timestamp, got.Timestamp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(expTS.Labels) != len(gotTS.Labels) {
|
|
|
|
t.Fatalf("expected number of labels %d; got %d", len(expTS.Labels), len(gotTS.Labels))
|
|
|
|
}
|
|
|
|
for i, exp := range expTS.Labels {
|
|
|
|
got := gotTS.Labels[i]
|
|
|
|
if got.Name != exp.Name {
|
|
|
|
t.Errorf("expected label name %q; got %q", exp.Name, got.Name)
|
|
|
|
}
|
|
|
|
if got.Value != exp.Value {
|
|
|
|
t.Errorf("expected label value %q; got %q", exp.Value, got.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 11:44:03 +00:00
|
|
|
func newTestRule(name string, waitFor time.Duration) *Rule {
|
|
|
|
return &Rule{Name: name, alerts: make(map[uint64]*notifier.Alert), For: waitFor}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRule_Exec(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
rule *Rule
|
|
|
|
steps [][]datasource.Metric
|
|
|
|
expAlerts map[uint64]*notifier.Alert
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
newTestRule("empty", 0),
|
|
|
|
[][]datasource.Metric{},
|
|
|
|
map[uint64]*notifier.Alert{},
|
|
|
|
},
|
2020-05-04 21:51:22 +00:00
|
|
|
{
|
|
|
|
newTestRule("empty labels", 0),
|
|
|
|
[][]datasource.Metric{
|
|
|
|
{datasource.Metric{}},
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
|
|
|
hash(datasource.Metric{}): {State: notifier.StateFiring},
|
|
|
|
},
|
|
|
|
},
|
2020-04-06 11:44:03 +00:00
|
|
|
{
|
|
|
|
newTestRule("single-firing", 0),
|
|
|
|
[][]datasource.Metric{
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 21:51:22 +00:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateFiring},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("single-firing=>inactive", 0),
|
|
|
|
[][]datasource.Metric{
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
{},
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 21:51:22 +00:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateInactive},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("single-firing=>inactive=>firing", 0),
|
|
|
|
[][]datasource.Metric{
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
{},
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 21:51:22 +00:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateFiring},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("single-firing=>inactive=>firing=>inactive", 0),
|
|
|
|
[][]datasource.Metric{
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
{},
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
{},
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 21:51:22 +00:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateInactive},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("single-firing=>inactive=>firing=>inactive=>empty", 0),
|
|
|
|
[][]datasource.Metric{
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
{},
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("single-firing=>inactive=>firing=>inactive=>empty=>firing", 0),
|
|
|
|
[][]datasource.Metric{
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
{},
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
{},
|
|
|
|
{},
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 21:51:22 +00:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateFiring},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("multiple-firing", 0),
|
|
|
|
[][]datasource.Metric{
|
|
|
|
{
|
2020-05-04 21:51:22 +00:00
|
|
|
metricWithLabels(t, "name", "foo"),
|
|
|
|
metricWithLabels(t, "name", "foo1"),
|
|
|
|
metricWithLabels(t, "name", "foo2"),
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 21:51:22 +00: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 11:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("multiple-steps-firing", 0),
|
|
|
|
[][]datasource.Metric{
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
|
|
|
{metricWithLabels(t, "name", "foo1")},
|
|
|
|
{metricWithLabels(t, "name", "foo2")},
|
2020-04-06 11:44:03 +00: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 21:51:22 +00:00
|
|
|
hash(metricWithLabels(t, "name", "foo1")): {State: notifier.StateInactive},
|
|
|
|
hash(metricWithLabels(t, "name", "foo2")): {State: notifier.StateFiring},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("duplicate", 0),
|
|
|
|
[][]datasource.Metric{
|
|
|
|
{
|
|
|
|
// metrics with the same labelset should result in one alert
|
2020-05-04 21:51:22 +00:00
|
|
|
metricWithLabels(t, "name", "foo", "type", "bar"),
|
|
|
|
metricWithLabels(t, "type", "bar", "name", "foo"),
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 21:51:22 +00:00
|
|
|
hash(metricWithLabels(t, "name", "foo", "type", "bar")): {State: notifier.StateFiring},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("for-pending", time.Minute),
|
|
|
|
[][]datasource.Metric{
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 21:51:22 +00:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StatePending},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("for-fired", time.Millisecond),
|
|
|
|
[][]datasource.Metric{
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 21:51:22 +00:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateFiring},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-05-17 14:13:22 +00:00
|
|
|
newTestRule("for-pending=>empty", time.Second),
|
2020-04-06 11:44:03 +00:00
|
|
|
[][]datasource.Metric{
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-05-17 14:13:22 +00:00
|
|
|
// empty step to reset and delete pending alerts
|
2020-04-06 11:44:03 +00:00
|
|
|
{},
|
|
|
|
},
|
2020-05-17 14:13:22 +00:00
|
|
|
map[uint64]*notifier.Alert{},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("for-pending=>firing=>inactive", time.Millisecond),
|
|
|
|
[][]datasource.Metric{
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
// empty step to reset pending alerts
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 21:51:22 +00:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateInactive},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("for-pending=>firing=>inactive=>pending", time.Millisecond),
|
|
|
|
[][]datasource.Metric{
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
// empty step to reset pending alerts
|
|
|
|
{},
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 21:51:22 +00:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StatePending},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
newTestRule("for-pending=>firing=>inactive=>pending=>firing", time.Millisecond),
|
|
|
|
[][]datasource.Metric{
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
// empty step to reset pending alerts
|
|
|
|
{},
|
2020-05-04 21:51:22 +00:00
|
|
|
{metricWithLabels(t, "name", "foo")},
|
|
|
|
{metricWithLabels(t, "name", "foo")},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
map[uint64]*notifier.Alert{
|
2020-05-04 21:51:22 +00:00
|
|
|
hash(metricWithLabels(t, "name", "foo")): {State: notifier.StateFiring},
|
2020-04-06 11:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-05-04 21:51:22 +00:00
|
|
|
fakeGroup := Group{Name: "TestRule_Exec"}
|
2020-04-06 11:44:03 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.rule.Name, func(t *testing.T) {
|
|
|
|
fq := &fakeQuerier{}
|
|
|
|
tc.rule.group = fakeGroup
|
|
|
|
for _, step := range tc.steps {
|
|
|
|
fq.reset()
|
2020-05-04 21:51:22 +00:00
|
|
|
fq.add(step...)
|
2020-04-06 11:44:03 +00:00
|
|
|
if err := tc.rule.Exec(context.TODO(), fq); err != nil {
|
|
|
|
t.Fatalf("unexpected err: %s", err)
|
|
|
|
}
|
|
|
|
// artificial delay between applying steps
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func metricWithLabels(t *testing.T, labels ...string) datasource.Metric {
|
|
|
|
t.Helper()
|
|
|
|
if len(labels) == 0 || len(labels)%2 != 0 {
|
|
|
|
t.Fatalf("expected to get even number of labels")
|
|
|
|
}
|
|
|
|
m := datasource.Metric{}
|
|
|
|
for i := 0; i < len(labels); i += 2 {
|
|
|
|
m.Labels = append(m.Labels, datasource.Label{
|
|
|
|
Name: labels[i],
|
|
|
|
Value: labels[i+1],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeQuerier struct {
|
2020-05-10 16:58:17 +00:00
|
|
|
sync.Mutex
|
2020-04-06 11:44:03 +00:00
|
|
|
metrics []datasource.Metric
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fq *fakeQuerier) reset() {
|
2020-05-10 16:58:17 +00:00
|
|
|
fq.Lock()
|
2020-04-06 11:44:03 +00:00
|
|
|
fq.metrics = fq.metrics[:0]
|
2020-05-10 16:58:17 +00:00
|
|
|
fq.Unlock()
|
2020-04-06 11:44:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 21:51:22 +00:00
|
|
|
func (fq *fakeQuerier) add(metrics ...datasource.Metric) {
|
2020-05-10 16:58:17 +00:00
|
|
|
fq.Lock()
|
2020-04-06 11:44:03 +00:00
|
|
|
fq.metrics = append(fq.metrics, metrics...)
|
2020-05-10 16:58:17 +00:00
|
|
|
fq.Unlock()
|
2020-04-06 11:44:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 16:58:17 +00:00
|
|
|
func (fq *fakeQuerier) Query(_ context.Context, _ string) ([]datasource.Metric, error) {
|
|
|
|
fq.Lock()
|
|
|
|
cpy := make([]datasource.Metric, len(fq.metrics))
|
|
|
|
copy(cpy, fq.metrics)
|
|
|
|
fq.Unlock()
|
|
|
|
return cpy, nil
|
2020-04-06 11:44:03 +00:00
|
|
|
}
|
2020-05-04 21:51:22 +00:00
|
|
|
|
|
|
|
func TestRule_Restore(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
rule *Rule
|
|
|
|
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, "",
|
|
|
|
"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{}
|
|
|
|
tc.rule.group = fakeGroup
|
|
|
|
fq.add(tc.metrics...)
|
|
|
|
if err := tc.rule.Restore(context.TODO(), fq, time.Hour); err != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestRuleWithLabels(name string, labels ...string) *Rule {
|
|
|
|
r := newTestRule(name, 0)
|
|
|
|
r.Labels = make(map[string]string)
|
|
|
|
for i := 0; i < len(labels); i += 2 {
|
|
|
|
r.Labels[labels[i]] = labels[i+1]
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func metricWithValueAndLabels(t *testing.T, value float64, labels ...string) datasource.Metric {
|
|
|
|
t.Helper()
|
|
|
|
m := metricWithLabels(t, labels...)
|
|
|
|
m.Value = value
|
|
|
|
return m
|
|
|
|
}
|