mirror of
https://github.com/prometheus/prometheus
synced 2024-12-26 08:33:06 +00:00
Cleanup PromQL functions (#6551)
* Cleanup PromQL functions The engine ensures, for Matrix functions, that functions are called with exactly one series at the time. Therefore a lot of code can be inlined and we can directly assume the first element of the arguments exists and contains all the samples needed. Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
This commit is contained in:
parent
536d416299
commit
577e738986
@ -66,16 +66,15 @@ func extrapolatedRate(vals []Value, args Expressions, enh *EvalNodeHelper, isCou
|
|||||||
ms := args[0].(*MatrixSelector)
|
ms := args[0].(*MatrixSelector)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
matrix = vals[0].(Matrix)
|
samples = vals[0].(Matrix)[0]
|
||||||
rangeStart = enh.ts - durationMilliseconds(ms.Range+ms.Offset)
|
rangeStart = enh.ts - durationMilliseconds(ms.Range+ms.Offset)
|
||||||
rangeEnd = enh.ts - durationMilliseconds(ms.Offset)
|
rangeEnd = enh.ts - durationMilliseconds(ms.Offset)
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, samples := range matrix {
|
|
||||||
// No sense in trying to compute a rate without at least two points. Drop
|
// No sense in trying to compute a rate without at least two points. Drop
|
||||||
// this Vector element.
|
// this Vector element.
|
||||||
if len(samples.Points) < 2 {
|
if len(samples.Points) < 2 {
|
||||||
continue
|
return enh.out
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
counterCorrection float64
|
counterCorrection float64
|
||||||
@ -132,12 +131,10 @@ func extrapolatedRate(vals []Value, args Expressions, enh *EvalNodeHelper, isCou
|
|||||||
resultValue = resultValue / ms.Range.Seconds()
|
resultValue = resultValue / ms.Range.Seconds()
|
||||||
}
|
}
|
||||||
|
|
||||||
enh.out = append(enh.out, Sample{
|
return append(enh.out, Sample{
|
||||||
Point: Point{V: resultValue},
|
Point: Point{V: resultValue},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return enh.out
|
|
||||||
}
|
|
||||||
|
|
||||||
// === delta(Matrix ValueTypeMatrix) Vector ===
|
// === delta(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcDelta(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
func funcDelta(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
||||||
@ -165,11 +162,11 @@ func funcIdelta(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func instantValue(vals []Value, out Vector, isRate bool) Vector {
|
func instantValue(vals []Value, out Vector, isRate bool) Vector {
|
||||||
for _, samples := range vals[0].(Matrix) {
|
samples := vals[0].(Matrix)[0]
|
||||||
// No sense in trying to compute a rate without at least two points. Drop
|
// No sense in trying to compute a rate without at least two points. Drop
|
||||||
// this Vector element.
|
// this Vector element.
|
||||||
if len(samples.Points) < 2 {
|
if len(samples.Points) < 2 {
|
||||||
continue
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
lastSample := samples.Points[len(samples.Points)-1]
|
lastSample := samples.Points[len(samples.Points)-1]
|
||||||
@ -186,7 +183,7 @@ func instantValue(vals []Value, out Vector, isRate bool) Vector {
|
|||||||
sampledInterval := lastSample.T - previousSample.T
|
sampledInterval := lastSample.T - previousSample.T
|
||||||
if sampledInterval == 0 {
|
if sampledInterval == 0 {
|
||||||
// Avoid dividing by 0.
|
// Avoid dividing by 0.
|
||||||
continue
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
if isRate {
|
if isRate {
|
||||||
@ -194,12 +191,10 @@ func instantValue(vals []Value, out Vector, isRate bool) Vector {
|
|||||||
resultValue /= float64(sampledInterval) / 1000
|
resultValue /= float64(sampledInterval) / 1000
|
||||||
}
|
}
|
||||||
|
|
||||||
out = append(out, Sample{
|
return append(out, Sample{
|
||||||
Point: Point{V: resultValue},
|
Point: Point{V: resultValue},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate the trend value at the given index i in raw data d.
|
// 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.
|
// This is somewhat analogous to the slope of the trend at the given index.
|
||||||
@ -223,7 +218,7 @@ func calcTrendValue(i int, sf, tf, s0, s1, b float64) float64 {
|
|||||||
// how trends in historical data will affect the current data. A higher trend factor increases the influence.
|
// how trends in historical data will affect the current data. A higher trend factor increases the influence.
|
||||||
// of trends. Algorithm taken from https://en.wikipedia.org/wiki/Exponential_smoothing titled: "Double exponential smoothing".
|
// of trends. Algorithm taken from https://en.wikipedia.org/wiki/Exponential_smoothing titled: "Double exponential smoothing".
|
||||||
func funcHoltWinters(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
func funcHoltWinters(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
||||||
mat := vals[0].(Matrix)
|
samples := vals[0].(Matrix)[0]
|
||||||
|
|
||||||
// The smoothing factor argument.
|
// The smoothing factor argument.
|
||||||
sf := vals[1].(Vector)[0].V
|
sf := vals[1].(Vector)[0].V
|
||||||
@ -239,13 +234,11 @@ func funcHoltWinters(vals []Value, args Expressions, enh *EvalNodeHelper) Vector
|
|||||||
panic(errors.Errorf("invalid trend factor. Expected: 0 < tf < 1, got: %f", tf))
|
panic(errors.Errorf("invalid trend factor. Expected: 0 < tf < 1, got: %f", tf))
|
||||||
}
|
}
|
||||||
|
|
||||||
var l int
|
l := len(samples.Points)
|
||||||
for _, samples := range mat {
|
|
||||||
l = len(samples.Points)
|
|
||||||
|
|
||||||
// Can't do the smoothing operation with less than two points.
|
// Can't do the smoothing operation with less than two points.
|
||||||
if l < 2 {
|
if l < 2 {
|
||||||
continue
|
return enh.out
|
||||||
}
|
}
|
||||||
|
|
||||||
var s0, s1, b float64
|
var s0, s1, b float64
|
||||||
@ -267,14 +260,11 @@ func funcHoltWinters(vals []Value, args Expressions, enh *EvalNodeHelper) Vector
|
|||||||
s0, s1 = s1, x+y
|
s0, s1 = s1, x+y
|
||||||
}
|
}
|
||||||
|
|
||||||
enh.out = append(enh.out, Sample{
|
return append(enh.out, Sample{
|
||||||
Point: Point{V: s1},
|
Point: Point{V: s1},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return enh.out
|
|
||||||
}
|
|
||||||
|
|
||||||
// === sort(node ValueTypeVector) Vector ===
|
// === sort(node ValueTypeVector) Vector ===
|
||||||
func funcSort(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
func funcSort(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
||||||
// NaN should sort to the bottom, so take descending sort with NaN first and
|
// NaN should sort to the bottom, so take descending sort with NaN first and
|
||||||
@ -355,19 +345,12 @@ func funcScalar(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func aggrOverTime(vals []Value, enh *EvalNodeHelper, aggrFn func([]Point) float64) Vector {
|
func aggrOverTime(vals []Value, enh *EvalNodeHelper, aggrFn func([]Point) float64) Vector {
|
||||||
mat := vals[0].(Matrix)
|
el := vals[0].(Matrix)[0]
|
||||||
|
|
||||||
for _, el := range mat {
|
return append(enh.out, Sample{
|
||||||
if len(el.Points) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
enh.out = append(enh.out, Sample{
|
|
||||||
Point: Point{V: aggrFn(el.Points)},
|
Point: Point{V: aggrFn(el.Points)},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return enh.out
|
|
||||||
}
|
|
||||||
|
|
||||||
// === avg_over_time(Matrix ValueTypeMatrix) Vector ===
|
// === avg_over_time(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcAvgOverTime(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
func funcAvgOverTime(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
||||||
@ -429,23 +412,16 @@ func funcSumOverTime(vals []Value, args Expressions, enh *EvalNodeHelper) Vector
|
|||||||
// === quantile_over_time(Matrix ValueTypeMatrix) Vector ===
|
// === quantile_over_time(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcQuantileOverTime(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
func funcQuantileOverTime(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
||||||
q := vals[0].(Vector)[0].V
|
q := vals[0].(Vector)[0].V
|
||||||
mat := vals[1].(Matrix)
|
el := vals[1].(Matrix)[0]
|
||||||
|
|
||||||
for _, el := range mat {
|
|
||||||
if len(el.Points) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
values := make(vectorByValueHeap, 0, len(el.Points))
|
values := make(vectorByValueHeap, 0, len(el.Points))
|
||||||
for _, v := range el.Points {
|
for _, v := range el.Points {
|
||||||
values = append(values, Sample{Point: Point{V: v.V}})
|
values = append(values, Sample{Point: Point{V: v.V}})
|
||||||
}
|
}
|
||||||
enh.out = append(enh.out, Sample{
|
return append(enh.out, Sample{
|
||||||
Point: Point{V: quantile(q, values)},
|
Point: Point{V: quantile(q, values)},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return enh.out
|
|
||||||
}
|
|
||||||
|
|
||||||
// === stddev_over_time(Matrix ValueTypeMatrix) Vector ===
|
// === stddev_over_time(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcStddevOverTime(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
func funcStddevOverTime(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
||||||
@ -588,45 +564,39 @@ func linearRegression(samples []Point, interceptTime int64) (slope, intercept fl
|
|||||||
|
|
||||||
// === deriv(node ValueTypeMatrix) Vector ===
|
// === deriv(node ValueTypeMatrix) Vector ===
|
||||||
func funcDeriv(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
func funcDeriv(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
||||||
mat := vals[0].(Matrix)
|
samples := vals[0].(Matrix)[0]
|
||||||
|
|
||||||
for _, samples := range mat {
|
|
||||||
// No sense in trying to compute a derivative without at least two points.
|
// No sense in trying to compute a derivative without at least two points.
|
||||||
// Drop this Vector element.
|
// Drop this Vector element.
|
||||||
if len(samples.Points) < 2 {
|
if len(samples.Points) < 2 {
|
||||||
continue
|
return enh.out
|
||||||
}
|
}
|
||||||
|
|
||||||
// We pass in an arbitrary timestamp that is near the values in use
|
// We pass in an arbitrary timestamp that is near the values in use
|
||||||
// to avoid floating point accuracy issues, see
|
// to avoid floating point accuracy issues, see
|
||||||
// https://github.com/prometheus/prometheus/issues/2674
|
// https://github.com/prometheus/prometheus/issues/2674
|
||||||
slope, _ := linearRegression(samples.Points, samples.Points[0].T)
|
slope, _ := linearRegression(samples.Points, samples.Points[0].T)
|
||||||
enh.out = append(enh.out, Sample{
|
return append(enh.out, Sample{
|
||||||
Point: Point{V: slope},
|
Point: Point{V: slope},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return enh.out
|
|
||||||
}
|
|
||||||
|
|
||||||
// === predict_linear(node ValueTypeMatrix, k ValueTypeScalar) Vector ===
|
// === predict_linear(node ValueTypeMatrix, k ValueTypeScalar) Vector ===
|
||||||
func funcPredictLinear(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
func funcPredictLinear(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
||||||
mat := vals[0].(Matrix)
|
samples := vals[0].(Matrix)[0]
|
||||||
duration := vals[1].(Vector)[0].V
|
duration := vals[1].(Vector)[0].V
|
||||||
|
|
||||||
for _, samples := range mat {
|
|
||||||
// No sense in trying to predict anything without at least two points.
|
// No sense in trying to predict anything without at least two points.
|
||||||
// Drop this Vector element.
|
// Drop this Vector element.
|
||||||
if len(samples.Points) < 2 {
|
if len(samples.Points) < 2 {
|
||||||
continue
|
return enh.out
|
||||||
}
|
}
|
||||||
slope, intercept := linearRegression(samples.Points, enh.ts)
|
slope, intercept := linearRegression(samples.Points, enh.ts)
|
||||||
|
|
||||||
enh.out = append(enh.out, Sample{
|
return append(enh.out, Sample{
|
||||||
Point: Point{V: slope*duration + intercept},
|
Point: Point{V: slope*duration + intercept},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return enh.out
|
|
||||||
}
|
|
||||||
|
|
||||||
// === histogram_quantile(k ValueTypeScalar, Vector ValueTypeVector) Vector ===
|
// === histogram_quantile(k ValueTypeScalar, Vector ValueTypeVector) Vector ===
|
||||||
func funcHistogramQuantile(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
func funcHistogramQuantile(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
||||||
@ -678,9 +648,8 @@ func funcHistogramQuantile(vals []Value, args Expressions, enh *EvalNodeHelper)
|
|||||||
|
|
||||||
// === resets(Matrix ValueTypeMatrix) Vector ===
|
// === resets(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcResets(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
func funcResets(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
||||||
in := vals[0].(Matrix)
|
samples := vals[0].(Matrix)[0]
|
||||||
|
|
||||||
for _, samples := range in {
|
|
||||||
resets := 0
|
resets := 0
|
||||||
prev := samples.Points[0].V
|
prev := samples.Points[0].V
|
||||||
for _, sample := range samples.Points[1:] {
|
for _, sample := range samples.Points[1:] {
|
||||||
@ -691,18 +660,15 @@ func funcResets(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
|||||||
prev = current
|
prev = current
|
||||||
}
|
}
|
||||||
|
|
||||||
enh.out = append(enh.out, Sample{
|
return append(enh.out, Sample{
|
||||||
Point: Point{V: float64(resets)},
|
Point: Point{V: float64(resets)},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return enh.out
|
|
||||||
}
|
|
||||||
|
|
||||||
// === changes(Matrix ValueTypeMatrix) Vector ===
|
// === changes(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcChanges(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
func funcChanges(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
||||||
in := vals[0].(Matrix)
|
samples := vals[0].(Matrix)[0]
|
||||||
|
|
||||||
for _, samples := range in {
|
|
||||||
changes := 0
|
changes := 0
|
||||||
prev := samples.Points[0].V
|
prev := samples.Points[0].V
|
||||||
for _, sample := range samples.Points[1:] {
|
for _, sample := range samples.Points[1:] {
|
||||||
@ -713,12 +679,10 @@ func funcChanges(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
|||||||
prev = current
|
prev = current
|
||||||
}
|
}
|
||||||
|
|
||||||
enh.out = append(enh.out, Sample{
|
return append(enh.out, Sample{
|
||||||
Point: Point{V: float64(changes)},
|
Point: Point{V: float64(changes)},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return enh.out
|
|
||||||
}
|
|
||||||
|
|
||||||
// === label_replace(Vector ValueTypeVector, dst_label, replacement, src_labelname, regex ValueTypeString) Vector ===
|
// === label_replace(Vector ValueTypeVector, dst_label, replacement, src_labelname, regex ValueTypeString) Vector ===
|
||||||
func funcLabelReplace(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
func funcLabelReplace(vals []Value, args Expressions, enh *EvalNodeHelper) Vector {
|
||||||
@ -1297,7 +1261,7 @@ func createLabelsForAbsentFunction(expr Expr) labels.Labels {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range empty {
|
for _, v := range empty {
|
||||||
m = labels.NewBuilder(m).Set(v, "").Labels()
|
m = labels.NewBuilder(m).Del(v).Labels()
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user