Add increase() function, to replace delta(..., 1).

This calculates how much a counter increases over
a given period of time, which is the area under the curve
of it's rate.

increase(x[5m]) is equivilent to rate(x[5m]) * 300.
This commit is contained in:
Brian Brazil 2015-05-24 19:48:56 +01:00
parent 7b5304850d
commit f34de493d5
5 changed files with 30 additions and 4 deletions

View file

@ -118,6 +118,13 @@ func funcRate(ev *evaluator, args Expressions) Value {
return vector return vector
} }
// === increase(node ExprMatrix) Vector ===
func funcIncrease(ev *evaluator, args Expressions) Value {
args = append(args, &NumberLiteral{1})
vector := funcDelta(ev, args).(Vector)
return vector
}
// === sort(node ExprVector) Vector === // === sort(node ExprVector) Vector ===
func funcSort(ev *evaluator, args Expressions) Value { func funcSort(ev *evaluator, args Expressions) Value {
byValueSorter := vectorByValueHeap(ev.evalVector(args[0])) byValueSorter := vectorByValueHeap(ev.evalVector(args[0]))
@ -572,6 +579,12 @@ var functions = map[string]*Function{
ReturnType: ExprVector, ReturnType: ExprVector,
Call: funcAbsent, Call: funcAbsent,
}, },
"increase": {
Name: "increase",
ArgTypes: []ExprType{ExprMatrix},
ReturnType: ExprVector,
Call: funcIncrease,
},
"avg_over_time": { "avg_over_time": {
Name: "avg_over_time", Name: "avg_over_time",
ArgTypes: []ExprType{ExprMatrix}, ArgTypes: []ExprType{ExprMatrix},

View file

@ -25,8 +25,8 @@ import (
// excludedLabels are the labels to exclude from signature calculation for // excludedLabels are the labels to exclude from signature calculation for
// quantiles. // quantiles.
var excludedLabels = map[clientmodel.LabelName]struct{}{ var excludedLabels = map[clientmodel.LabelName]struct{}{
clientmodel.MetricNameLabel: struct{}{}, clientmodel.MetricNameLabel: {},
clientmodel.BucketLabel: struct{}{}, clientmodel.BucketLabel: {},
} }
type bucket struct { type bucket struct {

View file

@ -49,3 +49,16 @@ eval instant at 50m changes(http_requests[50m])
{path="/biz"} 1 {path="/biz"} 1
eval instant at 50m changes(nonexistent_metric[50m]) eval instant at 50m changes(nonexistent_metric[50m])
clear
# Tests for increase().
load 5m
http_requests{path="/foo"} 0+10x10
http_requests{path="/bar"} 0+10x5 0+10x5
# Tests for increase().
eval instant at 50m increase(http_requests[50m])
{path="/foo"} 100
{path="/bar"} 90

View file

@ -310,7 +310,7 @@ func (tm *TargetManager) targetsFromGroup(tg *config.TargetGroup, cfg *config.Sc
// set already. Apply the labelsets in order of decreasing precedence. // set already. Apply the labelsets in order of decreasing precedence.
labelsets := []clientmodel.LabelSet{ labelsets := []clientmodel.LabelSet{
tg.Labels, tg.Labels,
clientmodel.LabelSet{ {
clientmodel.MetricsPathLabel: clientmodel.LabelValue(cfg.MetricsPath), clientmodel.MetricsPathLabel: clientmodel.LabelValue(cfg.MetricsPath),
clientmodel.JobLabel: clientmodel.LabelValue(cfg.JobName), clientmodel.JobLabel: clientmodel.LabelValue(cfg.JobName),
}, },

View file

@ -43,7 +43,7 @@ func TestTargetManagerChan(t *testing.T) {
targetManager := &TargetManager{ targetManager := &TargetManager{
sampleAppender: nopAppender{}, sampleAppender: nopAppender{},
providers: map[*config.ScrapeConfig][]TargetProvider{ providers: map[*config.ScrapeConfig][]TargetProvider{
testJob1: []TargetProvider{prov1}, testJob1: {prov1},
}, },
targets: make(map[string][]*Target), targets: make(map[string][]*Target),
} }