mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-09 23:24:05 -08:00
add idelta function
similar to the irate function the idelta function calculates the delta function with the last two values
This commit is contained in:
parent
0ce5e7fe6d
commit
dbf83666bb
|
@ -183,6 +183,37 @@ func funcIrate(ev *evaluator, args Expressions) model.Value {
|
|||
return resultVector
|
||||
}
|
||||
|
||||
// === idelta(node model.ValMatric) Vector ===
|
||||
func funcIdelta(ev *evaluator, args Expressions) model.Value {
|
||||
resultVector := vector{}
|
||||
for _, samples := range ev.evalMatrix(args[0]) {
|
||||
// No sense in trying to compute a rate without at least two points. Drop
|
||||
// this vector element.
|
||||
if len(samples.Values) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
lastSample := samples.Values[len(samples.Values)-1]
|
||||
previousSample := samples.Values[len(samples.Values)-2]
|
||||
resultValue := lastSample.Value - previousSample.Value
|
||||
|
||||
sampledInterval := lastSample.Timestamp.Sub(previousSample.Timestamp)
|
||||
if sampledInterval == 0 {
|
||||
// Avoid dividing by 0.
|
||||
continue
|
||||
}
|
||||
|
||||
resultSample := &sample{
|
||||
Metric: samples.Metric,
|
||||
Value: resultValue,
|
||||
Timestamp: ev.Timestamp,
|
||||
}
|
||||
resultSample.Metric.Del(model.MetricNameLabel)
|
||||
resultVector = append(resultVector, resultSample)
|
||||
}
|
||||
return resultVector
|
||||
}
|
||||
|
||||
// Calculate the trend value at the given index i in raw data d.
|
||||
// This is somewhat analogous to the slope of the trend at the given index.
|
||||
// The argument "s" is the set of computed smoothed values.
|
||||
|
@ -956,6 +987,12 @@ var functions = map[string]*Function{
|
|||
ReturnType: model.ValVector,
|
||||
Call: funcIrate,
|
||||
},
|
||||
"idelta": {
|
||||
Name: "idelta",
|
||||
ArgTypes: []model.ValueType{model.ValMatrix},
|
||||
ReturnType: model.ValVector,
|
||||
Call: funcIdelta,
|
||||
},
|
||||
"label_replace": {
|
||||
Name: "label_replace",
|
||||
ArgTypes: []model.ValueType{model.ValVector, model.ValString, model.ValString, model.ValString, model.ValString},
|
||||
|
|
13
promql/testdata/functions.test
vendored
13
promql/testdata/functions.test
vendored
|
@ -88,9 +88,22 @@ clear
|
|||
# Tests for delta().
|
||||
load 5m
|
||||
http_requests{path="/foo"} 0 50 100 150 200
|
||||
http_requests{path="/bar"} 200 150 100 50 0
|
||||
|
||||
eval instant at 20m delta(http_requests[20m])
|
||||
{path="/foo"} 200
|
||||
{path="/bar"} -200
|
||||
|
||||
clear
|
||||
|
||||
# Tests for idelta().
|
||||
load 5m
|
||||
http_requests{path="/foo"} 0 50 100 150
|
||||
http_requests{path="/bar"} 0 50 100 50
|
||||
|
||||
eval instant at 20m idelta(http_requests[20m])
|
||||
{path="/foo"} 50
|
||||
{path="/bar"} -50
|
||||
|
||||
clear
|
||||
|
||||
|
|
Loading…
Reference in a new issue