prometheus/promql/functions.go

1308 lines
34 KiB
Go
Raw Normal View History

// Copyright 2015 The Prometheus Authors
// 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.
package promql
import (
"math"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
)
// Function represents a function of the expression language and is
// used by function nodes.
type Function struct {
Name string
2017-06-23 11:15:44 +00:00
ArgTypes []ValueType
Variadic int
2017-06-23 11:15:44 +00:00
ReturnType ValueType
Call func(ev *evaluator, args Expressions) Value
}
// === time() float64 ===
func funcTime(ev *evaluator, args Expressions) Value {
2016-12-24 09:44:04 +00:00
return Scalar{
2016-12-24 10:23:06 +00:00
V: float64(ev.Timestamp / 1000),
T: ev.Timestamp,
}
}
// extrapolatedRate is a utility function for rate/increase/delta.
// It calculates the rate (allowing for counter resets if isCounter is true),
// extrapolates if the first/last sample is close to the boundary, and returns
// the result as either per-second (if isRate is true) or overall.
func extrapolatedRate(ev *evaluator, arg Expr, isCounter bool, isRate bool) Value {
ms := arg.(*MatrixSelector)
2016-12-28 08:16:48 +00:00
var (
matrix = ev.evalMatrix(ms)
rangeStart = ev.Timestamp - durationMilliseconds(ms.Range+ms.Offset)
rangeEnd = ev.Timestamp - durationMilliseconds(ms.Offset)
resultVector = make(Vector, 0, len(matrix))
)
2016-12-28 08:16:48 +00:00
for _, samples := range matrix {
// No sense in trying to compute a rate without at least two points. Drop
2016-12-24 09:40:09 +00:00
// this Vector element.
2016-12-24 10:29:39 +00:00
if len(samples.Points) < 2 {
continue
}
2015-08-24 16:04:41 +00:00
var (
counterCorrection float64
lastValue float64
2015-08-24 16:04:41 +00:00
)
2016-12-24 10:29:39 +00:00
for _, sample := range samples.Points {
2016-12-24 10:23:06 +00:00
if isCounter && sample.V < lastValue {
counterCorrection += lastValue
}
2016-12-24 10:23:06 +00:00
lastValue = sample.V
}
2016-12-24 10:29:39 +00:00
resultValue := lastValue - samples.Points[0].V + counterCorrection
// Duration between first/last samples and boundary of range.
2016-12-28 08:16:48 +00:00
durationToStart := float64(samples.Points[0].T-rangeStart) / 1000
durationToEnd := float64(rangeEnd-samples.Points[len(samples.Points)-1].T) / 1000
2016-12-28 08:16:48 +00:00
sampledInterval := float64(samples.Points[len(samples.Points)-1].T-samples.Points[0].T) / 1000
averageDurationBetweenSamples := sampledInterval / float64(len(samples.Points)-1)
2016-12-24 10:29:39 +00:00
if isCounter && resultValue > 0 && samples.Points[0].V >= 0 {
// Counters cannot be negative. If we have any slope at
// all (i.e. resultValue went up), we can extrapolate
// the zero point of the counter. If the duration to the
// zero point is shorter than the durationToStart, we
// take the zero point as the start of the series,
// thereby avoiding extrapolation to negative counter
// values.
2016-12-28 08:16:48 +00:00
durationToZero := sampledInterval * (samples.Points[0].V / resultValue)
if durationToZero < durationToStart {
durationToStart = durationToZero
}
}
// If the first/last samples are close to the boundaries of the range,
// extrapolate the result. This is as we expect that another sample
// will exist given the spacing between samples we've seen thus far,
// with an allowance for noise.
extrapolationThreshold := averageDurationBetweenSamples * 1.1
extrapolateToInterval := sampledInterval
if durationToStart < extrapolationThreshold {
extrapolateToInterval += durationToStart
} else {
extrapolateToInterval += averageDurationBetweenSamples / 2
}
if durationToEnd < extrapolationThreshold {
extrapolateToInterval += durationToEnd
} else {
extrapolateToInterval += averageDurationBetweenSamples / 2
}
2016-12-28 08:16:48 +00:00
resultValue = resultValue * (extrapolateToInterval / sampledInterval)
if isRate {
2016-12-28 08:16:48 +00:00
resultValue = resultValue / ms.Range.Seconds()
}
2016-12-24 10:32:10 +00:00
resultVector = append(resultVector, Sample{
Metric: dropMetricName(samples.Metric),
2016-12-24 10:23:06 +00:00
Point: Point{V: resultValue, T: ev.Timestamp},
})
}
return resultVector
}
2016-12-24 09:42:54 +00:00
// === delta(Matrix ValueTypeMatrix) Vector ===
func funcDelta(ev *evaluator, args Expressions) Value {
return extrapolatedRate(ev, args[0], false, false)
}
// === rate(node ValueTypeMatrix) Vector ===
func funcRate(ev *evaluator, args Expressions) Value {
return extrapolatedRate(ev, args[0], true, true)
}
// === increase(node ValueTypeMatrix) Vector ===
func funcIncrease(ev *evaluator, args Expressions) Value {
return extrapolatedRate(ev, args[0], true, false)
}
// === irate(node ValueTypeMatrix) Vector ===
func funcIrate(ev *evaluator, args Expressions) Value {
return instantValue(ev, args[0], true)
}
// === idelta(node model.ValMatric) Vector ===
func funcIdelta(ev *evaluator, args Expressions) Value {
return instantValue(ev, args[0], false)
}
func instantValue(ev *evaluator, arg Expr, isRate bool) Value {
2016-12-24 09:40:09 +00:00
resultVector := Vector{}
for _, samples := range ev.evalMatrix(arg) {
// No sense in trying to compute a rate without at least two points. Drop
2016-12-24 09:40:09 +00:00
// this Vector element.
2016-12-24 10:29:39 +00:00
if len(samples.Points) < 2 {
continue
}
2016-12-24 10:29:39 +00:00
lastSample := samples.Points[len(samples.Points)-1]
previousSample := samples.Points[len(samples.Points)-2]
var resultValue float64
2016-12-24 10:23:06 +00:00
if isRate && lastSample.V < previousSample.V {
// Counter reset.
2016-12-24 10:23:06 +00:00
resultValue = lastSample.V
} else {
2016-12-24 10:23:06 +00:00
resultValue = lastSample.V - previousSample.V
}
2016-12-24 10:23:06 +00:00
sampledInterval := lastSample.T - previousSample.T
if sampledInterval == 0 {
// Avoid dividing by 0.
continue
}
if isRate {
// Convert to per-second.
resultValue /= float64(sampledInterval) / 1000
}
2016-12-24 10:32:10 +00:00
resultVector = append(resultVector, Sample{
Metric: dropMetricName(samples.Metric),
2016-12-24 10:23:06 +00:00
Point: Point{V: resultValue, T: ev.Timestamp},
})
}
return resultVector
}
2016-03-10 03:29:02 +00:00
// 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.
// The argument "b" is the set of computed trend factors.
// The argument "d" is the set of raw input values.
func calcTrendValue(i int, sf, tf float64, s, b, d []float64) float64 {
if i == 0 {
return b[0]
}
x := tf * (s[i] - s[i-1])
y := (1 - tf) * b[i-1]
// Cache the computed value.
b[i] = x + y
return b[i]
}
// Holt-Winters is similar to a weighted moving average, where historical data has exponentially less influence on the current data.
2016-05-12 14:14:48 +00:00
// Holt-Winter also accounts for trends in data. The smoothing factor (0 < sf < 1) affects how historical data will affect the current
// data. A lower smoothing factor increases the influence of historical data. The trend factor (0 < tf < 1) affects
// how trends in historical data will affect the current data. A higher trend factor increases the influence.
2016-03-10 03:29:02 +00:00
// of trends. Algorithm taken from https://en.wikipedia.org/wiki/Exponential_smoothing titled: "Double exponential smoothing".
func funcHoltWinters(ev *evaluator, args Expressions) Value {
2016-03-10 03:29:02 +00:00
mat := ev.evalMatrix(args[0])
// The smoothing factor argument.
sf := ev.evalFloat(args[1])
// The trend factor argument.
tf := ev.evalFloat(args[2])
// Sanity check the input.
if sf <= 0 || sf >= 1 {
2016-12-24 10:23:06 +00:00
ev.errorf("invalid smoothing factor. Expected: 0 < sf < 1 goT: %f", sf)
2016-03-10 03:29:02 +00:00
}
if tf <= 0 || tf >= 1 {
2016-12-24 10:23:06 +00:00
ev.errorf("invalid trend factor. Expected: 0 < tf < 1 goT: %f", sf)
2016-03-10 03:29:02 +00:00
}
2016-12-24 09:40:09 +00:00
// Make an output Vector large enough to hold the entire result.
resultVector := make(Vector, 0, len(mat))
2016-03-10 03:29:02 +00:00
// Create scratch values.
var s, b, d []float64
var l int
for _, samples := range mat {
2016-12-24 10:29:39 +00:00
l = len(samples.Points)
2016-03-10 03:29:02 +00:00
// Can't do the smoothing operation with less than two points.
if l < 2 {
continue
}
// Resize scratch values.
if l != len(s) {
s = make([]float64, l)
b = make([]float64, l)
d = make([]float64, l)
}
// Fill in the d values with the raw values from the input.
2016-12-24 10:29:39 +00:00
for i, v := range samples.Points {
2016-12-24 10:23:06 +00:00
d[i] = v.V
2016-03-10 03:29:02 +00:00
}
// Set initial values.
s[0] = d[0]
b[0] = d[1] - d[0]
// Run the smoothing operation.
var x, y float64
for i := 1; i < len(d); i++ {
// Scale the raw value against the smoothing factor.
x = sf * d[i]
// Scale the last smoothed value with the trend at this point.
y = (1 - sf) * (s[i-1] + calcTrendValue(i-1, sf, tf, s, b, d))
s[i] = x + y
}
2016-12-24 10:32:10 +00:00
resultVector = append(resultVector, Sample{
Metric: dropMetricName(samples.Metric),
2016-12-24 10:23:06 +00:00
Point: Point{V: s[len(s)-1], T: ev.Timestamp}, // The last value in the Vector is the smoothed result.
2016-03-10 03:29:02 +00:00
})
}
return resultVector
}
// === sort(node ValueTypeVector) Vector ===
func funcSort(ev *evaluator, args Expressions) Value {
// NaN should sort to the bottom, so take descending sort with NaN first and
// reverse it.
2016-12-24 10:37:16 +00:00
byValueSorter := vectorByReverseValueHeap(ev.evalVector(args[0]))
sort.Sort(sort.Reverse(byValueSorter))
2016-12-24 09:40:09 +00:00
return Vector(byValueSorter)
}
// === sortDesc(node ValueTypeVector) Vector ===
func funcSortDesc(ev *evaluator, args Expressions) Value {
// NaN should sort to the bottom, so take ascending sort with NaN first and
// reverse it.
2016-12-24 10:37:16 +00:00
byValueSorter := vectorByValueHeap(ev.evalVector(args[0]))
sort.Sort(sort.Reverse(byValueSorter))
2016-12-24 09:40:09 +00:00
return Vector(byValueSorter)
}
2016-12-24 09:40:09 +00:00
// === clamp_max(Vector ValueTypeVector, max Scalar) Vector ===
func funcClampMax(ev *evaluator, args Expressions) Value {
vec := ev.evalVector(args[0])
max := ev.evalFloat(args[1])
2016-12-28 08:16:48 +00:00
for i := range vec {
el := &vec[i]
el.Metric = dropMetricName(el.Metric)
2016-12-24 10:23:06 +00:00
el.V = math.Min(max, float64(el.V))
}
return vec
}
2016-12-24 09:40:09 +00:00
// === clamp_min(Vector ValueTypeVector, min Scalar) Vector ===
func funcClampMin(ev *evaluator, args Expressions) Value {
vec := ev.evalVector(args[0])
min := ev.evalFloat(args[1])
2016-12-28 08:16:48 +00:00
for i := range vec {
el := &vec[i]
el.Metric = dropMetricName(el.Metric)
2016-12-24 10:23:06 +00:00
el.V = math.Max(min, float64(el.V))
}
return vec
}
2016-12-24 09:40:09 +00:00
// === round(Vector ValueTypeVector, toNearest=1 Scalar) Vector ===
func funcRound(ev *evaluator, args Expressions) Value {
// round returns a number rounded to toNearest.
// Ties are solved by rounding up.
toNearest := float64(1)
if len(args) >= 2 {
toNearest = ev.evalFloat(args[1])
}
// Invert as it seems to cause fewer floating point accuracy issues.
toNearestInverse := 1.0 / toNearest
2015-08-24 16:04:41 +00:00
vec := ev.evalVector(args[0])
2016-12-28 08:16:48 +00:00
for i := range vec {
el := &vec[i]
el.Metric = dropMetricName(el.Metric)
2016-12-24 10:23:06 +00:00
el.V = math.Floor(float64(el.V)*toNearestInverse+0.5) / toNearestInverse
}
2015-08-24 16:04:41 +00:00
return vec
}
2016-12-24 09:44:04 +00:00
// === Scalar(node ValueTypeVector) Scalar ===
func funcScalar(ev *evaluator, args Expressions) Value {
v := ev.evalVector(args[0])
if len(v) != 1 {
2016-12-24 09:44:04 +00:00
return Scalar{
2016-12-24 10:23:06 +00:00
V: math.NaN(),
T: ev.Timestamp,
2015-09-18 14:49:32 +00:00
}
}
2016-12-24 09:44:04 +00:00
return Scalar{
2016-12-24 10:23:06 +00:00
V: v[0].V,
T: ev.Timestamp,
}
}
2016-12-24 10:23:06 +00:00
func aggrOverTime(ev *evaluator, args Expressions, aggrFn func([]Point) float64) Value {
2015-08-24 16:04:41 +00:00
mat := ev.evalMatrix(args[0])
2016-12-24 09:40:09 +00:00
resultVector := Vector{}
2015-08-24 16:04:41 +00:00
for _, el := range mat {
2016-12-24 10:29:39 +00:00
if len(el.Points) == 0 {
continue
}
2016-12-24 10:32:10 +00:00
resultVector = append(resultVector, Sample{
Metric: dropMetricName(el.Metric),
2016-12-24 10:29:39 +00:00
Point: Point{V: aggrFn(el.Points), T: ev.Timestamp},
})
}
return resultVector
}
2016-12-24 09:42:54 +00:00
// === avg_over_time(Matrix ValueTypeMatrix) Vector ===
func funcAvgOverTime(ev *evaluator, args Expressions) Value {
2016-12-24 10:23:06 +00:00
return aggrOverTime(ev, args, func(values []Point) float64 {
var sum float64
for _, v := range values {
2016-12-24 10:23:06 +00:00
sum += v.V
}
return sum / float64(len(values))
})
}
2016-12-24 09:42:54 +00:00
// === count_over_time(Matrix ValueTypeMatrix) Vector ===
func funcCountOverTime(ev *evaluator, args Expressions) Value {
2016-12-24 10:23:06 +00:00
return aggrOverTime(ev, args, func(values []Point) float64 {
return float64(len(values))
})
}
2016-12-24 09:40:09 +00:00
// === floor(Vector ValueTypeVector) Vector ===
func funcFloor(ev *evaluator, args Expressions) Value {
2016-12-28 08:16:48 +00:00
vec := ev.evalVector(args[0])
for i := range vec {
el := &vec[i]
el.Metric = dropMetricName(el.Metric)
2016-12-24 10:23:06 +00:00
el.V = math.Floor(float64(el.V))
}
2016-12-28 08:16:48 +00:00
return vec
}
2016-12-24 09:42:54 +00:00
// === max_over_time(Matrix ValueTypeMatrix) Vector ===
func funcMaxOverTime(ev *evaluator, args Expressions) Value {
2016-12-24 10:23:06 +00:00
return aggrOverTime(ev, args, func(values []Point) float64 {
max := math.Inf(-1)
for _, v := range values {
2016-12-24 10:23:06 +00:00
max = math.Max(max, float64(v.V))
}
return max
})
}
2016-12-24 09:42:54 +00:00
// === min_over_time(Matrix ValueTypeMatrix) Vector ===
func funcMinOverTime(ev *evaluator, args Expressions) Value {
2016-12-24 10:23:06 +00:00
return aggrOverTime(ev, args, func(values []Point) float64 {
min := math.Inf(1)
for _, v := range values {
2016-12-24 10:23:06 +00:00
min = math.Min(min, float64(v.V))
}
return min
})
}
2016-12-24 09:42:54 +00:00
// === sum_over_time(Matrix ValueTypeMatrix) Vector ===
func funcSumOverTime(ev *evaluator, args Expressions) Value {
2016-12-24 10:23:06 +00:00
return aggrOverTime(ev, args, func(values []Point) float64 {
var sum float64
for _, v := range values {
2016-12-24 10:23:06 +00:00
sum += v.V
}
return sum
})
}
2016-12-24 09:42:54 +00:00
// === quantile_over_time(Matrix ValueTypeMatrix) Vector ===
func funcQuantileOverTime(ev *evaluator, args Expressions) Value {
2016-07-08 12:22:22 +00:00
q := ev.evalFloat(args[0])
mat := ev.evalMatrix(args[1])
2016-12-24 09:40:09 +00:00
resultVector := Vector{}
2016-07-08 12:22:22 +00:00
for _, el := range mat {
2016-12-24 10:29:39 +00:00
if len(el.Points) == 0 {
2016-07-08 12:22:22 +00:00
continue
}
el.Metric = dropMetricName(el.Metric)
2016-12-24 10:37:16 +00:00
values := make(vectorByValueHeap, 0, len(el.Points))
2016-12-24 10:29:39 +00:00
for _, v := range el.Points {
2016-12-24 10:32:10 +00:00
values = append(values, Sample{Point: Point{V: v.V}})
2016-07-08 12:33:20 +00:00
}
2016-12-24 10:32:10 +00:00
resultVector = append(resultVector, Sample{
2016-12-24 10:23:06 +00:00
Metric: el.Metric,
Point: Point{V: quantile(q, values), T: ev.Timestamp},
2016-07-08 12:22:22 +00:00
})
}
return resultVector
}
2016-12-24 09:42:54 +00:00
// === stddev_over_time(Matrix ValueTypeMatrix) Vector ===
func funcStddevOverTime(ev *evaluator, args Expressions) Value {
2016-12-24 10:23:06 +00:00
return aggrOverTime(ev, args, func(values []Point) float64 {
var sum, squaredSum, count float64
for _, v := range values {
2016-12-24 10:23:06 +00:00
sum += v.V
squaredSum += v.V * v.V
count++
}
avg := sum / count
return math.Sqrt(float64(squaredSum/count - avg*avg))
})
}
2016-12-24 09:42:54 +00:00
// === stdvar_over_time(Matrix ValueTypeMatrix) Vector ===
func funcStdvarOverTime(ev *evaluator, args Expressions) Value {
2016-12-24 10:23:06 +00:00
return aggrOverTime(ev, args, func(values []Point) float64 {
var sum, squaredSum, count float64
for _, v := range values {
2016-12-24 10:23:06 +00:00
sum += v.V
squaredSum += v.V * v.V
count++
}
avg := sum / count
return squaredSum/count - avg*avg
})
2016-07-08 12:48:48 +00:00
}
2016-12-24 09:40:09 +00:00
// === abs(Vector ValueTypeVector) Vector ===
func funcAbs(ev *evaluator, args Expressions) Value {
2016-12-28 08:16:48 +00:00
vec := ev.evalVector(args[0])
for i := range vec {
el := &vec[i]
el.Metric = dropMetricName(el.Metric)
2016-12-24 10:23:06 +00:00
el.V = math.Abs(float64(el.V))
}
2016-12-28 08:16:48 +00:00
return vec
}
2016-12-24 09:40:09 +00:00
// === absent(Vector ValueTypeVector) Vector ===
func funcAbsent(ev *evaluator, args Expressions) Value {
if len(ev.evalVector(args[0])) > 0 {
2016-12-24 09:40:09 +00:00
return Vector{}
}
m := []labels.Label{}
if vs, ok := args[0].(*VectorSelector); ok {
for _, ma := range vs.LabelMatchers {
2016-12-25 10:34:22 +00:00
if ma.Type == labels.MatchEqual && ma.Name != labels.MetricName {
m = append(m, labels.Label{Name: ma.Name, Value: ma.Value})
}
}
}
2016-12-24 09:40:09 +00:00
return Vector{
2016-12-24 10:32:10 +00:00
Sample{
2016-12-24 10:23:06 +00:00
Metric: labels.New(m...),
Point: Point{V: 1, T: ev.Timestamp},
},
}
}
2016-12-24 09:40:09 +00:00
// === ceil(Vector ValueTypeVector) Vector ===
func funcCeil(ev *evaluator, args Expressions) Value {
2016-12-28 08:16:48 +00:00
vec := ev.evalVector(args[0])
for i := range vec {
el := &vec[i]
el.Metric = dropMetricName(el.Metric)
2016-12-24 10:23:06 +00:00
el.V = math.Ceil(float64(el.V))
}
2016-12-28 08:16:48 +00:00
return vec
}
2016-12-24 09:40:09 +00:00
// === exp(Vector ValueTypeVector) Vector ===
func funcExp(ev *evaluator, args Expressions) Value {
2016-12-28 08:16:48 +00:00
vec := ev.evalVector(args[0])
for i := range vec {
el := &vec[i]
el.Metric = dropMetricName(el.Metric)
2016-12-24 10:23:06 +00:00
el.V = math.Exp(float64(el.V))
}
2016-12-28 08:16:48 +00:00
return vec
}
2016-12-24 09:40:09 +00:00
// === sqrt(Vector VectorNode) Vector ===
func funcSqrt(ev *evaluator, args Expressions) Value {
2016-12-28 08:16:48 +00:00
vec := ev.evalVector(args[0])
for i := range vec {
el := &vec[i]
el.Metric = dropMetricName(el.Metric)
2016-12-24 10:23:06 +00:00
el.V = math.Sqrt(float64(el.V))
}
2016-12-28 08:16:48 +00:00
return vec
}
2016-12-24 09:40:09 +00:00
// === ln(Vector ValueTypeVector) Vector ===
func funcLn(ev *evaluator, args Expressions) Value {
2016-12-28 08:16:48 +00:00
vec := ev.evalVector(args[0])
for i := range vec {
el := &vec[i]
el.Metric = dropMetricName(el.Metric)
2016-12-24 10:23:06 +00:00
el.V = math.Log(float64(el.V))
}
2016-12-28 08:16:48 +00:00
return vec
}
2016-12-24 09:40:09 +00:00
// === log2(Vector ValueTypeVector) Vector ===
func funcLog2(ev *evaluator, args Expressions) Value {
2016-12-28 08:16:48 +00:00
vec := ev.evalVector(args[0])
for i := range vec {
el := &vec[i]
el.Metric = dropMetricName(el.Metric)
2016-12-24 10:23:06 +00:00
el.V = math.Log2(float64(el.V))
}
2016-12-28 08:16:48 +00:00
return vec
}
2016-12-24 09:40:09 +00:00
// === log10(Vector ValueTypeVector) Vector ===
func funcLog10(ev *evaluator, args Expressions) Value {
2016-12-28 08:16:48 +00:00
vec := ev.evalVector(args[0])
for i := range vec {
el := &vec[i]
el.Metric = dropMetricName(el.Metric)
2016-12-24 10:23:06 +00:00
el.V = math.Log10(float64(el.V))
}
2016-12-28 08:16:48 +00:00
return vec
}
// === timestamp(Vector ValueTypeVector) Vector ===
func funcTimestamp(ev *evaluator, args Expressions) Value {
vec := ev.evalVector(args[0])
for i := range vec {
el := &vec[i]
el.Metric = dropMetricName(el.Metric)
el.V = float64(el.T) / 1000.0
}
return vec
}
// linearRegression performs a least-square linear regression analysis on the
// provided SamplePairs. It returns the slope, and the intercept value at the
// provided time.
2016-12-24 10:23:06 +00:00
func linearRegression(samples []Point, interceptTime int64) (slope, intercept float64) {
var (
n float64
sumX, sumY float64
sumXY, sumX2 float64
)
for _, sample := range samples {
2016-12-28 08:16:48 +00:00
x := float64(sample.T-interceptTime) / 1e3
n += 1.0
2016-12-24 10:23:06 +00:00
sumY += sample.V
sumX += x
2016-12-24 10:23:06 +00:00
sumXY += x * sample.V
sumX2 += x * x
}
covXY := sumXY - sumX*sumY/n
varX := sumX2 - sumX*sumX/n
slope = covXY / varX
intercept = sumY/n - slope*sumX/n
2016-03-09 14:06:00 +00:00
return slope, intercept
}
// === deriv(node ValueTypeMatrix) Vector ===
func funcDeriv(ev *evaluator, args Expressions) Value {
2015-08-24 16:04:41 +00:00
mat := ev.evalMatrix(args[0])
2016-12-24 09:40:09 +00:00
resultVector := make(Vector, 0, len(mat))
2015-08-24 16:04:41 +00:00
for _, samples := range mat {
// No sense in trying to compute a derivative without at least two points.
2016-12-24 09:40:09 +00:00
// Drop this Vector element.
2016-12-24 10:29:39 +00:00
if len(samples.Points) < 2 {
continue
}
2017-08-10 14:28:49 +00:00
// We pass in an arbitrary timestamp that is near the values in use
// to avoid floating point accuracy issues, see
// https://github.com/prometheus/prometheus/issues/2674
2017-08-10 14:28:49 +00:00
slope, _ := linearRegression(samples.Points, samples.Points[0].T)
2016-12-24 10:32:10 +00:00
resultSample := Sample{
Metric: dropMetricName(samples.Metric),
2016-12-24 10:23:06 +00:00
Point: Point{V: slope, T: ev.Timestamp},
}
resultVector = append(resultVector, resultSample)
}
return resultVector
}
// === predict_linear(node ValueTypeMatrix, k ValueTypeScalar) Vector ===
func funcPredictLinear(ev *evaluator, args Expressions) Value {
mat := ev.evalMatrix(args[0])
2016-12-24 09:40:09 +00:00
resultVector := make(Vector, 0, len(mat))
duration := ev.evalFloat(args[1])
for _, samples := range mat {
// No sense in trying to predict anything without at least two points.
2016-12-24 09:40:09 +00:00
// Drop this Vector element.
2016-12-24 10:29:39 +00:00
if len(samples.Points) < 2 {
continue
}
2016-12-24 10:29:39 +00:00
slope, intercept := linearRegression(samples.Points, ev.Timestamp)
2016-12-24 10:32:10 +00:00
resultVector = append(resultVector, Sample{
Metric: dropMetricName(samples.Metric),
2016-12-24 10:23:06 +00:00
Point: Point{V: slope*duration + intercept, T: ev.Timestamp},
})
}
return resultVector
}
2016-12-24 09:40:09 +00:00
// === histogram_quantile(k ValueTypeScalar, Vector ValueTypeVector) Vector ===
func funcHistogramQuantile(ev *evaluator, args Expressions) Value {
q := ev.evalFloat(args[0])
inVec := ev.evalVector(args[1])
2016-12-24 09:40:09 +00:00
outVec := Vector{}
signatureToMetricWithBuckets := map[uint64]*metricWithBuckets{}
for _, el := range inVec {
upperBound, err := strconv.ParseFloat(
el.Metric.Get(model.BucketLabel), 64,
)
if err != nil {
// Oops, no bucket label or malformed label value. Skip.
// TODO(beorn7): Issue a warning somehow.
continue
}
hash := hashWithoutLabels(el.Metric, excludedLabels...)
mb, ok := signatureToMetricWithBuckets[hash]
if !ok {
el.Metric = labels.NewBuilder(el.Metric).
Del(labels.BucketLabel, labels.MetricName).
Labels()
mb = &metricWithBuckets{el.Metric, nil}
signatureToMetricWithBuckets[hash] = mb
}
2016-12-24 10:23:06 +00:00
mb.buckets = append(mb.buckets, bucket{upperBound, el.V})
}
for _, mb := range signatureToMetricWithBuckets {
2016-12-24 10:32:10 +00:00
outVec = append(outVec, Sample{
2016-12-24 10:23:06 +00:00
Metric: mb.metric,
Point: Point{V: bucketQuantile(q, mb.buckets), T: ev.Timestamp},
})
}
return outVec
}
2016-12-24 09:42:54 +00:00
// === resets(Matrix ValueTypeMatrix) Vector ===
func funcResets(ev *evaluator, args Expressions) Value {
in := ev.evalMatrix(args[0])
2016-12-24 09:40:09 +00:00
out := make(Vector, 0, len(in))
for _, samples := range in {
resets := 0
2016-12-24 10:29:39 +00:00
prev := samples.Points[0].V
for _, sample := range samples.Points[1:] {
2016-12-24 10:23:06 +00:00
current := sample.V
if current < prev {
resets++
}
prev = current
}
2016-12-24 10:32:10 +00:00
out = append(out, Sample{
Metric: dropMetricName(samples.Metric),
2016-12-24 10:23:06 +00:00
Point: Point{V: float64(resets), T: ev.Timestamp},
})
}
return out
}
2016-12-24 09:42:54 +00:00
// === changes(Matrix ValueTypeMatrix) Vector ===
func funcChanges(ev *evaluator, args Expressions) Value {
in := ev.evalMatrix(args[0])
2016-12-24 09:40:09 +00:00
out := make(Vector, 0, len(in))
for _, samples := range in {
changes := 0
2016-12-24 10:29:39 +00:00
prev := samples.Points[0].V
for _, sample := range samples.Points[1:] {
2016-12-24 10:23:06 +00:00
current := sample.V
if current != prev && !(math.IsNaN(float64(current)) && math.IsNaN(float64(prev))) {
changes++
}
prev = current
}
2016-12-24 10:32:10 +00:00
out = append(out, Sample{
Metric: dropMetricName(samples.Metric),
2016-12-24 10:23:06 +00:00
Point: Point{V: float64(changes), T: ev.Timestamp},
})
}
return out
}
2016-12-24 09:40:09 +00:00
// === label_replace(Vector ValueTypeVector, dst_label, replacement, src_labelname, regex ValueTypeString) Vector ===
func funcLabelReplace(ev *evaluator, args Expressions) Value {
var (
2016-12-28 08:16:48 +00:00
vector = ev.evalVector(args[0])
2016-12-24 10:25:26 +00:00
dst = ev.evalString(args[1]).V
repl = ev.evalString(args[2]).V
src = ev.evalString(args[3]).V
regexStr = ev.evalString(args[4]).V
)
regex, err := regexp.Compile("^(?:" + regexStr + ")$")
if err != nil {
ev.errorf("invalid regular expression in label_replace(): %s", regexStr)
}
if !model.LabelNameRE.MatchString(string(dst)) {
ev.errorf("invalid destination label name in label_replace(): %s", dst)
}
2016-12-28 08:16:48 +00:00
outSet := make(map[uint64]struct{}, len(vector))
for i := range vector {
el := &vector[i]
srcVal := el.Metric.Get(src)
indexes := regex.FindStringSubmatchIndex(srcVal)
// If there is no match, no replacement should take place.
if indexes == nil {
continue
}
res := regex.ExpandString([]byte{}, repl, srcVal, indexes)
lb := labels.NewBuilder(el.Metric).Del(dst)
if len(res) > 0 {
lb.Set(dst, string(res))
}
el.Metric = lb.Labels()
h := el.Metric.Hash()
if _, ok := outSet[h]; ok {
ev.errorf("duplicated label set in output of label_replace(): %s", el.Metric)
} else {
outSet[h] = struct{}{}
}
}
2016-12-28 08:16:48 +00:00
return vector
}
2016-12-24 09:44:04 +00:00
// === Vector(s Scalar) Vector ===
func funcVector(ev *evaluator, args Expressions) Value {
2016-12-24 09:40:09 +00:00
return Vector{
2016-12-24 10:32:10 +00:00
Sample{
2016-12-24 10:23:06 +00:00
Metric: labels.Labels{},
Point: Point{V: ev.evalFloat(args[0]), T: ev.Timestamp},
},
}
}
// === label_join(vector model.ValVector, dest_labelname, separator, src_labelname...) Vector ===
2017-06-23 11:15:44 +00:00
func funcLabelJoin(ev *evaluator, args Expressions) Value {
var (
vector = ev.evalVector(args[0])
2017-06-23 11:15:44 +00:00
dst = ev.evalString(args[1]).V
sep = ev.evalString(args[2]).V
srcLabels = make([]string, len(args)-3)
)
for i := 3; i < len(args); i++ {
2017-06-23 11:15:44 +00:00
src := ev.evalString(args[i]).V
if !model.LabelName(src).IsValid() {
ev.errorf("invalid source label name in label_join(): %s", src)
}
srcLabels[i-3] = src
}
2017-06-23 11:15:44 +00:00
if !model.LabelName(dst).IsValid() {
ev.errorf("invalid destination label name in label_join(): %s", dst)
}
2017-06-23 11:15:44 +00:00
outSet := make(map[uint64]struct{}, len(vector))
for i := range vector {
el := &vector[i]
srcVals := make([]string, len(srcLabels))
for i, src := range srcLabels {
2017-06-23 11:15:44 +00:00
srcVals[i] = el.Metric.Get(src)
}
2017-06-23 11:15:44 +00:00
lb := labels.NewBuilder(el.Metric)
strval := strings.Join(srcVals, sep)
if strval == "" {
2017-06-23 11:15:44 +00:00
lb.Del(dst)
} else {
2017-06-23 11:15:44 +00:00
lb.Set(dst, strval)
}
2017-06-23 11:15:44 +00:00
el.Metric = lb.Labels()
h := el.Metric.Hash()
if _, exists := outSet[h]; exists {
ev.errorf("duplicated label set in output of label_join(): %s", el.Metric)
} else {
2017-06-23 11:15:44 +00:00
outSet[h] = struct{}{}
}
}
return vector
}
// Common code for date related functions.
func dateWrapper(ev *evaluator, args Expressions, f func(time.Time) float64) Value {
2016-12-24 09:40:09 +00:00
var v Vector
if len(args) == 0 {
2016-12-24 09:40:09 +00:00
v = Vector{
2016-12-24 10:32:10 +00:00
Sample{
Metric: labels.Labels{},
2017-09-04 12:09:21 +00:00
Point: Point{V: float64(ev.Timestamp) / 1000, T: ev.Timestamp},
},
}
} else {
v = ev.evalVector(args[0])
}
2016-12-28 08:16:48 +00:00
for i := range v {
el := &v[i]
el.Metric = dropMetricName(el.Metric)
2016-12-24 10:23:06 +00:00
t := time.Unix(int64(el.V), 0).UTC()
el.V = f(t)
2016-08-22 20:08:13 +00:00
}
return v
}
2016-12-24 09:44:04 +00:00
// === days_in_month(v Vector) Scalar ===
func funcDaysInMonth(ev *evaluator, args Expressions) Value {
return dateWrapper(ev, args, func(t time.Time) float64 {
return float64(32 - time.Date(t.Year(), t.Month(), 32, 0, 0, 0, 0, time.UTC).Day())
})
2016-08-22 20:08:13 +00:00
}
2016-12-24 09:44:04 +00:00
// === day_of_month(v Vector) Scalar ===
func funcDayOfMonth(ev *evaluator, args Expressions) Value {
return dateWrapper(ev, args, func(t time.Time) float64 {
return float64(t.Day())
})
}
2016-12-24 09:44:04 +00:00
// === day_of_week(v Vector) Scalar ===
func funcDayOfWeek(ev *evaluator, args Expressions) Value {
return dateWrapper(ev, args, func(t time.Time) float64 {
return float64(t.Weekday())
})
}
2016-12-24 09:44:04 +00:00
// === hour(v Vector) Scalar ===
func funcHour(ev *evaluator, args Expressions) Value {
return dateWrapper(ev, args, func(t time.Time) float64 {
return float64(t.Hour())
})
}
2016-12-24 09:44:04 +00:00
// === minute(v Vector) Scalar ===
func funcMinute(ev *evaluator, args Expressions) Value {
return dateWrapper(ev, args, func(t time.Time) float64 {
return float64(t.Minute())
})
}
2016-12-24 09:44:04 +00:00
// === month(v Vector) Scalar ===
func funcMonth(ev *evaluator, args Expressions) Value {
return dateWrapper(ev, args, func(t time.Time) float64 {
return float64(t.Month())
})
}
2016-12-24 09:44:04 +00:00
// === year(v Vector) Scalar ===
func funcYear(ev *evaluator, args Expressions) Value {
return dateWrapper(ev, args, func(t time.Time) float64 {
return float64(t.Year())
})
}
var functions = map[string]*Function{
"abs": {
Name: "abs",
ArgTypes: []ValueType{ValueTypeVector},
ReturnType: ValueTypeVector,
Call: funcAbs,
},
"absent": {
Name: "absent",
ArgTypes: []ValueType{ValueTypeVector},
ReturnType: ValueTypeVector,
Call: funcAbsent,
},
"avg_over_time": {
Name: "avg_over_time",
ArgTypes: []ValueType{ValueTypeMatrix},
ReturnType: ValueTypeVector,
Call: funcAvgOverTime,
},
"ceil": {
Name: "ceil",
ArgTypes: []ValueType{ValueTypeVector},
ReturnType: ValueTypeVector,
Call: funcCeil,
},
"changes": {
Name: "changes",
ArgTypes: []ValueType{ValueTypeMatrix},
ReturnType: ValueTypeVector,
Call: funcChanges,
},
"clamp_max": {
Name: "clamp_max",
ArgTypes: []ValueType{ValueTypeVector, ValueTypeScalar},
ReturnType: ValueTypeVector,
Call: funcClampMax,
},
"clamp_min": {
Name: "clamp_min",
ArgTypes: []ValueType{ValueTypeVector, ValueTypeScalar},
ReturnType: ValueTypeVector,
Call: funcClampMin,
},
"count_over_time": {
Name: "count_over_time",
ArgTypes: []ValueType{ValueTypeMatrix},
ReturnType: ValueTypeVector,
Call: funcCountOverTime,
},
2016-08-22 20:08:13 +00:00
"days_in_month": {
Name: "days_in_month",
2017-06-23 11:15:44 +00:00
ArgTypes: []ValueType{ValueTypeVector},
Variadic: 1,
2017-06-23 11:15:44 +00:00
ReturnType: ValueTypeVector,
Call: funcDaysInMonth,
2016-08-22 20:08:13 +00:00
},
"day_of_month": {
Name: "day_of_month",
2017-06-23 11:15:44 +00:00
ArgTypes: []ValueType{ValueTypeVector},
Variadic: 1,
2017-06-23 11:15:44 +00:00
ReturnType: ValueTypeVector,
Call: funcDayOfMonth,
},
"day_of_week": {
Name: "day_of_week",
2017-06-23 11:15:44 +00:00
ArgTypes: []ValueType{ValueTypeVector},
Variadic: 1,
2017-06-23 11:15:44 +00:00
ReturnType: ValueTypeVector,
Call: funcDayOfWeek,
},
"delta": {
Name: "delta",
ArgTypes: []ValueType{ValueTypeMatrix},
ReturnType: ValueTypeVector,
Call: funcDelta,
},
"deriv": {
Name: "deriv",
ArgTypes: []ValueType{ValueTypeMatrix},
ReturnType: ValueTypeVector,
Call: funcDeriv,
},
"exp": {
Name: "exp",
ArgTypes: []ValueType{ValueTypeVector},
ReturnType: ValueTypeVector,
Call: funcExp,
},
"floor": {
Name: "floor",
ArgTypes: []ValueType{ValueTypeVector},
ReturnType: ValueTypeVector,
Call: funcFloor,
},
"histogram_quantile": {
Name: "histogram_quantile",
ArgTypes: []ValueType{ValueTypeScalar, ValueTypeVector},
ReturnType: ValueTypeVector,
Call: funcHistogramQuantile,
},
2016-03-10 03:29:02 +00:00
"holt_winters": {
Name: "holt_winters",
ArgTypes: []ValueType{ValueTypeMatrix, ValueTypeScalar, ValueTypeScalar},
ReturnType: ValueTypeVector,
2016-03-10 03:29:02 +00:00
Call: funcHoltWinters,
},
"hour": {
Name: "hour",
2017-06-23 11:15:44 +00:00
ArgTypes: []ValueType{ValueTypeVector},
Variadic: 1,
2017-06-23 11:15:44 +00:00
ReturnType: ValueTypeVector,
Call: funcHour,
},
"idelta": {
Name: "idelta",
ArgTypes: []ValueType{ValueTypeMatrix},
ReturnType: ValueTypeVector,
Call: funcIdelta,
},
2016-08-22 20:00:53 +00:00
"increase": {
Name: "increase",
ArgTypes: []ValueType{ValueTypeMatrix},
ReturnType: ValueTypeVector,
2016-08-22 20:00:53 +00:00
Call: funcIncrease,
},
"irate": {
Name: "irate",
ArgTypes: []ValueType{ValueTypeMatrix},
ReturnType: ValueTypeVector,
2016-08-22 20:00:53 +00:00
Call: funcIrate,
},
"label_replace": {
Name: "label_replace",
ArgTypes: []ValueType{ValueTypeVector, ValueTypeString, ValueTypeString, ValueTypeString, ValueTypeString},
ReturnType: ValueTypeVector,
Call: funcLabelReplace,
},
"label_join": {
Name: "label_join",
2017-06-23 11:15:44 +00:00
ArgTypes: []ValueType{ValueTypeVector, ValueTypeString, ValueTypeString, ValueTypeString},
Variadic: -1,
2017-06-23 11:15:44 +00:00
ReturnType: ValueTypeVector,
Call: funcLabelJoin,
},
"ln": {
Name: "ln",
ArgTypes: []ValueType{ValueTypeVector},
ReturnType: ValueTypeVector,
Call: funcLn,
},
"log10": {
Name: "log10",
ArgTypes: []ValueType{ValueTypeVector},
ReturnType: ValueTypeVector,
Call: funcLog10,
},
"log2": {
Name: "log2",
ArgTypes: []ValueType{ValueTypeVector},
ReturnType: ValueTypeVector,
Call: funcLog2,
},
"max_over_time": {
Name: "max_over_time",
ArgTypes: []ValueType{ValueTypeMatrix},
ReturnType: ValueTypeVector,
Call: funcMaxOverTime,
},
"min_over_time": {
Name: "min_over_time",
ArgTypes: []ValueType{ValueTypeMatrix},
ReturnType: ValueTypeVector,
Call: funcMinOverTime,
},
"minute": {
Name: "minute",
2017-06-23 11:15:44 +00:00
ArgTypes: []ValueType{ValueTypeVector},
Variadic: 1,
2017-06-23 11:15:44 +00:00
ReturnType: ValueTypeVector,
Call: funcMinute,
},
"month": {
Name: "month",
2017-06-23 11:15:44 +00:00
ArgTypes: []ValueType{ValueTypeVector},
Variadic: 1,
2017-06-23 11:15:44 +00:00
ReturnType: ValueTypeVector,
Call: funcMonth,
},
"predict_linear": {
Name: "predict_linear",
ArgTypes: []ValueType{ValueTypeMatrix, ValueTypeScalar},
ReturnType: ValueTypeVector,
Call: funcPredictLinear,
},
2016-07-08 12:22:22 +00:00
"quantile_over_time": {
Name: "quantile_over_time",
ArgTypes: []ValueType{ValueTypeScalar, ValueTypeMatrix},
ReturnType: ValueTypeVector,
2016-07-08 12:22:22 +00:00
Call: funcQuantileOverTime,
},
"rate": {
Name: "rate",
ArgTypes: []ValueType{ValueTypeMatrix},
ReturnType: ValueTypeVector,
Call: funcRate,
},
"resets": {
Name: "resets",
ArgTypes: []ValueType{ValueTypeMatrix},
ReturnType: ValueTypeVector,
Call: funcResets,
},
"round": {
Name: "round",
2017-06-23 11:15:44 +00:00
ArgTypes: []ValueType{ValueTypeVector, ValueTypeScalar},
Variadic: 1,
2017-06-23 11:15:44 +00:00
ReturnType: ValueTypeVector,
Call: funcRound,
},
2016-12-28 08:16:48 +00:00
"scalar": {
Name: "scalar",
ArgTypes: []ValueType{ValueTypeVector},
ReturnType: ValueTypeScalar,
Call: funcScalar,
},
"sort": {
Name: "sort",
ArgTypes: []ValueType{ValueTypeVector},
ReturnType: ValueTypeVector,
Call: funcSort,
},
"sort_desc": {
Name: "sort_desc",
ArgTypes: []ValueType{ValueTypeVector},
ReturnType: ValueTypeVector,
Call: funcSortDesc,
},
"sqrt": {
Name: "sqrt",
ArgTypes: []ValueType{ValueTypeVector},
ReturnType: ValueTypeVector,
Call: funcSqrt,
},
"stddev_over_time": {
Name: "stddev_over_time",
ArgTypes: []ValueType{ValueTypeMatrix},
ReturnType: ValueTypeVector,
Call: funcStddevOverTime,
},
"stdvar_over_time": {
Name: "stdvar_over_time",
ArgTypes: []ValueType{ValueTypeMatrix},
ReturnType: ValueTypeVector,
Call: funcStdvarOverTime,
},
"sum_over_time": {
Name: "sum_over_time",
ArgTypes: []ValueType{ValueTypeMatrix},
ReturnType: ValueTypeVector,
Call: funcSumOverTime,
},
"time": {
Name: "time",
ArgTypes: []ValueType{},
ReturnType: ValueTypeScalar,
Call: funcTime,
},
"timestamp": {
Name: "timestamp",
ArgTypes: []ValueType{ValueTypeVector},
ReturnType: ValueTypeVector,
Call: funcTimestamp,
},
2016-12-28 08:16:48 +00:00
"vector": {
Name: "vector",
ArgTypes: []ValueType{ValueTypeScalar},
ReturnType: ValueTypeVector,
2015-09-16 12:33:12 +00:00
Call: funcVector,
},
"year": {
Name: "year",
2017-06-23 11:15:44 +00:00
ArgTypes: []ValueType{ValueTypeVector},
Variadic: 1,
2017-06-23 11:15:44 +00:00
ReturnType: ValueTypeVector,
Call: funcYear,
},
}
// getFunction returns a predefined Function object for the given name.
func getFunction(name string) (*Function, bool) {
function, ok := functions[name]
return function, ok
}
2016-12-24 10:37:16 +00:00
type vectorByValueHeap Vector
2016-12-24 10:37:16 +00:00
func (s vectorByValueHeap) Len() int {
return len(s)
}
2016-12-24 10:37:16 +00:00
func (s vectorByValueHeap) Less(i, j int) bool {
2016-12-24 10:23:06 +00:00
if math.IsNaN(float64(s[i].V)) {
return true
}
2016-12-24 10:23:06 +00:00
return s[i].V < s[j].V
}
2016-12-24 10:37:16 +00:00
func (s vectorByValueHeap) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
2016-12-24 10:37:16 +00:00
func (s *vectorByValueHeap) Push(x interface{}) {
2016-12-28 08:16:48 +00:00
*s = append(*s, *(x.(*Sample)))
}
2016-12-24 10:37:16 +00:00
func (s *vectorByValueHeap) Pop() interface{} {
old := *s
n := len(old)
el := old[n-1]
*s = old[0 : n-1]
return el
}
2016-12-24 10:37:16 +00:00
type vectorByReverseValueHeap Vector
2016-12-24 10:37:16 +00:00
func (s vectorByReverseValueHeap) Len() int {
return len(s)
}
2016-12-24 10:37:16 +00:00
func (s vectorByReverseValueHeap) Less(i, j int) bool {
2016-12-24 10:23:06 +00:00
if math.IsNaN(float64(s[i].V)) {
return true
}
2016-12-24 10:23:06 +00:00
return s[i].V > s[j].V
}
2016-12-24 10:37:16 +00:00
func (s vectorByReverseValueHeap) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
2016-12-24 10:37:16 +00:00
func (s *vectorByReverseValueHeap) Push(x interface{}) {
2016-12-28 08:16:48 +00:00
*s = append(*s, *(x.(*Sample)))
}
2016-12-24 10:37:16 +00:00
func (s *vectorByReverseValueHeap) Pop() interface{} {
old := *s
n := len(old)
el := old[n-1]
*s = old[0 : n-1]
return el
}