mirror of
https://github.com/prometheus/prometheus.git
synced 2025-02-21 03:16:00 -08:00
Implement changes() function.
changes() takes a range vector and returns the number of times a value has changed in the given time window for each time series as an instant vector.
This commit is contained in:
parent
c00334d560
commit
d44a89c6e8
|
@ -532,6 +532,33 @@ func funcResets(ev *evaluator, args Expressions) Value {
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// === changes(matrix ExprMatrix) Vector ===
|
||||||
|
func funcChanges(ev *evaluator, args Expressions) Value {
|
||||||
|
in := ev.evalMatrix(args[0])
|
||||||
|
out := make(Vector, 0, len(in))
|
||||||
|
|
||||||
|
for _, samples := range in {
|
||||||
|
changes := 0
|
||||||
|
prev := clientmodel.SampleValue(samples.Values[0].Value)
|
||||||
|
for _, sample := range samples.Values[1:] {
|
||||||
|
current := sample.Value
|
||||||
|
if current != prev {
|
||||||
|
changes++
|
||||||
|
}
|
||||||
|
prev = current
|
||||||
|
}
|
||||||
|
|
||||||
|
rs := &Sample{
|
||||||
|
Metric: samples.Metric,
|
||||||
|
Value: clientmodel.SampleValue(changes),
|
||||||
|
Timestamp: ev.Timestamp,
|
||||||
|
}
|
||||||
|
rs.Metric.Delete(clientmodel.MetricNameLabel)
|
||||||
|
out = append(out, rs)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
var functions = map[string]*Function{
|
var functions = map[string]*Function{
|
||||||
"abs": {
|
"abs": {
|
||||||
Name: "abs",
|
Name: "abs",
|
||||||
|
@ -563,6 +590,12 @@ var functions = map[string]*Function{
|
||||||
ReturnType: ExprVector,
|
ReturnType: ExprVector,
|
||||||
Call: funcCeil,
|
Call: funcCeil,
|
||||||
},
|
},
|
||||||
|
"changes": {
|
||||||
|
Name: "changes",
|
||||||
|
ArgTypes: []ExprType{ExprMatrix},
|
||||||
|
ReturnType: ExprVector,
|
||||||
|
Call: funcChanges,
|
||||||
|
},
|
||||||
"count_over_time": {
|
"count_over_time": {
|
||||||
Name: "count_over_time",
|
Name: "count_over_time",
|
||||||
ArgTypes: []ExprType{ExprMatrix},
|
ArgTypes: []ExprType{ExprMatrix},
|
||||||
|
|
30
promql/testdata/functions.test
vendored
30
promql/testdata/functions.test
vendored
|
@ -1,21 +1,51 @@
|
||||||
|
# Testdata for resets() and changes().
|
||||||
load 5m
|
load 5m
|
||||||
http_requests{path="/foo"} 1 2 3 0 1 0 0 1 2 0
|
http_requests{path="/foo"} 1 2 3 0 1 0 0 1 2 0
|
||||||
http_requests{path="/bar"} 1 2 3 4 5 1 2 3 4 5
|
http_requests{path="/bar"} 1 2 3 4 5 1 2 3 4 5
|
||||||
|
http_requests{path="/biz"} 0 0 0 0 0 1 1 1 1 1
|
||||||
|
|
||||||
|
# Tests for resets().
|
||||||
eval instant at 50m resets(http_requests[5m])
|
eval instant at 50m resets(http_requests[5m])
|
||||||
{path="/foo"} 0
|
{path="/foo"} 0
|
||||||
{path="/bar"} 0
|
{path="/bar"} 0
|
||||||
|
{path="/biz"} 0
|
||||||
|
|
||||||
eval instant at 50m resets(http_requests[20m])
|
eval instant at 50m resets(http_requests[20m])
|
||||||
{path="/foo"} 1
|
{path="/foo"} 1
|
||||||
{path="/bar"} 0
|
{path="/bar"} 0
|
||||||
|
{path="/biz"} 0
|
||||||
|
|
||||||
eval instant at 50m resets(http_requests[30m])
|
eval instant at 50m resets(http_requests[30m])
|
||||||
{path="/foo"} 2
|
{path="/foo"} 2
|
||||||
{path="/bar"} 1
|
{path="/bar"} 1
|
||||||
|
{path="/biz"} 0
|
||||||
|
|
||||||
eval instant at 50m resets(http_requests[50m])
|
eval instant at 50m resets(http_requests[50m])
|
||||||
{path="/foo"} 3
|
{path="/foo"} 3
|
||||||
{path="/bar"} 1
|
{path="/bar"} 1
|
||||||
|
{path="/biz"} 0
|
||||||
|
|
||||||
eval instant at 50m resets(nonexistent_metric[50m])
|
eval instant at 50m resets(nonexistent_metric[50m])
|
||||||
|
|
||||||
|
# Tests for changes().
|
||||||
|
eval instant at 50m changes(http_requests[5m])
|
||||||
|
{path="/foo"} 0
|
||||||
|
{path="/bar"} 0
|
||||||
|
{path="/biz"} 0
|
||||||
|
|
||||||
|
eval instant at 50m changes(http_requests[20m])
|
||||||
|
{path="/foo"} 3
|
||||||
|
{path="/bar"} 3
|
||||||
|
{path="/biz"} 0
|
||||||
|
|
||||||
|
eval instant at 50m changes(http_requests[30m])
|
||||||
|
{path="/foo"} 4
|
||||||
|
{path="/bar"} 5
|
||||||
|
{path="/biz"} 1
|
||||||
|
|
||||||
|
eval instant at 50m changes(http_requests[50m])
|
||||||
|
{path="/foo"} 8
|
||||||
|
{path="/bar"} 9
|
||||||
|
{path="/biz"} 1
|
||||||
|
|
||||||
|
eval instant at 50m changes(nonexistent_metric[50m])
|
||||||
|
|
Loading…
Reference in a new issue