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 (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2013-03-21 17:06:15 +00:00
|
|
|
"time"
|
2013-06-25 12:02:27 +00:00
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
2015-03-30 17:43:19 +00:00
|
|
|
"github.com/prometheus/prometheus/promql"
|
2014-06-06 09:55:53 +00:00
|
|
|
"github.com/prometheus/prometheus/storage/local"
|
2014-12-12 19:01:32 +00:00
|
|
|
"github.com/prometheus/prometheus/storage/metric"
|
2013-01-07 22:24:26 +00:00
|
|
|
)
|
|
|
|
|
2013-04-22 22:26:59 +00:00
|
|
|
var (
|
2015-03-30 17:43:19 +00:00
|
|
|
testSampleInterval = time.Duration(5) * time.Minute
|
|
|
|
testStartTime = clientmodel.Timestamp(0)
|
2015-02-19 18:56:45 +00:00
|
|
|
)
|
|
|
|
|
2015-03-30 17:43:19 +00:00
|
|
|
func getTestValueStream(startVal clientmodel.SampleValue, endVal clientmodel.SampleValue, stepVal clientmodel.SampleValue, startTime clientmodel.Timestamp) (resultValues metric.Values) {
|
|
|
|
currentTime := startTime
|
|
|
|
for currentVal := startVal; currentVal <= endVal; currentVal += stepVal {
|
|
|
|
sample := metric.SamplePair{
|
|
|
|
Value: currentVal,
|
|
|
|
Timestamp: currentTime,
|
|
|
|
}
|
|
|
|
resultValues = append(resultValues, sample)
|
|
|
|
currentTime = currentTime.Add(testSampleInterval)
|
|
|
|
}
|
|
|
|
return resultValues
|
|
|
|
}
|
2013-01-07 22:24:26 +00:00
|
|
|
|
2015-03-30 17:43:19 +00:00
|
|
|
func getTestVectorFromTestMatrix(matrix promql.Matrix) promql.Vector {
|
|
|
|
vector := promql.Vector{}
|
|
|
|
for _, sampleStream := range matrix {
|
|
|
|
lastSample := sampleStream.Values[len(sampleStream.Values)-1]
|
|
|
|
vector = append(vector, &promql.Sample{
|
|
|
|
Metric: sampleStream.Metric,
|
|
|
|
Value: lastSample.Value,
|
|
|
|
Timestamp: lastSample.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
|
|
|
func storeMatrix(storage local.Storage, matrix promql.Matrix) {
|
|
|
|
pendingSamples := clientmodel.Samples{}
|
|
|
|
for _, sampleStream := range matrix {
|
|
|
|
for _, sample := range sampleStream.Values {
|
|
|
|
pendingSamples = append(pendingSamples, &clientmodel.Sample{
|
|
|
|
Metric: sampleStream.Metric.Metric,
|
|
|
|
Value: sample.Value,
|
|
|
|
Timestamp: sample.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
2015-03-30 17:43:19 +00:00
|
|
|
for _, s := range pendingSamples {
|
|
|
|
storage.Append(s)
|
|
|
|
}
|
|
|
|
storage.WaitForIndexing()
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func vectorComparisonString(expected []string, actual []string) string {
|
|
|
|
separator := "\n--------------\n"
|
|
|
|
return fmt.Sprintf("Expected:%v%v%v\nActual:%v%v%v ",
|
|
|
|
separator,
|
|
|
|
strings.Join(expected, "\n"),
|
|
|
|
separator,
|
|
|
|
separator,
|
|
|
|
strings.Join(actual, "\n"),
|
|
|
|
separator)
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:43:19 +00:00
|
|
|
func annotateWithTime(lines []string, timestamp clientmodel.Timestamp) []string {
|
|
|
|
annotatedLines := []string{}
|
|
|
|
for _, line := range lines {
|
|
|
|
annotatedLines = append(annotatedLines, fmt.Sprintf(line, timestamp))
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
2015-03-30 17:43:19 +00:00
|
|
|
return annotatedLines
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
2013-04-22 22:26:59 +00:00
|
|
|
|
2015-03-30 17:43:19 +00:00
|
|
|
var testMatrix = promql.Matrix{
|
|
|
|
{
|
|
|
|
Metric: clientmodel.COWMetric{
|
|
|
|
Metric: clientmodel.Metric{
|
|
|
|
clientmodel.MetricNameLabel: "http_requests",
|
|
|
|
clientmodel.JobLabel: "api-server",
|
|
|
|
"instance": "0",
|
|
|
|
"group": "canary",
|
2014-12-12 19:01:32 +00:00
|
|
|
},
|
|
|
|
},
|
2015-03-30 17:43:19 +00:00
|
|
|
Values: getTestValueStream(0, 300, 30, testStartTime),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Metric: clientmodel.COWMetric{
|
|
|
|
Metric: clientmodel.Metric{
|
|
|
|
clientmodel.MetricNameLabel: "http_requests",
|
|
|
|
clientmodel.JobLabel: "api-server",
|
|
|
|
"instance": "1",
|
|
|
|
"group": "canary",
|
2015-02-21 16:45:47 +00:00
|
|
|
},
|
|
|
|
},
|
2015-03-30 17:43:19 +00:00
|
|
|
Values: getTestValueStream(0, 400, 40, testStartTime),
|
2013-04-22 22:26:59 +00:00
|
|
|
},
|
|
|
|
{
|
2015-03-30 17:43:19 +00:00
|
|
|
Metric: clientmodel.COWMetric{
|
|
|
|
Metric: clientmodel.Metric{
|
|
|
|
clientmodel.MetricNameLabel: "http_requests",
|
|
|
|
clientmodel.JobLabel: "app-server",
|
|
|
|
"instance": "0",
|
|
|
|
"group": "canary",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Values: getTestValueStream(0, 700, 70, testStartTime),
|
2013-04-22 22:26:59 +00:00
|
|
|
},
|
|
|
|
{
|
2015-03-30 17:43:19 +00:00
|
|
|
Metric: clientmodel.COWMetric{
|
|
|
|
Metric: clientmodel.Metric{
|
|
|
|
clientmodel.MetricNameLabel: "http_requests",
|
|
|
|
clientmodel.JobLabel: "app-server",
|
|
|
|
"instance": "1",
|
|
|
|
"group": "canary",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Values: getTestValueStream(0, 800, 80, testStartTime),
|
2013-04-22 22:26:59 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-04-24 09:51:40 +00:00
|
|
|
func TestAlertingRule(t *testing.T) {
|
|
|
|
// Labels in expected output need to be alphabetically sorted.
|
|
|
|
var evalOutputs = [][]string{
|
|
|
|
{
|
2013-07-30 15:18:07 +00:00
|
|
|
`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]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
{
|
2013-07-30 15:18:07 +00:00
|
|
|
`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]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
{
|
2013-07-30 15:18:07 +00:00
|
|
|
`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]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
/* empty */
|
|
|
|
},
|
|
|
|
{
|
|
|
|
/* empty */
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:43:19 +00:00
|
|
|
storage, closer := local.NewTestStorage(t, 1)
|
2013-04-24 09:51:40 +00:00
|
|
|
defer closer.Close()
|
|
|
|
|
2015-03-30 17:43:19 +00:00
|
|
|
storeMatrix(storage, testMatrix)
|
|
|
|
|
2015-06-15 10:49:11 +00:00
|
|
|
engine := promql.NewEngine(storage, nil)
|
2015-03-30 17:43:19 +00:00
|
|
|
defer engine.Stop()
|
|
|
|
|
2015-04-29 09:36:41 +00:00
|
|
|
expr, err := promql.ParseExpr(`http_requests{group="canary", job="app-server"} < 100`)
|
2013-04-24 09:51:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to parse alert expression: %s", err)
|
|
|
|
}
|
2015-03-30 17:43:19 +00:00
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
alertLabels := clientmodel.LabelSet{
|
2013-07-30 15:18:07 +00:00
|
|
|
"severity": "critical",
|
2013-04-24 09:51:40 +00:00
|
|
|
}
|
2015-06-24 17:27:09 +00:00
|
|
|
rule := NewAlertingRule("HttpRequestRateLow", expr, time.Minute, alertLabels, "summary", "description", "runbook")
|
2013-04-24 09:51:40 +00:00
|
|
|
|
2015-03-30 17:43:19 +00:00
|
|
|
for i, expectedLines := range evalOutputs {
|
2013-04-24 09:51:40 +00:00
|
|
|
evalTime := testStartTime.Add(testSampleInterval * time.Duration(i))
|
2015-03-30 17:43:19 +00:00
|
|
|
|
2015-05-25 19:16:32 +00:00
|
|
|
res, err := rule.eval(evalTime, engine)
|
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
|
|
|
|
|
|
|
actualLines := strings.Split(res.String(), "\n")
|
|
|
|
expectedLines := annotateWithTime(expectedLines, evalTime)
|
2013-04-24 09:51:40 +00:00
|
|
|
if actualLines[0] == "" {
|
|
|
|
actualLines = []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
failed := false
|
|
|
|
if len(actualLines) != len(expectedLines) {
|
|
|
|
t.Errorf("%d. Number of samples in expected and actual output don't match (%d vs. %d)", i, len(expectedLines), len(actualLines))
|
|
|
|
failed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for j, expectedSample := range expectedLines {
|
|
|
|
found := false
|
|
|
|
for _, actualSample := range actualLines {
|
|
|
|
if actualSample == expectedSample {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Errorf("%d.%d. Couldn't find expected sample in output: '%v'", i, j, expectedSample)
|
|
|
|
failed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if failed {
|
|
|
|
t.Fatalf("%d. Expected and actual outputs don't match:\n%v", i, vectorComparisonString(expectedLines, actualLines))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|