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"
|
2013-04-22 22:26:59 +00:00
|
|
|
"path"
|
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
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/rules/ast"
|
|
|
|
"github.com/prometheus/prometheus/stats"
|
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-06-25 12:02:27 +00:00
|
|
|
"github.com/prometheus/prometheus/utility/test"
|
2013-01-07 22:24:26 +00:00
|
|
|
)
|
|
|
|
|
2013-04-22 22:26:59 +00:00
|
|
|
var (
|
2013-04-24 09:51:40 +00:00
|
|
|
testEvalTime = testStartTime.Add(testSampleInterval * 10)
|
2013-04-22 22:26:59 +00:00
|
|
|
fixturesPath = "fixtures"
|
|
|
|
)
|
2013-01-07 22:24:26 +00:00
|
|
|
|
Use custom timestamp type for sample timestamps and related code.
So far we've been using Go's native time.Time for anything related to sample
timestamps. Since the range of time.Time is much bigger than what we need, this
has created two problems:
- there could be time.Time values which were out of the range/precision of the
time type that we persist to disk, therefore causing incorrectly ordered keys.
One bug caused by this was:
https://github.com/prometheus/prometheus/issues/367
It would be good to use a timestamp type that's more closely aligned with
what the underlying storage supports.
- sizeof(time.Time) is 192, while Prometheus should be ok with a single 64-bit
Unix timestamp (possibly even a 32-bit one). Since we store samples in large
numbers, this seriously affects memory usage. Furthermore, copying/working
with the data will be faster if it's smaller.
*MEMORY USAGE RESULTS*
Initial memory usage comparisons for a running Prometheus with 1 timeseries and
100,000 samples show roughly a 13% decrease in total (VIRT) memory usage. In my
tests, this advantage for some reason decreased a bit the more samples the
timeseries had (to 5-7% for millions of samples). This I can't fully explain,
but perhaps garbage collection issues were involved.
*WHEN TO USE THE NEW TIMESTAMP TYPE*
The new clientmodel.Timestamp type should be used whenever time
calculations are either directly or indirectly related to sample
timestamps.
For example:
- the timestamp of a sample itself
- all kinds of watermarks
- anything that may become or is compared to a sample timestamp (like the timestamp
passed into Target.Scrape()).
When to still use time.Time:
- for measuring durations/times not related to sample timestamps, like duration
telemetry exporting, timers that indicate how frequently to execute some
action, etc.
*NOTE ON OPERATOR OPTIMIZATION TESTS*
We don't use operator optimization code anymore, but it still lives in
the code as dead code. It still has tests, but I couldn't get all of them to
pass with the new timestamp format. I commented out the failing cases for now,
but we should probably remove the dead code soon. I just didn't want to do that
in the same change as this.
Change-Id: I821787414b0debe85c9fffaeb57abd453727af0f
2013-10-28 13:35:02 +00:00
|
|
|
func annotateWithTime(lines []string, timestamp clientmodel.Timestamp) []string {
|
2013-01-07 22:24:26 +00:00
|
|
|
annotatedLines := []string{}
|
|
|
|
for _, line := range lines {
|
2013-04-24 09:51:40 +00:00
|
|
|
annotatedLines = append(annotatedLines, fmt.Sprintf(line, timestamp))
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
return annotatedLines
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-10-08 11:49:42 +00:00
|
|
|
func newTestStorage(t testing.TB) (storage local.Storage, closer test.Closer) {
|
|
|
|
storage, closer = local.NewTestStorage(t)
|
2014-04-11 07:27:05 +00:00
|
|
|
storeMatrix(storage, testMatrix)
|
2014-06-06 09:55:53 +00:00
|
|
|
return storage, closer
|
2013-04-24 09:51:40 +00:00
|
|
|
}
|
2013-01-07 22:24:26 +00:00
|
|
|
|
2013-05-08 14:35:16 +00:00
|
|
|
func TestExpressions(t *testing.T) {
|
2013-04-24 09:51:40 +00:00
|
|
|
// Labels in expected output need to be alphabetically sorted.
|
2014-12-12 19:01:32 +00:00
|
|
|
expressionTests := []struct {
|
2015-02-18 01:30:41 +00:00
|
|
|
expr string
|
|
|
|
output []string
|
|
|
|
shouldFail bool
|
|
|
|
checkOrder bool
|
2013-04-24 09:51:40 +00:00
|
|
|
}{
|
|
|
|
{
|
2015-02-18 01:30:41 +00:00
|
|
|
expr: `SUM(http_requests)`,
|
|
|
|
output: []string{`{} => 3600 @[%v]`},
|
2013-12-13 18:20:42 +00:00
|
|
|
}, {
|
|
|
|
expr: `SUM(http_requests{instance="0"}) BY(job)`,
|
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => 400 @[%v]`,
|
|
|
|
`{job="app-server"} => 1200 @[%v]`,
|
2013-12-13 18:20:42 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
expr: `SUM(http_requests{instance="0"}) BY(job) KEEPING_EXTRA`,
|
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{instance="0", job="api-server"} => 400 @[%v]`,
|
|
|
|
`{instance="0", job="app-server"} => 1200 @[%v]`,
|
2013-12-13 18:20:42 +00:00
|
|
|
},
|
2013-04-24 09:51:40 +00:00
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `SUM(http_requests) BY (job)`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => 1000 @[%v]`,
|
|
|
|
`{job="app-server"} => 2600 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
2014-01-24 15:05:30 +00:00
|
|
|
}, {
|
|
|
|
// Non-existent labels mentioned in BY-clauses shouldn't propagate to output.
|
|
|
|
expr: `SUM(http_requests) BY (job, nonexistent)`,
|
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => 1000 @[%v]`,
|
|
|
|
`{job="app-server"} => 2600 @[%v]`,
|
2014-01-24 15:05:30 +00:00
|
|
|
},
|
2013-07-11 16:38:44 +00:00
|
|
|
}, {
|
|
|
|
expr: `
|
|
|
|
// Test comment.
|
|
|
|
SUM(http_requests) BY /* comments shouldn't
|
|
|
|
have any effect */ (job) // another comment`,
|
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => 1000 @[%v]`,
|
|
|
|
`{job="app-server"} => 2600 @[%v]`,
|
2013-07-11 16:38:44 +00:00
|
|
|
},
|
2013-05-08 14:35:16 +00:00
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `COUNT(http_requests) BY (job)`,
|
2013-05-08 14:35:16 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => 4 @[%v]`,
|
|
|
|
`{job="app-server"} => 4 @[%v]`,
|
2013-05-08 14:35:16 +00:00
|
|
|
},
|
2013-04-24 09:51:40 +00:00
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `SUM(http_requests) BY (job, group)`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{group="canary", job="api-server"} => 700 @[%v]`,
|
|
|
|
`{group="canary", job="app-server"} => 1500 @[%v]`,
|
|
|
|
`{group="production", job="api-server"} => 300 @[%v]`,
|
|
|
|
`{group="production", job="app-server"} => 1100 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `AVG(http_requests) BY (job)`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => 250 @[%v]`,
|
|
|
|
`{job="app-server"} => 650 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `MIN(http_requests) BY (job)`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => 100 @[%v]`,
|
|
|
|
`{job="app-server"} => 500 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `MAX(http_requests) BY (job)`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => 400 @[%v]`,
|
|
|
|
`{job="app-server"} => 800 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `SUM(http_requests) BY (job) - COUNT(http_requests) BY (job)`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => 996 @[%v]`,
|
|
|
|
`{job="app-server"} => 2596 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
2014-04-08 14:14:35 +00:00
|
|
|
}, {
|
|
|
|
expr: `2 - SUM(http_requests) BY (job)`,
|
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => -998 @[%v]`,
|
|
|
|
`{job="app-server"} => -2598 @[%v]`,
|
2014-04-08 14:14:35 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
expr: `1000 / SUM(http_requests) BY (job)`,
|
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => 1 @[%v]`,
|
|
|
|
`{job="app-server"} => 0.38461538461538464 @[%v]`,
|
2014-04-08 14:14:35 +00:00
|
|
|
},
|
2013-04-24 09:51:40 +00:00
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `SUM(http_requests) BY (job) - 2`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => 998 @[%v]`,
|
|
|
|
`{job="app-server"} => 2598 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `SUM(http_requests) BY (job) % 3`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => 1 @[%v]`,
|
|
|
|
`{job="app-server"} => 2 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `SUM(http_requests) BY (job) / 0`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => +Inf @[%v]`,
|
|
|
|
`{job="app-server"} => +Inf @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `SUM(http_requests) BY (job) > 1000`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="app-server"} => 2600 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
2014-04-08 14:14:35 +00:00
|
|
|
}, {
|
|
|
|
expr: `1000 < SUM(http_requests) BY (job)`,
|
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="app-server"} => 1000 @[%v]`,
|
2014-04-08 14:14:35 +00:00
|
|
|
},
|
2013-04-24 09:51:40 +00:00
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `SUM(http_requests) BY (job) <= 1000`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => 1000 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `SUM(http_requests) BY (job) != 1000`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="app-server"} => 2600 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `SUM(http_requests) BY (job) == 1000`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => 1000 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `SUM(http_requests) BY (job) + SUM(http_requests) BY (job)`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="api-server"} => 2000 @[%v]`,
|
|
|
|
`{job="app-server"} => 5200 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `http_requests{job="api-server", group="canary"}`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2013-06-11 09:01:36 +00:00
|
|
|
`http_requests{group="canary", instance="0", job="api-server"} => 300 @[%v]`,
|
|
|
|
`http_requests{group="canary", instance="1", job="api-server"} => 400 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
}, {
|
2015-01-22 11:52:30 +00:00
|
|
|
expr: `http_requests{job="api-server", group="canary"} + rate(http_requests{job="api-server"}[5m]) * 5 * 60`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{group="canary", instance="0", job="api-server"} => 330 @[%v]`,
|
|
|
|
`{group="canary", instance="1", job="api-server"} => 440 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
2015-01-22 11:52:30 +00:00
|
|
|
}, {
|
|
|
|
expr: `rate(http_requests[25m]) * 25 * 60`,
|
|
|
|
output: []string{
|
|
|
|
`{group="canary", instance="0", job="api-server"} => 150 @[%v]`,
|
|
|
|
`{group="canary", instance="0", job="app-server"} => 350 @[%v]`,
|
|
|
|
`{group="canary", instance="1", job="api-server"} => 200 @[%v]`,
|
|
|
|
`{group="canary", instance="1", job="app-server"} => 400 @[%v]`,
|
|
|
|
`{group="production", instance="0", job="api-server"} => 50 @[%v]`,
|
|
|
|
`{group="production", instance="0", job="app-server"} => 249.99999999999997 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => 100 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="app-server"} => 300 @[%v]`,
|
|
|
|
},
|
2013-04-24 09:51:40 +00:00
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `delta(http_requests[25m], 1)`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{group="canary", instance="0", job="api-server"} => 150 @[%v]`,
|
|
|
|
`{group="canary", instance="0", job="app-server"} => 350 @[%v]`,
|
|
|
|
`{group="canary", instance="1", job="api-server"} => 200 @[%v]`,
|
|
|
|
`{group="canary", instance="1", job="app-server"} => 400 @[%v]`,
|
|
|
|
`{group="production", instance="0", job="api-server"} => 50 @[%v]`,
|
|
|
|
`{group="production", instance="0", job="app-server"} => 250 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => 100 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="app-server"} => 300 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `sort(http_requests)`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2013-06-11 09:01:36 +00:00
|
|
|
`http_requests{group="production", instance="0", job="api-server"} => 100 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="1", job="api-server"} => 200 @[%v]`,
|
|
|
|
`http_requests{group="canary", instance="0", job="api-server"} => 300 @[%v]`,
|
|
|
|
`http_requests{group="canary", instance="1", job="api-server"} => 400 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="0", job="app-server"} => 500 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="1", job="app-server"} => 600 @[%v]`,
|
|
|
|
`http_requests{group="canary", instance="0", job="app-server"} => 700 @[%v]`,
|
|
|
|
`http_requests{group="canary", instance="1", job="app-server"} => 800 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
2015-02-18 01:30:41 +00:00
|
|
|
checkOrder: true,
|
2013-04-24 09:51:40 +00:00
|
|
|
}, {
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `sort_desc(http_requests)`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2013-06-11 09:01:36 +00:00
|
|
|
`http_requests{group="canary", instance="1", job="app-server"} => 800 @[%v]`,
|
|
|
|
`http_requests{group="canary", instance="0", job="app-server"} => 700 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="1", job="app-server"} => 600 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="0", job="app-server"} => 500 @[%v]`,
|
|
|
|
`http_requests{group="canary", instance="1", job="api-server"} => 400 @[%v]`,
|
|
|
|
`http_requests{group="canary", instance="0", job="api-server"} => 300 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="1", job="api-server"} => 200 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="0", job="api-server"} => 100 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
2015-02-18 01:30:41 +00:00
|
|
|
checkOrder: true,
|
2014-08-05 16:57:47 +00:00
|
|
|
}, {
|
|
|
|
expr: `topk(3, http_requests)`,
|
|
|
|
output: []string{
|
|
|
|
`http_requests{group="canary", instance="1", job="app-server"} => 800 @[%v]`,
|
|
|
|
`http_requests{group="canary", instance="0", job="app-server"} => 700 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="1", job="app-server"} => 600 @[%v]`,
|
|
|
|
},
|
2015-02-18 01:30:41 +00:00
|
|
|
checkOrder: true,
|
2014-08-05 18:14:04 +00:00
|
|
|
}, {
|
|
|
|
expr: `topk(5, http_requests{group="canary",job="app-server"})`,
|
|
|
|
output: []string{
|
|
|
|
`http_requests{group="canary", instance="1", job="app-server"} => 800 @[%v]`,
|
|
|
|
`http_requests{group="canary", instance="0", job="app-server"} => 700 @[%v]`,
|
|
|
|
},
|
2015-02-18 01:30:41 +00:00
|
|
|
checkOrder: true,
|
2014-08-05 16:57:47 +00:00
|
|
|
}, {
|
|
|
|
expr: `bottomk(3, http_requests)`,
|
|
|
|
output: []string{
|
|
|
|
`http_requests{group="production", instance="0", job="api-server"} => 100 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="1", job="api-server"} => 200 @[%v]`,
|
|
|
|
`http_requests{group="canary", instance="0", job="api-server"} => 300 @[%v]`,
|
|
|
|
},
|
2015-02-18 01:30:41 +00:00
|
|
|
checkOrder: true,
|
2014-08-05 18:14:04 +00:00
|
|
|
}, {
|
|
|
|
expr: `bottomk(5, http_requests{group="canary",job="app-server"})`,
|
|
|
|
output: []string{
|
|
|
|
`http_requests{group="canary", instance="0", job="app-server"} => 700 @[%v]`,
|
|
|
|
`http_requests{group="canary", instance="1", job="app-server"} => 800 @[%v]`,
|
|
|
|
},
|
2015-02-18 01:30:41 +00:00
|
|
|
checkOrder: true,
|
2013-04-24 09:51:40 +00:00
|
|
|
}, {
|
|
|
|
// Single-letter label names and values.
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `x{y="testvalue"}`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2013-06-11 09:01:36 +00:00
|
|
|
`x{y="testvalue"} => 100 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
// Lower-cased aggregation operators should work too.
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `sum(http_requests) by (job) + min(http_requests) by (job) + max(http_requests) by (job) + avg(http_requests) by (job)`,
|
2013-04-24 09:51:40 +00:00
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{job="app-server"} => 4550 @[%v]`,
|
|
|
|
`{job="api-server"} => 1750 @[%v]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
// Deltas should be adjusted for target interval vs. samples under target interval.
|
2015-02-18 01:30:41 +00:00
|
|
|
expr: `delta(http_requests{group="canary", instance="1", job="app-server"}[18m])`,
|
|
|
|
output: []string{`{group="canary", instance="1", job="app-server"} => 288 @[%v]`},
|
2015-01-22 11:52:30 +00:00
|
|
|
}, {
|
|
|
|
// Deltas should perform the same operation when 2nd argument is 0.
|
2015-02-18 01:30:41 +00:00
|
|
|
expr: `delta(http_requests{group="canary", instance="1", job="app-server"}[18m], 0)`,
|
|
|
|
output: []string{`{group="canary", instance="1", job="app-server"} => 288 @[%v]`},
|
2013-04-24 09:51:40 +00:00
|
|
|
}, {
|
2015-01-22 11:52:30 +00:00
|
|
|
// Rates should calculate per-second rates.
|
2015-02-18 01:30:41 +00:00
|
|
|
expr: `rate(http_requests{group="canary", instance="1", job="app-server"}[60m])`,
|
|
|
|
output: []string{`{group="canary", instance="1", job="app-server"} => 0.26666666666666666 @[%v]`},
|
2013-05-12 00:03:16 +00:00
|
|
|
}, {
|
2015-01-22 11:52:30 +00:00
|
|
|
// Deriv should return the same as rate in simple cases.
|
2015-02-18 01:30:41 +00:00
|
|
|
expr: `deriv(http_requests{group="canary", instance="1", job="app-server"}[60m])`,
|
|
|
|
output: []string{`{group="canary", instance="1", job="app-server"} => 0.26666666666666666 @[%v]`},
|
2013-05-12 00:03:16 +00:00
|
|
|
}, {
|
2015-01-22 11:52:30 +00:00
|
|
|
// Counter resets at in the middle of range are handled correctly by rate().
|
2015-02-18 01:30:41 +00:00
|
|
|
expr: `rate(testcounter_reset_middle[60m])`,
|
|
|
|
output: []string{`{} => 0.03 @[%v]`},
|
2013-05-28 15:36:53 +00:00
|
|
|
}, {
|
2015-01-22 11:52:30 +00:00
|
|
|
// Counter resets at end of range are ignored by rate().
|
2015-02-18 01:30:41 +00:00
|
|
|
expr: `rate(testcounter_reset_end[5m])`,
|
|
|
|
output: []string{`{} => 0 @[%v]`},
|
2013-05-28 15:36:53 +00:00
|
|
|
}, {
|
2015-01-22 11:52:30 +00:00
|
|
|
// Deriv should return correct result.
|
2015-02-18 01:30:41 +00:00
|
|
|
expr: `deriv(testcounter_reset_middle[100m])`,
|
|
|
|
output: []string{`{} => 0.010606060606060607 @[%v]`},
|
2014-01-30 12:07:26 +00:00
|
|
|
}, {
|
|
|
|
// count_scalar for a non-empty vector should return scalar element count.
|
2015-02-18 01:30:41 +00:00
|
|
|
expr: `count_scalar(http_requests)`,
|
|
|
|
output: []string{`scalar: 8 @[%v]`},
|
2014-01-30 12:07:26 +00:00
|
|
|
}, {
|
|
|
|
// count_scalar for an empty vector should return scalar 0.
|
2015-02-18 01:30:41 +00:00
|
|
|
expr: `count_scalar(nonexistent)`,
|
|
|
|
output: []string{`scalar: 0 @[%v]`},
|
2013-04-24 09:51:40 +00:00
|
|
|
}, {
|
2014-08-05 18:14:04 +00:00
|
|
|
// Empty expressions shouldn't parse.
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: ``,
|
2013-04-24 09:51:40 +00:00
|
|
|
shouldFail: true,
|
|
|
|
}, {
|
2014-08-05 18:14:04 +00:00
|
|
|
// Interval durations can't be in quotes.
|
2013-06-11 09:01:36 +00:00
|
|
|
expr: `http_requests["1m"]`,
|
2013-04-24 09:51:40 +00:00
|
|
|
shouldFail: true,
|
2014-04-08 14:14:35 +00:00
|
|
|
}, {
|
|
|
|
// Binop arguments need to be scalar or vector.
|
|
|
|
expr: `http_requests - http_requests[1m]`,
|
|
|
|
shouldFail: true,
|
2014-03-28 10:58:47 +00:00
|
|
|
}, {
|
|
|
|
expr: `http_requests{group!="canary"}`,
|
|
|
|
output: []string{
|
|
|
|
`http_requests{group="production", instance="1", job="app-server"} => 600 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="0", job="app-server"} => 500 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="1", job="api-server"} => 200 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="0", job="api-server"} => 100 @[%v]`,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
expr: `http_requests{job=~"server",group!="canary"}`,
|
|
|
|
output: []string{
|
|
|
|
`http_requests{group="production", instance="1", job="app-server"} => 600 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="0", job="app-server"} => 500 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="1", job="api-server"} => 200 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="0", job="api-server"} => 100 @[%v]`,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
expr: `http_requests{job!~"api",group!="canary"}`,
|
|
|
|
output: []string{
|
|
|
|
`http_requests{group="production", instance="1", job="app-server"} => 600 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="0", job="app-server"} => 500 @[%v]`,
|
|
|
|
},
|
|
|
|
}, {
|
2015-02-18 01:30:41 +00:00
|
|
|
expr: `count_scalar(http_requests{job=~"^server$"})`,
|
|
|
|
output: []string{`scalar: 0 @[%v]`},
|
2014-03-28 10:58:47 +00:00
|
|
|
}, {
|
|
|
|
expr: `http_requests{group="production",job=~"^api"}`,
|
|
|
|
output: []string{
|
2014-07-28 14:12:58 +00:00
|
|
|
`http_requests{group="production", instance="0", job="api-server"} => 100 @[%v]`,
|
2014-03-28 10:58:47 +00:00
|
|
|
`http_requests{group="production", instance="1", job="api-server"} => 200 @[%v]`,
|
2014-07-28 14:12:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `abs(-1 * http_requests{group="production",job="api-server"})`,
|
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{group="production", instance="0", job="api-server"} => 100 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => 200 @[%v]`,
|
2014-03-28 10:58:47 +00:00
|
|
|
},
|
2013-04-24 09:51:40 +00:00
|
|
|
},
|
2015-02-04 11:24:00 +00:00
|
|
|
{
|
|
|
|
expr: `floor(0.004 * http_requests{group="production",job="api-server"})`,
|
|
|
|
output: []string{
|
|
|
|
`{group="production", instance="0", job="api-server"} => 0 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => 0 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `ceil(0.004 * http_requests{group="production",job="api-server"})`,
|
|
|
|
output: []string{
|
|
|
|
`{group="production", instance="0", job="api-server"} => 1 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => 1 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `round(0.004 * http_requests{group="production",job="api-server"})`,
|
|
|
|
output: []string{
|
|
|
|
`{group="production", instance="0", job="api-server"} => 0 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => 1 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ // Round should correctly handle negative numbers.
|
|
|
|
expr: `round(-1 * (0.004 * http_requests{group="production",job="api-server"}))`,
|
|
|
|
output: []string{
|
2015-02-05 16:06:35 +00:00
|
|
|
`{group="production", instance="0", job="api-server"} => 0 @[%v]`,
|
2015-02-04 11:24:00 +00:00
|
|
|
`{group="production", instance="1", job="api-server"} => -1 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
2015-02-05 16:06:35 +00:00
|
|
|
{ // Round should round half up.
|
2015-02-04 11:24:00 +00:00
|
|
|
expr: `round(0.005 * http_requests{group="production",job="api-server"})`,
|
|
|
|
output: []string{
|
2015-02-05 16:06:35 +00:00
|
|
|
`{group="production", instance="0", job="api-server"} => 1 @[%v]`,
|
2015-02-04 11:24:00 +00:00
|
|
|
`{group="production", instance="1", job="api-server"} => 1 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `round(-1 * (0.005 * http_requests{group="production",job="api-server"}))`,
|
|
|
|
output: []string{
|
|
|
|
`{group="production", instance="0", job="api-server"} => 0 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => -1 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
2015-02-05 16:06:35 +00:00
|
|
|
{
|
2015-02-04 11:24:00 +00:00
|
|
|
expr: `round(1 + 0.005 * http_requests{group="production",job="api-server"})`,
|
|
|
|
output: []string{
|
|
|
|
`{group="production", instance="0", job="api-server"} => 2 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => 2 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `round(-1 * (1 + 0.005 * http_requests{group="production",job="api-server"}))`,
|
|
|
|
output: []string{
|
2015-02-05 16:06:35 +00:00
|
|
|
`{group="production", instance="0", job="api-server"} => -1 @[%v]`,
|
2015-02-04 11:24:00 +00:00
|
|
|
`{group="production", instance="1", job="api-server"} => -2 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
2015-02-05 16:06:35 +00:00
|
|
|
{ // Round should accept the number to round nearest to.
|
|
|
|
expr: `round(0.0005 * http_requests{group="production",job="api-server"}, 0.1)`,
|
2015-02-04 11:24:00 +00:00
|
|
|
output: []string{
|
2015-02-05 16:06:35 +00:00
|
|
|
`{group="production", instance="0", job="api-server"} => 0.1 @[%v]`,
|
2015-02-04 11:24:00 +00:00
|
|
|
`{group="production", instance="1", job="api-server"} => 0.1 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
2015-02-05 16:06:35 +00:00
|
|
|
{
|
|
|
|
expr: `round(2.1 + 0.0005 * http_requests{group="production",job="api-server"}, 0.1)`,
|
2015-02-04 11:24:00 +00:00
|
|
|
output: []string{
|
|
|
|
`{group="production", instance="0", job="api-server"} => 2.2 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => 2.2 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-02-05 16:06:35 +00:00
|
|
|
expr: `round(5.2 + 0.0005 * http_requests{group="production",job="api-server"}, 0.1)`,
|
2015-02-04 11:24:00 +00:00
|
|
|
output: []string{
|
2015-02-05 16:06:35 +00:00
|
|
|
`{group="production", instance="0", job="api-server"} => 5.3 @[%v]`,
|
2015-02-04 11:24:00 +00:00
|
|
|
`{group="production", instance="1", job="api-server"} => 5.3 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ // Round should work correctly with negative numbers and multiple decimal places.
|
2015-02-05 16:06:35 +00:00
|
|
|
expr: `round(-1 * (5.2 + 0.0005 * http_requests{group="production",job="api-server"}), 0.1)`,
|
2015-02-04 11:24:00 +00:00
|
|
|
output: []string{
|
|
|
|
`{group="production", instance="0", job="api-server"} => -5.2 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => -5.3 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
2015-02-05 16:06:35 +00:00
|
|
|
{ // Round should work correctly with big toNearests.
|
|
|
|
expr: `round(0.025 * http_requests{group="production",job="api-server"}, 5)`,
|
|
|
|
output: []string{
|
|
|
|
`{group="production", instance="0", job="api-server"} => 5 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => 5 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `round(0.045 * http_requests{group="production",job="api-server"}, 5)`,
|
|
|
|
output: []string{
|
|
|
|
`{group="production", instance="0", job="api-server"} => 5 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => 10 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
2014-07-28 14:12:58 +00:00
|
|
|
{
|
|
|
|
expr: `avg_over_time(http_requests{group="production",job="api-server"}[1h])`,
|
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{group="production", instance="0", job="api-server"} => 50 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => 100 @[%v]`,
|
2014-07-28 14:12:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `count_over_time(http_requests{group="production",job="api-server"}[1h])`,
|
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{group="production", instance="0", job="api-server"} => 11 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => 11 @[%v]`,
|
2014-07-28 14:12:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `max_over_time(http_requests{group="production",job="api-server"}[1h])`,
|
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{group="production", instance="0", job="api-server"} => 100 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => 200 @[%v]`,
|
2014-07-28 14:12:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `min_over_time(http_requests{group="production",job="api-server"}[1h])`,
|
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{group="production", instance="0", job="api-server"} => 0 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => 0 @[%v]`,
|
2014-07-28 14:12:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `sum_over_time(http_requests{group="production",job="api-server"}[1h])`,
|
|
|
|
output: []string{
|
2014-10-15 15:03:08 +00:00
|
|
|
`{group="production", instance="0", job="api-server"} => 550 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => 1100 @[%v]`,
|
2014-07-28 14:12:58 +00:00
|
|
|
},
|
|
|
|
},
|
2014-07-28 16:10:11 +00:00
|
|
|
{
|
2015-02-18 01:30:41 +00:00
|
|
|
expr: `time()`,
|
|
|
|
output: []string{`scalar: 3000 @[%v]`},
|
2014-07-28 16:10:11 +00:00
|
|
|
},
|
2014-08-05 17:56:35 +00:00
|
|
|
{
|
|
|
|
expr: `drop_common_labels(http_requests{group="production",job="api-server"})`,
|
|
|
|
output: []string{
|
|
|
|
`http_requests{instance="0"} => 100 @[%v]`,
|
|
|
|
`http_requests{instance="1"} => 200 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
2014-10-14 15:21:39 +00:00
|
|
|
{
|
|
|
|
expr: `{` + string(clientmodel.MetricNameLabel) + `=~".*"}`,
|
|
|
|
output: []string{
|
|
|
|
`http_requests{group="canary", instance="0", job="api-server"} => 300 @[%v]`,
|
|
|
|
`http_requests{group="canary", instance="0", job="app-server"} => 700 @[%v]`,
|
|
|
|
`http_requests{group="canary", instance="1", job="api-server"} => 400 @[%v]`,
|
|
|
|
`http_requests{group="canary", instance="1", job="app-server"} => 800 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="0", job="api-server"} => 100 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="0", job="app-server"} => 500 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="1", job="api-server"} => 200 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="1", job="app-server"} => 600 @[%v]`,
|
|
|
|
`testcounter_reset_end => 0 @[%v]`,
|
|
|
|
`testcounter_reset_middle => 50 @[%v]`,
|
|
|
|
`x{y="testvalue"} => 100 @[%v]`,
|
2015-02-21 13:05:50 +00:00
|
|
|
`label_grouping_test{a="a", b="abb"} => 200 @[%v]`,
|
|
|
|
`label_grouping_test{a="aa", b="bb"} => 100 @[%v]`,
|
2014-10-14 15:21:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `{job=~"server", job!~"api"}`,
|
|
|
|
output: []string{
|
|
|
|
`http_requests{group="canary", instance="0", job="app-server"} => 700 @[%v]`,
|
|
|
|
`http_requests{group="canary", instance="1", job="app-server"} => 800 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="0", job="app-server"} => 500 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="1", job="app-server"} => 600 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
2014-10-22 09:03:11 +00:00
|
|
|
{
|
|
|
|
// Test alternative "by"-clause order.
|
|
|
|
expr: `sum by (group) (http_requests{job="api-server"})`,
|
|
|
|
output: []string{
|
2014-11-24 19:12:58 +00:00
|
|
|
`{group="canary"} => 700 @[%v]`,
|
|
|
|
`{group="production"} => 300 @[%v]`,
|
2014-10-22 09:03:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Test alternative "by"-clause order with "keeping_extra".
|
|
|
|
expr: `sum by (group) keeping_extra (http_requests{job="api-server"})`,
|
|
|
|
output: []string{
|
2014-11-24 19:12:58 +00:00
|
|
|
`{group="canary", job="api-server"} => 700 @[%v]`,
|
|
|
|
`{group="production", job="api-server"} => 300 @[%v]`,
|
2014-10-22 09:03:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Test both alternative "by"-clause orders in one expression.
|
|
|
|
// Public health warning: stick to one form within an expression (or even
|
|
|
|
// in an organization), or risk serious user confusion.
|
|
|
|
expr: `sum(sum by (group) keeping_extra (http_requests{job="api-server"})) by (job)`,
|
|
|
|
output: []string{
|
2014-11-24 19:12:58 +00:00
|
|
|
`{job="api-server"} => 1000 @[%v]`,
|
2014-10-22 09:03:11 +00:00
|
|
|
},
|
|
|
|
},
|
Add absent() function.
A common problem in Prometheus alerting is to detect when no timeseries
exist for a given metric name and label combination. Unfortunately,
Prometheus alert expressions need to be of vector type, and
"count(nonexistent_metric)" results in an empty vector, yielding no
output vector elements to base an alert on. The newly introduced
absent() function solves this issue:
ALERT FooAbsent IF absent(foo{job="myjob"}) [...]
absent() has the following behavior:
- if the vector passed to it has any elements, it returns an empty
vector.
- if the vector passed to it has no elements, it returns a 1-element
vector with the value 1.
In the second case, absent() tries to be smart about deriving labels of
the 1-element output vector from the input vector:
absent(nonexistent{job="myjob"}) => {job="myjob"}
absent(nonexistent{job="myjob",instance=~".*"}) => {job="myjob"}
absent(sum(nonexistent{job="myjob"})) => {}
That is, if the passed vector is a literal vector selector, it takes all
"=" label matchers as the basis for the output labels, but ignores all
non-equals or regex matchers. Also, if the passed vector results from a
non-selector expression, no labels can be derived.
Change-Id: I948505a1488d50265ab5692a3286bd7c8c70cd78
2014-10-15 17:13:52 +00:00
|
|
|
{
|
|
|
|
expr: `absent(nonexistent)`,
|
|
|
|
output: []string{
|
|
|
|
`{} => 1 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `absent(nonexistent{job="testjob", instance="testinstance", method=~".*"})`,
|
|
|
|
output: []string{
|
|
|
|
`{instance="testinstance", job="testjob"} => 1 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `count_scalar(absent(http_requests))`,
|
|
|
|
output: []string{
|
|
|
|
`scalar: 0 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `count_scalar(absent(sum(http_requests)))`,
|
|
|
|
output: []string{
|
|
|
|
`scalar: 0 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `absent(sum(nonexistent{job="testjob", instance="testinstance"}))`,
|
|
|
|
output: []string{
|
|
|
|
`{} => 1 @[%v]`,
|
|
|
|
},
|
2015-02-18 01:30:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `http_requests{group="production",job="api-server"} offset 5m`,
|
|
|
|
output: []string{
|
|
|
|
`http_requests{group="production", instance="0", job="api-server"} => 90 @[%v]`,
|
|
|
|
`http_requests{group="production", instance="1", job="api-server"} => 180 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `rate(http_requests{group="production",job="api-server"}[10m] offset 5m)`,
|
|
|
|
output: []string{
|
|
|
|
`{group="production", instance="0", job="api-server"} => 0.03333333333333333 @[%v]`,
|
|
|
|
`{group="production", instance="1", job="api-server"} => 0.06666666666666667 @[%v]`,
|
|
|
|
},
|
Add absent() function.
A common problem in Prometheus alerting is to detect when no timeseries
exist for a given metric name and label combination. Unfortunately,
Prometheus alert expressions need to be of vector type, and
"count(nonexistent_metric)" results in an empty vector, yielding no
output vector elements to base an alert on. The newly introduced
absent() function solves this issue:
ALERT FooAbsent IF absent(foo{job="myjob"}) [...]
absent() has the following behavior:
- if the vector passed to it has any elements, it returns an empty
vector.
- if the vector passed to it has no elements, it returns a 1-element
vector with the value 1.
In the second case, absent() tries to be smart about deriving labels of
the 1-element output vector from the input vector:
absent(nonexistent{job="myjob"}) => {job="myjob"}
absent(nonexistent{job="myjob",instance=~".*"}) => {job="myjob"}
absent(sum(nonexistent{job="myjob"})) => {}
That is, if the passed vector is a literal vector selector, it takes all
"=" label matchers as the basis for the output labels, but ignores all
non-equals or regex matchers. Also, if the passed vector results from a
non-selector expression, no labels can be derived.
Change-Id: I948505a1488d50265ab5692a3286bd7c8c70cd78
2014-10-15 17:13:52 +00:00
|
|
|
},
|
2015-02-18 01:56:40 +00:00
|
|
|
{
|
|
|
|
expr: `rate(http_requests[10m]) offset 5m`,
|
|
|
|
shouldFail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expr: `sum(http_requests) offset 5m`,
|
|
|
|
shouldFail: true,
|
|
|
|
},
|
2015-02-21 13:05:50 +00:00
|
|
|
// Regression test for missing separator byte in labelsToGroupingKey.
|
|
|
|
{
|
|
|
|
expr: `sum(label_grouping_test) by (a, b)`,
|
|
|
|
output: []string{
|
|
|
|
`{a="a", b="abb"} => 200 @[%v]`,
|
|
|
|
`{a="aa", b="bb"} => 100 @[%v]`,
|
|
|
|
},
|
|
|
|
},
|
2013-04-24 09:51:40 +00:00
|
|
|
}
|
2013-03-21 17:06:15 +00:00
|
|
|
|
2014-06-06 09:55:53 +00:00
|
|
|
storage, closer := newTestStorage(t)
|
2013-04-24 09:51:40 +00:00
|
|
|
defer closer.Close()
|
2013-01-07 22:24:26 +00:00
|
|
|
|
2013-04-10 08:44:13 +00:00
|
|
|
for i, exprTest := range expressionTests {
|
2013-04-24 09:51:40 +00:00
|
|
|
expectedLines := annotateWithTime(exprTest.output, testEvalTime)
|
2013-01-07 22:24:26 +00:00
|
|
|
|
2013-01-11 00:17:37 +00:00
|
|
|
testExpr, err := LoadExprFromString(exprTest.expr)
|
2013-01-07 22:24:26 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2013-01-11 00:17:37 +00:00
|
|
|
if exprTest.shouldFail {
|
2013-01-07 22:24:26 +00:00
|
|
|
continue
|
|
|
|
}
|
2013-04-24 09:51:40 +00:00
|
|
|
t.Errorf("%d. Error during parsing: %v", i, err)
|
|
|
|
t.Errorf("%d. Expression: %v", i, exprTest.expr)
|
2013-01-07 22:24:26 +00:00
|
|
|
} else {
|
2013-03-21 17:06:15 +00:00
|
|
|
if exprTest.shouldFail {
|
2013-04-24 09:51:40 +00:00
|
|
|
t.Errorf("%d. Test should fail, but didn't", i)
|
2013-03-21 17:06:15 +00:00
|
|
|
}
|
2013-01-07 22:24:26 +00:00
|
|
|
failed := false
|
2014-12-25 00:28:35 +00:00
|
|
|
resultStr := ast.EvalToString(testExpr, testEvalTime, ast.Text, storage, stats.NewTimerGroup())
|
2013-01-07 22:24:26 +00:00
|
|
|
resultLines := strings.Split(resultStr, "\n")
|
|
|
|
|
2013-01-11 00:17:37 +00:00
|
|
|
if len(exprTest.output) != len(resultLines) {
|
2013-04-24 09:51:40 +00:00
|
|
|
t.Errorf("%d. Number of samples in expected and actual output don't match", i)
|
2013-01-07 22:24:26 +00:00
|
|
|
failed = true
|
|
|
|
}
|
2013-04-10 08:44:13 +00:00
|
|
|
|
|
|
|
if exprTest.checkOrder {
|
|
|
|
for j, expectedSample := range expectedLines {
|
|
|
|
if resultLines[j] != expectedSample {
|
2013-04-24 09:51:40 +00:00
|
|
|
t.Errorf("%d.%d. Expected sample '%v', got '%v'", i, j, resultLines[j], expectedSample)
|
2013-04-10 08:44:13 +00:00
|
|
|
failed = true
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-10 08:44:13 +00:00
|
|
|
} else {
|
|
|
|
for j, expectedSample := range expectedLines {
|
|
|
|
found := false
|
|
|
|
for _, actualSample := range resultLines {
|
|
|
|
if actualSample == expectedSample {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2013-04-24 09:51:40 +00:00
|
|
|
t.Errorf("%d.%d. Couldn't find expected sample in output: '%v'", i, j, expectedSample)
|
2013-04-10 08:44:13 +00:00
|
|
|
failed = true
|
|
|
|
}
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-21 17:06:15 +00:00
|
|
|
|
2013-01-07 22:24:26 +00:00
|
|
|
if failed {
|
2013-04-24 09:51:40 +00:00
|
|
|
t.Errorf("%d. Expression: %v\n%v", i, exprTest.expr, vectorComparisonString(expectedLines, resultLines))
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-22 22:26:59 +00:00
|
|
|
|
2014-12-12 19:01:32 +00:00
|
|
|
func TestRangedEvaluationRegressions(t *testing.T) {
|
|
|
|
scenarios := []struct {
|
|
|
|
in ast.Matrix
|
|
|
|
out ast.Matrix
|
|
|
|
expr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// Testing COWMetric behavior in drop_common_labels.
|
|
|
|
in: ast.Matrix{
|
|
|
|
{
|
|
|
|
Metric: clientmodel.COWMetric{
|
|
|
|
Metric: clientmodel.Metric{
|
|
|
|
clientmodel.MetricNameLabel: "testmetric",
|
|
|
|
"testlabel": "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Values: metric.Values{
|
|
|
|
{
|
|
|
|
Timestamp: testStartTime,
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamp: testStartTime.Add(time.Hour),
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Metric: clientmodel.COWMetric{
|
|
|
|
Metric: clientmodel.Metric{
|
|
|
|
clientmodel.MetricNameLabel: "testmetric",
|
|
|
|
"testlabel": "2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Values: metric.Values{
|
|
|
|
{
|
|
|
|
Timestamp: testStartTime.Add(time.Hour),
|
|
|
|
Value: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: ast.Matrix{
|
|
|
|
{
|
|
|
|
Metric: clientmodel.COWMetric{
|
|
|
|
Metric: clientmodel.Metric{
|
|
|
|
clientmodel.MetricNameLabel: "testmetric",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Values: metric.Values{
|
|
|
|
{
|
|
|
|
Timestamp: testStartTime,
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Metric: clientmodel.COWMetric{
|
|
|
|
Metric: clientmodel.Metric{
|
|
|
|
clientmodel.MetricNameLabel: "testmetric",
|
|
|
|
"testlabel": "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Values: metric.Values{
|
|
|
|
{
|
|
|
|
Timestamp: testStartTime.Add(time.Hour),
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Metric: clientmodel.COWMetric{
|
|
|
|
Metric: clientmodel.Metric{
|
|
|
|
clientmodel.MetricNameLabel: "testmetric",
|
|
|
|
"testlabel": "2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Values: metric.Values{
|
|
|
|
{
|
|
|
|
Timestamp: testStartTime.Add(time.Hour),
|
|
|
|
Value: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expr: "drop_common_labels(testmetric)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Testing COWMetric behavior in vector aggregation.
|
|
|
|
in: ast.Matrix{
|
|
|
|
{
|
|
|
|
Metric: clientmodel.COWMetric{
|
|
|
|
Metric: clientmodel.Metric{
|
|
|
|
clientmodel.MetricNameLabel: "testmetric",
|
|
|
|
"testlabel": "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Values: metric.Values{
|
|
|
|
{
|
|
|
|
Timestamp: testStartTime,
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamp: testStartTime.Add(time.Hour),
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Metric: clientmodel.COWMetric{
|
|
|
|
Metric: clientmodel.Metric{
|
|
|
|
clientmodel.MetricNameLabel: "testmetric",
|
|
|
|
"testlabel": "2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Values: metric.Values{
|
|
|
|
{
|
|
|
|
Timestamp: testStartTime,
|
|
|
|
Value: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: ast.Matrix{
|
|
|
|
{
|
|
|
|
Metric: clientmodel.COWMetric{
|
|
|
|
Metric: clientmodel.Metric{},
|
|
|
|
},
|
|
|
|
Values: metric.Values{
|
|
|
|
{
|
|
|
|
Timestamp: testStartTime,
|
|
|
|
Value: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Metric: clientmodel.COWMetric{
|
|
|
|
Metric: clientmodel.Metric{
|
|
|
|
"testlabel": "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Values: metric.Values{
|
|
|
|
{
|
|
|
|
Timestamp: testStartTime.Add(time.Hour),
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expr: "sum(testmetric) keeping_extra",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, s := range scenarios {
|
|
|
|
storage, closer := local.NewTestStorage(t)
|
|
|
|
storeMatrix(storage, s.in)
|
|
|
|
|
|
|
|
expr, err := LoadExprFromString(s.expr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%d. Error parsing expression: %v", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := ast.EvalVectorRange(
|
|
|
|
expr.(ast.VectorNode),
|
|
|
|
testStartTime,
|
|
|
|
testStartTime.Add(time.Hour),
|
|
|
|
time.Hour,
|
|
|
|
storage,
|
|
|
|
stats.NewTimerGroup(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%d. Error evaluating expression: %v", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got.String() != s.out.String() {
|
|
|
|
t.Fatalf("%d. Expression: %s\n\ngot:\n=====\n%v\n====\n\nwant:\n=====\n%v\n=====\n", i, s.expr, got.String(), s.out.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
closer.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-22 22:26:59 +00:00
|
|
|
var ruleTests = []struct {
|
|
|
|
inputFile string
|
|
|
|
shouldFail bool
|
|
|
|
errContains string
|
|
|
|
numRecordingRules int
|
|
|
|
numAlertingRules int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
inputFile: "empty.rules",
|
|
|
|
numRecordingRules: 0,
|
|
|
|
numAlertingRules: 0,
|
|
|
|
}, {
|
|
|
|
inputFile: "mixed.rules",
|
|
|
|
numRecordingRules: 2,
|
|
|
|
numAlertingRules: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inputFile: "syntax_error.rules",
|
|
|
|
shouldFail: true,
|
2013-07-11 16:38:44 +00:00
|
|
|
errContains: "Error parsing rules at line 5",
|
2013-04-22 22:26:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
inputFile: "non_vector.rules",
|
|
|
|
shouldFail: true,
|
|
|
|
errContains: "does not evaluate to vector type",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRules(t *testing.T) {
|
|
|
|
for i, ruleTest := range ruleTests {
|
|
|
|
testRules, err := LoadRulesFromFile(path.Join(fixturesPath, ruleTest.inputFile))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if !ruleTest.shouldFail {
|
|
|
|
t.Fatalf("%d. Error parsing rules file %v: %v", i, ruleTest.inputFile, err)
|
|
|
|
} else {
|
|
|
|
if !strings.Contains(err.Error(), ruleTest.errContains) {
|
|
|
|
t.Fatalf("%d. Expected error containing '%v', got: %v", i, ruleTest.errContains, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
numRecordingRules := 0
|
|
|
|
numAlertingRules := 0
|
|
|
|
|
|
|
|
for j, rule := range testRules {
|
|
|
|
switch rule.(type) {
|
|
|
|
case *RecordingRule:
|
|
|
|
numRecordingRules++
|
|
|
|
case *AlertingRule:
|
|
|
|
numAlertingRules++
|
|
|
|
default:
|
|
|
|
t.Fatalf("%d.%d. Unknown rule type!", i, j)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if numRecordingRules != ruleTest.numRecordingRules {
|
|
|
|
t.Fatalf("%d. Expected %d recording rules, got %d", i, ruleTest.numRecordingRules, numRecordingRules)
|
|
|
|
}
|
|
|
|
if numAlertingRules != ruleTest.numAlertingRules {
|
|
|
|
t.Fatalf("%d. Expected %d alerting rules, got %d", i, ruleTest.numAlertingRules, numAlertingRules)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(julius): add more complex checks on the parsed rules here.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 */
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-06-06 09:55:53 +00:00
|
|
|
storage, closer := newTestStorage(t)
|
2013-04-24 09:51:40 +00:00
|
|
|
defer closer.Close()
|
|
|
|
|
2013-06-11 09:01:36 +00:00
|
|
|
alertExpr, err := LoadExprFromString(`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)
|
|
|
|
}
|
|
|
|
alertName := "HttpRequestRateLow"
|
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
|
|
|
}
|
2013-07-30 15:18:07 +00:00
|
|
|
rule := NewAlertingRule(alertName, alertExpr.(ast.VectorNode), time.Minute, alertLabels, "summary", "description")
|
2013-04-24 09:51:40 +00:00
|
|
|
|
|
|
|
for i, expected := range evalOutputs {
|
|
|
|
evalTime := testStartTime.Add(testSampleInterval * time.Duration(i))
|
2014-06-06 09:55:53 +00:00
|
|
|
actual, err := rule.Eval(evalTime, storage)
|
2013-04-24 09:51:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error during alerting rule evaluation: %s", err)
|
|
|
|
}
|
|
|
|
actualLines := strings.Split(actual.String(), "\n")
|
|
|
|
expectedLines := annotateWithTime(expected, evalTime)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|