2015-01-21 11:07:45 -08:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-02-07 02:49:04 -08: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 14:24:26 -08:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-06-30 02:51:05 -07:00
|
|
|
"reflect"
|
2013-01-07 14:24:26 -08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2013-03-21 10:06:15 -07:00
|
|
|
"time"
|
2013-06-25 05:02:27 -07:00
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2013-06-25 05:02:27 -07:00
|
|
|
|
2015-03-30 10:43:19 -07:00
|
|
|
"github.com/prometheus/prometheus/promql"
|
2013-01-07 14:24:26 -08:00
|
|
|
)
|
|
|
|
|
2015-06-30 02:51:05 -07:00
|
|
|
func TestAlertingRule(t *testing.T) {
|
|
|
|
suite, err := promql.NewTest(t, `
|
|
|
|
load 5m
|
|
|
|
http_requests{job="api-server", instance="0", group="production"} 0+10x10
|
|
|
|
http_requests{job="api-server", instance="1", group="production"} 0+20x10
|
|
|
|
http_requests{job="api-server", instance="0", group="canary"} 0+30x10
|
|
|
|
http_requests{job="api-server", instance="1", group="canary"} 0+40x10
|
|
|
|
http_requests{job="app-server", instance="0", group="production"} 0+50x10
|
|
|
|
http_requests{job="app-server", instance="1", group="production"} 0+60x10
|
|
|
|
http_requests{job="app-server", instance="0", group="canary"} 0+70x10
|
|
|
|
http_requests{job="app-server", instance="1", group="canary"} 0+80x10
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2015-03-30 10:43:19 -07:00
|
|
|
}
|
2015-06-30 02:51:05 -07:00
|
|
|
defer suite.Close()
|
2015-03-30 10:43:19 -07:00
|
|
|
|
2015-06-30 02:51:05 -07:00
|
|
|
if err := suite.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-03-30 10:43:19 -07:00
|
|
|
}
|
2013-01-07 14:24:26 -08:00
|
|
|
|
2015-06-30 02:51:05 -07: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 14:24:26 -08:00
|
|
|
}
|
2013-04-22 15:26:59 -07:00
|
|
|
|
2015-06-30 02:51:05 -07:00
|
|
|
rule := NewAlertingRule(
|
|
|
|
"HTTPRequestRateLow",
|
|
|
|
expr,
|
|
|
|
time.Minute,
|
2015-08-20 08:18:46 -07:00
|
|
|
model.LabelSet{"severity": "critical"},
|
2015-06-30 02:51:05 -07:00
|
|
|
"summary", "description", "runbook",
|
|
|
|
)
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
time time.Duration
|
|
|
|
result []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
time: 0,
|
|
|
|
result: []string{
|
|
|
|
`ALERTS{alertname="HTTPRequestRateLow", alertstate="pending", group="canary", instance="0", job="app-server", severity="critical"} => 1 @[%v]`,
|
|
|
|
`ALERTS{alertname="HTTPRequestRateLow", alertstate="pending", group="canary", instance="1", job="app-server", severity="critical"} => 1 @[%v]`,
|
2015-02-21 08:45:47 -08:00
|
|
|
},
|
2015-06-30 02:51:05 -07:00
|
|
|
}, {
|
|
|
|
time: 5 * time.Minute,
|
|
|
|
result: []string{
|
|
|
|
`ALERTS{alertname="HTTPRequestRateLow", alertstate="pending", group="canary", instance="0", job="app-server", severity="critical"} => 0 @[%v]`,
|
|
|
|
`ALERTS{alertname="HTTPRequestRateLow", alertstate="firing", group="canary", instance="0", job="app-server", severity="critical"} => 1 @[%v]`,
|
|
|
|
`ALERTS{alertname="HTTPRequestRateLow", alertstate="pending", group="canary", instance="1", job="app-server", severity="critical"} => 0 @[%v]`,
|
|
|
|
`ALERTS{alertname="HTTPRequestRateLow", alertstate="firing", group="canary", instance="1", job="app-server", severity="critical"} => 1 @[%v]`,
|
2015-03-30 10:43:19 -07:00
|
|
|
},
|
2015-06-30 02:51:05 -07:00
|
|
|
}, {
|
|
|
|
time: 10 * time.Minute,
|
|
|
|
result: []string{
|
|
|
|
`ALERTS{alertname="HTTPRequestRateLow", alertstate="firing", group="canary", instance="1", job="app-server", severity="critical"} => 0 @[%v]`,
|
|
|
|
`ALERTS{alertname="HTTPRequestRateLow", alertstate="firing", group="canary", instance="0", job="app-server", severity="critical"} => 0 @[%v]`,
|
2015-03-30 10:43:19 -07:00
|
|
|
},
|
|
|
|
},
|
2013-04-24 02:51:40 -07:00
|
|
|
{
|
2015-06-30 02:51:05 -07:00
|
|
|
time: 15 * time.Minute,
|
|
|
|
result: nil,
|
2013-04-24 02:51:40 -07:00
|
|
|
},
|
|
|
|
{
|
2015-06-30 02:51:05 -07:00
|
|
|
time: 20 * time.Minute,
|
|
|
|
result: nil,
|
2013-04-24 02:51:40 -07:00
|
|
|
},
|
|
|
|
}
|
2015-03-30 10:43:19 -07:00
|
|
|
|
2015-06-30 02:51:05 -07:00
|
|
|
for i, test := range tests {
|
2015-08-20 08:18:46 -07:00
|
|
|
evalTime := model.Time(0).Add(test.time)
|
2015-03-30 10:43:19 -07:00
|
|
|
|
2015-06-30 02:51:05 -07:00
|
|
|
res, err := rule.eval(evalTime, suite.QueryEngine())
|
2013-04-24 02:51:40 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error during alerting rule evaluation: %s", err)
|
|
|
|
}
|
2015-03-30 10:43:19 -07:00
|
|
|
|
2015-06-30 02:51:05 -07:00
|
|
|
actual := strings.Split(res.String(), "\n")
|
|
|
|
expected := annotateWithTime(test.result, evalTime)
|
|
|
|
if actual[0] == "" {
|
|
|
|
actual = []string{}
|
2013-04-24 02:51:40 -07:00
|
|
|
}
|
|
|
|
|
2015-06-30 02:51:05 -07: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 02:51:40 -07:00
|
|
|
}
|
|
|
|
|
2015-06-30 02:51:05 -07:00
|
|
|
for j, expectedSample := range expected {
|
2013-04-24 02:51:40 -07:00
|
|
|
found := false
|
2015-06-30 02:51:05 -07:00
|
|
|
for _, actualSample := range actual {
|
2013-04-24 02:51:40 -07: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 02:51:05 -07: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 02:51:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-30 02:51:05 -07:00
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
func annotateWithTime(lines []string, timestamp model.Time) []string {
|
2015-06-30 02:51:05 -07:00
|
|
|
annotatedLines := []string{}
|
|
|
|
for _, line := range lines {
|
|
|
|
annotatedLines = append(annotatedLines, fmt.Sprintf(line, timestamp))
|
|
|
|
}
|
|
|
|
return annotatedLines
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransferAlertState(t *testing.T) {
|
|
|
|
m := NewManager(&ManagerOptions{})
|
|
|
|
|
|
|
|
alert := &Alert{
|
|
|
|
Name: "testalert",
|
|
|
|
State: StateFiring,
|
|
|
|
}
|
|
|
|
|
|
|
|
arule := AlertingRule{
|
|
|
|
name: "test",
|
2015-08-20 08:18:46 -07:00
|
|
|
activeAlerts: map[model.Fingerprint]*Alert{},
|
2015-06-30 02:51:05 -07:00
|
|
|
}
|
|
|
|
aruleCopy := arule
|
|
|
|
|
|
|
|
m.rules = append(m.rules, &arule)
|
|
|
|
|
|
|
|
// Set an alert.
|
|
|
|
arule.activeAlerts[0] = alert
|
|
|
|
|
|
|
|
// Save state and get the restore function.
|
|
|
|
restore := m.transferAlertState()
|
|
|
|
|
|
|
|
// Remove arule from the rule list and add an unrelated rule and the
|
|
|
|
// stateless copy of arule.
|
|
|
|
m.rules = []Rule{
|
|
|
|
&AlertingRule{
|
|
|
|
name: "test_other",
|
2015-08-20 08:18:46 -07:00
|
|
|
activeAlerts: map[model.Fingerprint]*Alert{},
|
2015-06-30 02:51:05 -07:00
|
|
|
},
|
|
|
|
&aruleCopy,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the restore function.
|
|
|
|
restore()
|
|
|
|
|
|
|
|
if ar := m.rules[0].(*AlertingRule); len(ar.activeAlerts) != 0 {
|
|
|
|
t.Fatalf("unexpected alert for unrelated alerting rule")
|
|
|
|
}
|
|
|
|
if ar := m.rules[1].(*AlertingRule); !reflect.DeepEqual(ar.activeAlerts[0], alert) {
|
|
|
|
t.Fatalf("alert state was not restored")
|
|
|
|
}
|
|
|
|
}
|