2015-01-21 19:07:45 +00:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-02-07 10:49:04 +00:00
|
|
|
// 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.
|
|
|
|
|
2013-01-07 22:24:26 +00:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
2017-05-18 16:47:00 +00:00
|
|
|
"context"
|
2013-01-07 22:24:26 +00:00
|
|
|
"fmt"
|
2017-05-18 16:47:00 +00:00
|
|
|
"math"
|
|
|
|
"reflect"
|
2013-01-07 22:24:26 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2013-03-21 17:06:15 +00:00
|
|
|
"time"
|
2013-06-25 12:02:27 +00:00
|
|
|
|
2017-08-11 18:45:52 +00:00
|
|
|
"github.com/go-kit/kit/log"
|
2015-08-20 15:18:46 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2013-06-25 12:02:27 +00:00
|
|
|
|
2016-12-29 16:31:14 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
|
|
|
"github.com/prometheus/prometheus/pkg/timestamp"
|
2017-05-18 16:47:00 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/value"
|
2015-03-30 17:43:19 +00:00
|
|
|
"github.com/prometheus/prometheus/promql"
|
2017-05-18 16:47:00 +00:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2013-01-07 22:24:26 +00:00
|
|
|
)
|
|
|
|
|
2015-06-30 09:51:05 +00:00
|
|
|
func TestAlertingRule(t *testing.T) {
|
|
|
|
suite, err := promql.NewTest(t, `
|
|
|
|
load 5m
|
2016-07-12 16:11:31 +00:00
|
|
|
http_requests{job="app-server", instance="0", group="canary", severity="overwrite-me"} 75 85 95 105 105 95 85
|
|
|
|
http_requests{job="app-server", instance="1", group="canary", severity="overwrite-me"} 80 90 100 110 120 130 140
|
2015-06-30 09:51:05 +00:00
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2015-03-30 17:43:19 +00:00
|
|
|
}
|
2015-06-30 09:51:05 +00:00
|
|
|
defer suite.Close()
|
2015-03-30 17:43:19 +00:00
|
|
|
|
2015-06-30 09:51:05 +00:00
|
|
|
if err := suite.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-03-30 17:43:19 +00:00
|
|
|
}
|
2013-01-07 22:24:26 +00:00
|
|
|
|
2015-06-30 09:51:05 +00:00
|
|
|
expr, err := promql.ParseExpr(`http_requests{group="canary", job="app-server"} < 100`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to parse alert expression: %s", err)
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
2013-04-22 22:26:59 +00:00
|
|
|
|
2015-06-30 09:51:05 +00:00
|
|
|
rule := NewAlertingRule(
|
|
|
|
"HTTPRequestRateLow",
|
|
|
|
expr,
|
|
|
|
time.Minute,
|
2016-12-29 16:31:14 +00:00
|
|
|
labels.FromStrings("severity", "{{\"c\"}}ritical"),
|
2017-08-11 18:45:52 +00:00
|
|
|
nil, nil,
|
2015-06-30 09:51:05 +00:00
|
|
|
)
|
|
|
|
|
2016-12-29 16:31:14 +00:00
|
|
|
baseTime := time.Unix(0, 0)
|
|
|
|
|
2015-06-30 09:51:05 +00:00
|
|
|
var tests = []struct {
|
|
|
|
time time.Duration
|
|
|
|
result []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
time: 0,
|
|
|
|
result: []string{
|
2016-12-29 16:31:14 +00:00
|
|
|
`{__name__="ALERTS", alertname="HTTPRequestRateLow", alertstate="pending", group="canary", instance="0", job="app-server", severity="critical"} => 1 @[%v]`,
|
|
|
|
`{__name__="ALERTS", alertname="HTTPRequestRateLow", alertstate="pending", group="canary", instance="1", job="app-server", severity="critical"} => 1 @[%v]`,
|
2015-02-21 16:45:47 +00:00
|
|
|
},
|
2015-06-30 09:51:05 +00:00
|
|
|
}, {
|
|
|
|
time: 5 * time.Minute,
|
|
|
|
result: []string{
|
2016-12-29 16:31:14 +00:00
|
|
|
`{__name__="ALERTS", alertname="HTTPRequestRateLow", alertstate="firing", group="canary", instance="0", job="app-server", severity="critical"} => 1 @[%v]`,
|
|
|
|
`{__name__="ALERTS", alertname="HTTPRequestRateLow", alertstate="firing", group="canary", instance="1", job="app-server", severity="critical"} => 1 @[%v]`,
|
2015-03-30 17:43:19 +00:00
|
|
|
},
|
2015-06-30 09:51:05 +00:00
|
|
|
}, {
|
|
|
|
time: 10 * time.Minute,
|
|
|
|
result: []string{
|
2016-12-29 16:31:14 +00:00
|
|
|
`{__name__="ALERTS", alertname="HTTPRequestRateLow", alertstate="firing", group="canary", instance="0", job="app-server", severity="critical"} => 1 @[%v]`,
|
2015-03-30 17:43:19 +00:00
|
|
|
},
|
|
|
|
},
|
2013-04-24 09:51:40 +00:00
|
|
|
{
|
2017-05-19 16:02:25 +00:00
|
|
|
time: 15 * time.Minute,
|
|
|
|
result: []string{},
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
{
|
2015-06-30 09:51:05 +00:00
|
|
|
time: 20 * time.Minute,
|
2016-02-05 04:42:55 +00:00
|
|
|
result: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
time: 25 * time.Minute,
|
|
|
|
result: []string{
|
2016-12-29 16:31:14 +00:00
|
|
|
`{__name__="ALERTS", alertname="HTTPRequestRateLow", alertstate="pending", group="canary", instance="0", job="app-server", severity="critical"} => 1 @[%v]`,
|
2016-02-05 04:42:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
time: 30 * time.Minute,
|
|
|
|
result: []string{
|
2016-12-29 16:31:14 +00:00
|
|
|
`{__name__="ALERTS", alertname="HTTPRequestRateLow", alertstate="firing", group="canary", instance="0", job="app-server", severity="critical"} => 1 @[%v]`,
|
2016-02-05 04:42:55 +00:00
|
|
|
},
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
}
|
2015-03-30 17:43:19 +00:00
|
|
|
|
2015-06-30 09:51:05 +00:00
|
|
|
for i, test := range tests {
|
2016-12-29 16:31:14 +00:00
|
|
|
evalTime := baseTime.Add(test.time)
|
2015-03-30 17:43:19 +00:00
|
|
|
|
2017-05-13 13:47:04 +00:00
|
|
|
res, err := rule.Eval(suite.Context(), evalTime, suite.QueryEngine(), nil)
|
2013-04-24 09:51:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error during alerting rule evaluation: %s", err)
|
|
|
|
}
|
2015-03-30 17:43:19 +00:00
|
|
|
|
2015-06-30 09:51:05 +00:00
|
|
|
actual := strings.Split(res.String(), "\n")
|
|
|
|
expected := annotateWithTime(test.result, evalTime)
|
|
|
|
if actual[0] == "" {
|
|
|
|
actual = []string{}
|
2013-04-24 09:51:40 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 09:51:05 +00:00
|
|
|
if len(actual) != len(expected) {
|
|
|
|
t.Errorf("%d. Number of samples in expected and actual output don't match (%d vs. %d)", i, len(expected), len(actual))
|
2013-04-24 09:51:40 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 09:51:05 +00:00
|
|
|
for j, expectedSample := range expected {
|
2013-04-24 09:51:40 +00:00
|
|
|
found := false
|
2015-06-30 09:51:05 +00:00
|
|
|
for _, actualSample := range actual {
|
2013-04-24 09:51:40 +00:00
|
|
|
if actualSample == expectedSample {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Errorf("%d.%d. Couldn't find expected sample in output: '%v'", i, j, expectedSample)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 09:51:05 +00:00
|
|
|
if t.Failed() {
|
|
|
|
t.Errorf("%d. Expected and actual outputs don't match:", i)
|
|
|
|
t.Fatalf("Expected:\n%v\n----\nActual:\n%v", strings.Join(expected, "\n"), strings.Join(actual, "\n"))
|
2013-04-24 09:51:40 +00:00
|
|
|
}
|
2016-08-01 22:32:01 +00:00
|
|
|
|
|
|
|
for _, aa := range rule.ActiveAlerts() {
|
2016-12-29 16:31:14 +00:00
|
|
|
if v := aa.Labels.Get(model.MetricNameLabel); v != "" {
|
2016-08-01 22:32:01 +00:00
|
|
|
t.Fatalf("%s label set on active alert: %s", model.MetricNameLabel, aa.Labels)
|
|
|
|
}
|
|
|
|
}
|
2013-04-24 09:51:40 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-30 09:51:05 +00:00
|
|
|
|
2016-12-29 16:31:14 +00:00
|
|
|
func annotateWithTime(lines []string, ts time.Time) []string {
|
2015-06-30 09:51:05 +00:00
|
|
|
annotatedLines := []string{}
|
|
|
|
for _, line := range lines {
|
2016-12-29 16:31:14 +00:00
|
|
|
annotatedLines = append(annotatedLines, fmt.Sprintf(line, timestamp.FromTime(ts)))
|
2015-06-30 09:51:05 +00:00
|
|
|
}
|
|
|
|
return annotatedLines
|
|
|
|
}
|
2017-05-18 16:47:00 +00:00
|
|
|
|
|
|
|
func TestStaleness(t *testing.T) {
|
|
|
|
storage := testutil.NewStorage(t)
|
|
|
|
defer storage.Close()
|
|
|
|
engine := promql.NewEngine(storage, nil)
|
|
|
|
opts := &ManagerOptions{
|
|
|
|
QueryEngine: engine,
|
|
|
|
Appendable: storage,
|
|
|
|
Context: context.Background(),
|
2017-08-11 18:45:52 +00:00
|
|
|
Logger: log.NewNopLogger(),
|
2017-05-18 16:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr, err := promql.ParseExpr("a + 1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
rule := NewRecordingRule("a_plus_one", expr, labels.Labels{})
|
2017-06-14 09:48:39 +00:00
|
|
|
group := NewGroup("default", "", time.Second, []Rule{rule}, opts)
|
2017-05-18 16:47:00 +00:00
|
|
|
|
|
|
|
// A time series that has two samples and then goes stale.
|
|
|
|
app, _ := storage.Appender()
|
|
|
|
app.Add(labels.FromStrings(model.MetricNameLabel, "a"), 0, 1)
|
|
|
|
app.Add(labels.FromStrings(model.MetricNameLabel, "a"), 1000, 2)
|
|
|
|
app.Add(labels.FromStrings(model.MetricNameLabel, "a"), 2000, math.Float64frombits(value.StaleNaN))
|
|
|
|
if err = app.Commit(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute 3 times, 1 second apart.
|
|
|
|
group.Eval(time.Unix(0, 0))
|
|
|
|
group.Eval(time.Unix(1, 0))
|
|
|
|
group.Eval(time.Unix(2, 0))
|
|
|
|
|
2017-10-04 19:04:15 +00:00
|
|
|
querier, err := storage.Querier(context.Background(), 0, 2000)
|
2017-05-18 16:47:00 +00:00
|
|
|
defer querier.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
matcher, _ := labels.NewMatcher(labels.MatchEqual, model.MetricNameLabel, "a_plus_one")
|
2017-05-19 15:43:59 +00:00
|
|
|
samples, err := readSeriesSet(querier.Select(matcher))
|
2017-05-18 16:47:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
metric := labels.FromStrings(model.MetricNameLabel, "a_plus_one").String()
|
|
|
|
metricSample, ok := samples[metric]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Series %s not returned.", metric)
|
|
|
|
}
|
|
|
|
if !value.IsStaleNaN(metricSample[2].V) {
|
|
|
|
t.Fatalf("Appended second sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(metricSample[2].V))
|
|
|
|
}
|
|
|
|
metricSample[2].V = 42 // reflect.DeepEqual cannot handle NaN.
|
|
|
|
|
|
|
|
want := map[string][]promql.Point{
|
|
|
|
metric: []promql.Point{{0, 2}, {1000, 3}, {2000, 42}},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(want, samples) {
|
|
|
|
t.Fatalf("Returned samples not as expected. Wanted: %+v Got: %+v", want, samples)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert a SeriesSet into a form useable with reflect.DeepEqual.
|
|
|
|
func readSeriesSet(ss storage.SeriesSet) (map[string][]promql.Point, error) {
|
|
|
|
result := map[string][]promql.Point{}
|
|
|
|
|
|
|
|
for ss.Next() {
|
|
|
|
series := ss.At()
|
|
|
|
|
|
|
|
points := []promql.Point{}
|
|
|
|
it := series.Iterator()
|
|
|
|
for it.Next() {
|
|
|
|
t, v := it.At()
|
|
|
|
points = append(points, promql.Point{T: t, V: v})
|
|
|
|
}
|
|
|
|
|
|
|
|
name := series.Labels().String()
|
|
|
|
result[name] = points
|
|
|
|
}
|
2017-05-19 15:43:59 +00:00
|
|
|
return result, ss.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopyState(t *testing.T) {
|
|
|
|
oldGroup := &Group{
|
|
|
|
rules: []Rule{
|
2017-08-11 18:45:52 +00:00
|
|
|
NewAlertingRule("alert", nil, 0, nil, nil, nil),
|
2017-05-19 15:43:59 +00:00
|
|
|
NewRecordingRule("rule1", nil, nil),
|
|
|
|
NewRecordingRule("rule2", nil, nil),
|
|
|
|
NewRecordingRule("rule3", nil, nil),
|
|
|
|
NewRecordingRule("rule3", nil, nil),
|
|
|
|
},
|
|
|
|
seriesInPreviousEval: []map[string]labels.Labels{
|
|
|
|
map[string]labels.Labels{"a": nil},
|
|
|
|
map[string]labels.Labels{"r1": nil},
|
|
|
|
map[string]labels.Labels{"r2": nil},
|
|
|
|
map[string]labels.Labels{"r3a": nil},
|
|
|
|
map[string]labels.Labels{"r3b": nil},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
oldGroup.rules[0].(*AlertingRule).active[42] = nil
|
|
|
|
newGroup := &Group{
|
|
|
|
rules: []Rule{
|
|
|
|
NewRecordingRule("rule3", nil, nil),
|
|
|
|
NewRecordingRule("rule3", nil, nil),
|
|
|
|
NewRecordingRule("rule3", nil, nil),
|
2017-08-11 18:45:52 +00:00
|
|
|
NewAlertingRule("alert", nil, 0, nil, nil, nil),
|
2017-05-19 15:43:59 +00:00
|
|
|
NewRecordingRule("rule1", nil, nil),
|
|
|
|
NewRecordingRule("rule4", nil, nil),
|
|
|
|
},
|
|
|
|
seriesInPreviousEval: make([]map[string]labels.Labels, 6),
|
|
|
|
}
|
|
|
|
newGroup.copyState(oldGroup)
|
|
|
|
|
|
|
|
want := []map[string]labels.Labels{
|
|
|
|
map[string]labels.Labels{"r3a": nil},
|
|
|
|
map[string]labels.Labels{"r3b": nil},
|
|
|
|
nil,
|
|
|
|
map[string]labels.Labels{"a": nil},
|
|
|
|
map[string]labels.Labels{"r1": nil},
|
|
|
|
nil,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(want, newGroup.seriesInPreviousEval) {
|
|
|
|
t.Fatalf("seriesInPreviousEval not as expected. Wanted: %+v Got: %+v", want, newGroup.seriesInPreviousEval)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(oldGroup.rules[0], newGroup.rules[3]) {
|
|
|
|
t.Fatalf("Active alerts not as expected. Wanted: %+v Got: %+v", oldGroup.rules[0], oldGroup.rules[3])
|
|
|
|
}
|
2017-05-18 16:47:00 +00:00
|
|
|
}
|