2015-01-21 11:07:45 -08:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-02-07 02:49:04 -08:00
|
|
|
// 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.
|
|
|
|
|
2013-01-07 14:24:26 -08:00
|
|
|
package ast
|
|
|
|
|
|
|
|
import (
|
2014-08-05 09:57:47 -07:00
|
|
|
"container/heap"
|
2013-01-07 14:24:26 -08:00
|
|
|
"fmt"
|
2013-09-17 05:59:31 -07:00
|
|
|
"math"
|
2013-04-10 01:44:13 -07:00
|
|
|
"sort"
|
2015-02-19 10:56:45 -08:00
|
|
|
"strconv"
|
2013-01-07 14:24:26 -08:00
|
|
|
"time"
|
2013-06-25 05:02:27 -07:00
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
2014-07-28 07:12:58 -07:00
|
|
|
"github.com/prometheus/prometheus/storage/metric"
|
2013-01-07 14:24:26 -08:00
|
|
|
)
|
|
|
|
|
2014-02-13 09:48:56 -08:00
|
|
|
// Function represents a function of the expression language and is
|
|
|
|
// used by function nodes.
|
2013-01-07 14:24:26 -08:00
|
|
|
type Function struct {
|
2015-01-22 03:52:30 -08:00
|
|
|
name string
|
|
|
|
argTypes []ExprType
|
|
|
|
optionalArgs int
|
|
|
|
returnType ExprType
|
|
|
|
callFn func(timestamp clientmodel.Timestamp, args []Node) interface{}
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2014-02-13 09:48:56 -08:00
|
|
|
// CheckArgTypes returns a non-nil error if the number or types of
|
|
|
|
// passed in arg nodes do not match the function's expectations.
|
2013-01-07 14:24:26 -08:00
|
|
|
func (function *Function) CheckArgTypes(args []Node) error {
|
2015-01-22 03:52:30 -08:00
|
|
|
if len(function.argTypes) < len(args) {
|
2014-02-13 09:48:56 -08:00
|
|
|
return fmt.Errorf(
|
2015-01-22 03:52:30 -08:00
|
|
|
"too many arguments to function %v(): %v expected at most, %v given",
|
2014-02-13 09:48:56 -08:00
|
|
|
function.name, len(function.argTypes), len(args),
|
|
|
|
)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2015-02-04 03:24:00 -08:00
|
|
|
if len(function.argTypes)-function.optionalArgs > len(args) {
|
2015-01-22 03:52:30 -08:00
|
|
|
return fmt.Errorf(
|
|
|
|
"too few arguments to function %v(): %v expected at least, %v given",
|
|
|
|
function.name, len(function.argTypes)-function.optionalArgs, len(args),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
for idx, arg := range args {
|
2013-01-07 14:24:26 -08:00
|
|
|
invalidType := false
|
|
|
|
var expectedType string
|
2015-01-22 03:52:30 -08:00
|
|
|
if _, ok := arg.(ScalarNode); function.argTypes[idx] == ScalarType && !ok {
|
2013-01-07 14:24:26 -08:00
|
|
|
invalidType = true
|
|
|
|
expectedType = "scalar"
|
|
|
|
}
|
2015-01-22 03:52:30 -08:00
|
|
|
if _, ok := arg.(VectorNode); function.argTypes[idx] == VectorType && !ok {
|
2013-01-07 14:24:26 -08:00
|
|
|
invalidType = true
|
|
|
|
expectedType = "vector"
|
|
|
|
}
|
2015-01-22 03:52:30 -08:00
|
|
|
if _, ok := arg.(MatrixNode); function.argTypes[idx] == MatrixType && !ok {
|
2013-01-07 14:24:26 -08:00
|
|
|
invalidType = true
|
|
|
|
expectedType = "matrix"
|
|
|
|
}
|
2015-01-22 03:52:30 -08:00
|
|
|
if _, ok := arg.(StringNode); function.argTypes[idx] == StringType && !ok {
|
2013-01-07 14:24:26 -08:00
|
|
|
invalidType = true
|
|
|
|
expectedType = "string"
|
|
|
|
}
|
|
|
|
|
|
|
|
if invalidType {
|
2014-02-13 09:48:56 -08:00
|
|
|
return fmt.Errorf(
|
|
|
|
"wrong type for argument %v in function %v(), expected %v",
|
|
|
|
idx, function.name, expectedType,
|
|
|
|
)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
// === time() clientmodel.SampleValue ===
|
2014-06-06 02:55:53 -07:00
|
|
|
func timeImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
2014-07-28 09:10:11 -07:00
|
|
|
return clientmodel.SampleValue(timestamp.Unix())
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2015-01-22 03:52:30 -08:00
|
|
|
// === delta(matrix MatrixNode, isCounter=0 ScalarNode) Vector ===
|
2014-06-06 02:55:53 -07:00
|
|
|
func deltaImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
2013-01-07 14:24:26 -08:00
|
|
|
matrixNode := args[0].(MatrixNode)
|
2015-01-22 03:52:30 -08:00
|
|
|
isCounter := len(args) >= 2 && args[1].(ScalarNode).Eval(timestamp) > 0
|
2013-01-07 14:24:26 -08:00
|
|
|
resultVector := Vector{}
|
|
|
|
|
|
|
|
// If we treat these metrics as counters, we need to fetch all values
|
|
|
|
// in the interval to find breaks in the timeseries' monotonicity.
|
|
|
|
// I.e. if a counter resets, we want to ignore that reset.
|
|
|
|
var matrixValue Matrix
|
2013-05-28 08:36:53 -07:00
|
|
|
if isCounter {
|
2014-06-06 02:55:53 -07:00
|
|
|
matrixValue = matrixNode.Eval(timestamp)
|
2013-01-07 14:24:26 -08:00
|
|
|
} else {
|
2014-06-06 02:55:53 -07:00
|
|
|
matrixValue = matrixNode.EvalBoundaries(timestamp)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
for _, samples := range matrixValue {
|
2013-05-27 02:10:40 -07:00
|
|
|
// No sense in trying to compute a delta without at least two points. Drop
|
|
|
|
// this vector element.
|
|
|
|
if len(samples.Values) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
counterCorrection := clientmodel.SampleValue(0)
|
|
|
|
lastValue := clientmodel.SampleValue(0)
|
2013-01-07 14:24:26 -08:00
|
|
|
for _, sample := range samples.Values {
|
|
|
|
currentValue := sample.Value
|
2013-05-28 08:36:53 -07:00
|
|
|
if isCounter && currentValue < lastValue {
|
2013-01-07 14:24:26 -08:00
|
|
|
counterCorrection += lastValue - currentValue
|
|
|
|
}
|
|
|
|
lastValue = currentValue
|
|
|
|
}
|
|
|
|
resultValue := lastValue - samples.Values[0].Value + counterCorrection
|
2013-04-12 17:45:58 -07:00
|
|
|
|
2014-02-22 13:29:32 -08:00
|
|
|
targetInterval := args[0].(*MatrixSelector).interval
|
2013-04-12 17:45:58 -07:00
|
|
|
sampledInterval := samples.Values[len(samples.Values)-1].Timestamp.Sub(samples.Values[0].Timestamp)
|
|
|
|
if sampledInterval == 0 {
|
|
|
|
// Only found one sample. Cannot compute a rate from this.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Correct for differences in target vs. actual delta interval.
|
|
|
|
//
|
|
|
|
// Above, we didn't actually calculate the delta for the specified target
|
|
|
|
// interval, but for an interval between the first and last found samples
|
|
|
|
// under the target interval, which will usually have less time between
|
|
|
|
// them. Depending on how many samples are found under a target interval,
|
|
|
|
// the delta results are distorted and temporal aliasing occurs (ugly
|
|
|
|
// bumps). This effect is corrected for below.
|
2013-06-25 05:02:27 -07:00
|
|
|
intervalCorrection := clientmodel.SampleValue(targetInterval) / clientmodel.SampleValue(sampledInterval)
|
2013-04-12 17:45:58 -07:00
|
|
|
resultValue *= intervalCorrection
|
|
|
|
|
2014-12-08 07:55:49 -08:00
|
|
|
resultSample := &Sample{
|
2013-01-07 14:24:26 -08:00
|
|
|
Metric: samples.Metric,
|
|
|
|
Value: resultValue,
|
2013-03-28 09:05:06 -07:00
|
|
|
Timestamp: timestamp,
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2014-12-08 07:55:49 -08:00
|
|
|
resultSample.Metric.Delete(clientmodel.MetricNameLabel)
|
2013-01-07 14:24:26 -08:00
|
|
|
resultVector = append(resultVector, resultSample)
|
|
|
|
}
|
|
|
|
return resultVector
|
|
|
|
}
|
|
|
|
|
2014-08-05 09:57:47 -07:00
|
|
|
// === rate(node MatrixNode) Vector ===
|
2014-06-06 02:55:53 -07:00
|
|
|
func rateImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
2013-01-07 14:24:26 -08:00
|
|
|
args = append(args, &ScalarLiteral{value: 1})
|
2014-06-06 02:55:53 -07:00
|
|
|
vector := deltaImpl(timestamp, args).(Vector)
|
2013-01-21 16:49:00 -08:00
|
|
|
|
|
|
|
// TODO: could be other type of MatrixNode in the future (right now, only
|
2014-02-22 13:29:32 -08:00
|
|
|
// MatrixSelector exists). Find a better way of getting the duration of a
|
2013-01-21 16:49:00 -08:00
|
|
|
// matrix, such as looking at the samples themselves.
|
2014-02-22 13:29:32 -08:00
|
|
|
interval := args[0].(*MatrixSelector).interval
|
2013-04-16 08:23:59 -07:00
|
|
|
for i := range vector {
|
2013-06-25 05:02:27 -07:00
|
|
|
vector[i].Value /= clientmodel.SampleValue(interval / time.Second)
|
2013-01-21 16:49:00 -08:00
|
|
|
}
|
|
|
|
return vector
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2014-08-05 09:57:47 -07:00
|
|
|
type vectorByValueHeap Vector
|
|
|
|
|
|
|
|
func (s vectorByValueHeap) Len() int {
|
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s vectorByValueHeap) Less(i, j int) bool {
|
|
|
|
return s[i].Value < s[j].Value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s vectorByValueHeap) Swap(i, j int) {
|
|
|
|
s[i], s[j] = s[j], s[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *vectorByValueHeap) Push(x interface{}) {
|
2014-12-08 07:55:49 -08:00
|
|
|
*s = append(*s, x.(*Sample))
|
2013-04-10 01:44:13 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 09:57:47 -07:00
|
|
|
func (s *vectorByValueHeap) Pop() interface{} {
|
|
|
|
old := *s
|
|
|
|
n := len(old)
|
|
|
|
el := old[n-1]
|
|
|
|
*s = old[0 : n-1]
|
|
|
|
return el
|
2013-04-10 01:44:13 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 09:57:47 -07:00
|
|
|
type reverseHeap struct {
|
|
|
|
heap.Interface
|
2013-04-10 01:44:13 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 09:57:47 -07:00
|
|
|
func (s reverseHeap) Less(i, j int) bool {
|
|
|
|
return s.Interface.Less(j, i)
|
2013-04-10 01:44:13 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 09:57:47 -07:00
|
|
|
// === sort(node VectorNode) Vector ===
|
2014-06-06 02:55:53 -07:00
|
|
|
func sortImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
byValueSorter := vectorByValueHeap(args[0].(VectorNode).Eval(timestamp))
|
2013-04-10 01:44:13 -07:00
|
|
|
sort.Sort(byValueSorter)
|
2014-08-05 09:57:47 -07:00
|
|
|
return Vector(byValueSorter)
|
2013-04-10 01:44:13 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 09:57:47 -07:00
|
|
|
// === sortDesc(node VectorNode) Vector ===
|
2014-06-06 02:55:53 -07:00
|
|
|
func sortDescImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
byValueSorter := vectorByValueHeap(args[0].(VectorNode).Eval(timestamp))
|
2014-08-05 09:57:47 -07:00
|
|
|
sort.Sort(sort.Reverse(byValueSorter))
|
|
|
|
return Vector(byValueSorter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// === topk(k ScalarNode, node VectorNode) Vector ===
|
2014-06-06 02:55:53 -07:00
|
|
|
func topkImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
k := int(args[0].(ScalarNode).Eval(timestamp))
|
2014-08-05 09:57:47 -07:00
|
|
|
if k < 1 {
|
|
|
|
return Vector{}
|
|
|
|
}
|
|
|
|
|
|
|
|
topk := make(vectorByValueHeap, 0, k)
|
2014-06-06 02:55:53 -07:00
|
|
|
vector := args[1].(VectorNode).Eval(timestamp)
|
2014-08-05 09:57:47 -07:00
|
|
|
|
|
|
|
for _, el := range vector {
|
|
|
|
if len(topk) < k || topk[0].Value < el.Value {
|
|
|
|
if len(topk) == k {
|
|
|
|
heap.Pop(&topk)
|
|
|
|
}
|
|
|
|
heap.Push(&topk, el)
|
|
|
|
}
|
2013-04-10 01:44:13 -07:00
|
|
|
}
|
2014-08-05 09:57:47 -07:00
|
|
|
sort.Sort(sort.Reverse(topk))
|
|
|
|
return Vector(topk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// === bottomk(k ScalarNode, node VectorNode) Vector ===
|
2014-06-06 02:55:53 -07:00
|
|
|
func bottomkImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
k := int(args[0].(ScalarNode).Eval(timestamp))
|
2014-08-05 09:57:47 -07:00
|
|
|
if k < 1 {
|
|
|
|
return Vector{}
|
|
|
|
}
|
|
|
|
|
|
|
|
bottomk := make(vectorByValueHeap, 0, k)
|
|
|
|
bkHeap := reverseHeap{Interface: &bottomk}
|
2014-06-06 02:55:53 -07:00
|
|
|
vector := args[1].(VectorNode).Eval(timestamp)
|
2014-08-05 09:57:47 -07:00
|
|
|
|
|
|
|
for _, el := range vector {
|
|
|
|
if len(bottomk) < k || bottomk[0].Value > el.Value {
|
|
|
|
if len(bottomk) == k {
|
|
|
|
heap.Pop(&bkHeap)
|
|
|
|
}
|
|
|
|
heap.Push(&bkHeap, el)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Sort(bottomk)
|
|
|
|
return Vector(bottomk)
|
2013-04-10 01:44:13 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 10:56:35 -07:00
|
|
|
// === drop_common_labels(node VectorNode) Vector ===
|
2014-06-06 02:55:53 -07:00
|
|
|
func dropCommonLabelsImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
vector := args[0].(VectorNode).Eval(timestamp)
|
2014-08-05 10:56:35 -07:00
|
|
|
if len(vector) < 1 {
|
|
|
|
return Vector{}
|
|
|
|
}
|
|
|
|
common := clientmodel.LabelSet{}
|
2014-12-08 07:55:49 -08:00
|
|
|
for k, v := range vector[0].Metric.Metric {
|
2014-12-03 09:07:23 -08:00
|
|
|
// TODO(julius): Should we also drop common metric names?
|
2014-08-05 10:56:35 -07:00
|
|
|
if k == clientmodel.MetricNameLabel {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
common[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, el := range vector[1:] {
|
|
|
|
for k, v := range common {
|
2014-12-08 07:55:49 -08:00
|
|
|
if el.Metric.Metric[k] != v {
|
2014-08-05 10:56:35 -07:00
|
|
|
// Deletion of map entries while iterating over them is safe.
|
|
|
|
// From http://golang.org/ref/spec#For_statements:
|
|
|
|
// "If map entries that have not yet been reached are deleted during
|
|
|
|
// iteration, the corresponding iteration values will not be produced."
|
|
|
|
delete(common, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, el := range vector {
|
2014-12-08 07:55:49 -08:00
|
|
|
for k := range el.Metric.Metric {
|
2014-08-05 10:56:35 -07:00
|
|
|
if _, ok := common[k]; ok {
|
2014-12-08 07:55:49 -08:00
|
|
|
el.Metric.Delete(k)
|
2014-08-05 10:56:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
2015-02-05 08:06:35 -08:00
|
|
|
// === round(vector VectorNode, toNearest=1 Scalar) Vector ===
|
2015-02-04 03:24:00 -08:00
|
|
|
func roundImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
2015-02-05 08:06:35 -08:00
|
|
|
// round returns a number rounded to toNearest.
|
|
|
|
// Ties are solved by rounding up.
|
|
|
|
toNearest := float64(1)
|
2015-02-04 03:24:00 -08:00
|
|
|
if len(args) >= 2 {
|
2015-02-05 08:06:35 -08:00
|
|
|
toNearest = float64(args[1].(ScalarNode).Eval(timestamp))
|
2015-02-04 03:24:00 -08:00
|
|
|
}
|
2015-02-05 08:06:35 -08:00
|
|
|
// Invert as it seems to cause fewer floating point accuracy issues.
|
|
|
|
toNearestInverse := 1.0 / toNearest
|
2015-02-04 03:24:00 -08:00
|
|
|
|
|
|
|
n := args[0].(VectorNode)
|
|
|
|
vector := n.Eval(timestamp)
|
|
|
|
for _, el := range vector {
|
|
|
|
el.Metric.Delete(clientmodel.MetricNameLabel)
|
2015-02-05 08:06:35 -08:00
|
|
|
el.Value = clientmodel.SampleValue(math.Floor(float64(el.Value)*toNearestInverse+0.5) / toNearestInverse)
|
2015-02-04 03:24:00 -08:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
2014-08-05 09:57:47 -07:00
|
|
|
// === scalar(node VectorNode) Scalar ===
|
2014-06-06 02:55:53 -07:00
|
|
|
func scalarImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
v := args[0].(VectorNode).Eval(timestamp)
|
2013-09-17 05:59:31 -07:00
|
|
|
if len(v) != 1 {
|
|
|
|
return clientmodel.SampleValue(math.NaN())
|
|
|
|
}
|
|
|
|
return clientmodel.SampleValue(v[0].Value)
|
|
|
|
}
|
|
|
|
|
2014-01-30 04:07:26 -08:00
|
|
|
// === count_scalar(vector VectorNode) model.SampleValue ===
|
2014-06-06 02:55:53 -07:00
|
|
|
func countScalarImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
return clientmodel.SampleValue(len(args[0].(VectorNode).Eval(timestamp)))
|
2014-01-30 04:07:26 -08:00
|
|
|
}
|
|
|
|
|
2014-06-06 02:55:53 -07:00
|
|
|
func aggrOverTime(timestamp clientmodel.Timestamp, args []Node, aggrFn func(metric.Values) clientmodel.SampleValue) interface{} {
|
2014-07-28 07:12:58 -07:00
|
|
|
n := args[0].(MatrixNode)
|
2014-06-06 02:55:53 -07:00
|
|
|
matrixVal := n.Eval(timestamp)
|
2014-07-28 07:12:58 -07:00
|
|
|
resultVector := Vector{}
|
|
|
|
|
|
|
|
for _, el := range matrixVal {
|
|
|
|
if len(el.Values) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-12-08 07:55:49 -08:00
|
|
|
el.Metric.Delete(clientmodel.MetricNameLabel)
|
|
|
|
resultVector = append(resultVector, &Sample{
|
2014-07-28 07:12:58 -07:00
|
|
|
Metric: el.Metric,
|
|
|
|
Value: aggrFn(el.Values),
|
|
|
|
Timestamp: timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return resultVector
|
|
|
|
}
|
|
|
|
|
|
|
|
// === avg_over_time(matrix MatrixNode) Vector ===
|
2014-06-06 02:55:53 -07:00
|
|
|
func avgOverTimeImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
return aggrOverTime(timestamp, args, func(values metric.Values) clientmodel.SampleValue {
|
2014-07-28 07:12:58 -07:00
|
|
|
var sum clientmodel.SampleValue
|
|
|
|
for _, v := range values {
|
|
|
|
sum += v.Value
|
|
|
|
}
|
|
|
|
return sum / clientmodel.SampleValue(len(values))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// === count_over_time(matrix MatrixNode) Vector ===
|
2014-06-06 02:55:53 -07:00
|
|
|
func countOverTimeImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
return aggrOverTime(timestamp, args, func(values metric.Values) clientmodel.SampleValue {
|
2014-07-28 07:12:58 -07:00
|
|
|
return clientmodel.SampleValue(len(values))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-02-04 03:24:00 -08:00
|
|
|
// === floor(vector VectorNode) Vector ===
|
|
|
|
func floorImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
n := args[0].(VectorNode)
|
|
|
|
vector := n.Eval(timestamp)
|
|
|
|
for _, el := range vector {
|
|
|
|
el.Metric.Delete(clientmodel.MetricNameLabel)
|
|
|
|
el.Value = clientmodel.SampleValue(math.Floor(float64(el.Value)))
|
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
2014-07-28 07:12:58 -07:00
|
|
|
// === max_over_time(matrix MatrixNode) Vector ===
|
2014-06-06 02:55:53 -07:00
|
|
|
func maxOverTimeImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
return aggrOverTime(timestamp, args, func(values metric.Values) clientmodel.SampleValue {
|
2014-07-28 07:12:58 -07:00
|
|
|
max := math.Inf(-1)
|
|
|
|
for _, v := range values {
|
|
|
|
max = math.Max(max, float64(v.Value))
|
|
|
|
}
|
|
|
|
return clientmodel.SampleValue(max)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// === min_over_time(matrix MatrixNode) Vector ===
|
2014-06-06 02:55:53 -07:00
|
|
|
func minOverTimeImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
return aggrOverTime(timestamp, args, func(values metric.Values) clientmodel.SampleValue {
|
2014-07-28 07:12:58 -07:00
|
|
|
min := math.Inf(1)
|
|
|
|
for _, v := range values {
|
|
|
|
min = math.Min(min, float64(v.Value))
|
|
|
|
}
|
|
|
|
return clientmodel.SampleValue(min)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// === sum_over_time(matrix MatrixNode) Vector ===
|
2014-06-06 02:55:53 -07:00
|
|
|
func sumOverTimeImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
return aggrOverTime(timestamp, args, func(values metric.Values) clientmodel.SampleValue {
|
2014-07-28 07:12:58 -07:00
|
|
|
var sum clientmodel.SampleValue
|
|
|
|
for _, v := range values {
|
|
|
|
sum += v.Value
|
|
|
|
}
|
|
|
|
return sum
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// === abs(vector VectorNode) Vector ===
|
2014-06-06 02:55:53 -07:00
|
|
|
func absImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
2014-07-28 07:12:58 -07:00
|
|
|
n := args[0].(VectorNode)
|
2014-06-06 02:55:53 -07:00
|
|
|
vector := n.Eval(timestamp)
|
2014-07-28 07:12:58 -07:00
|
|
|
for _, el := range vector {
|
2014-12-08 07:55:49 -08:00
|
|
|
el.Metric.Delete(clientmodel.MetricNameLabel)
|
2014-07-28 07:12:58 -07:00
|
|
|
el.Value = clientmodel.SampleValue(math.Abs(float64(el.Value)))
|
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
Add absent() function.
A common problem in Prometheus alerting is to detect when no timeseries
exist for a given metric name and label combination. Unfortunately,
Prometheus alert expressions need to be of vector type, and
"count(nonexistent_metric)" results in an empty vector, yielding no
output vector elements to base an alert on. The newly introduced
absent() function solves this issue:
ALERT FooAbsent IF absent(foo{job="myjob"}) [...]
absent() has the following behavior:
- if the vector passed to it has any elements, it returns an empty
vector.
- if the vector passed to it has no elements, it returns a 1-element
vector with the value 1.
In the second case, absent() tries to be smart about deriving labels of
the 1-element output vector from the input vector:
absent(nonexistent{job="myjob"}) => {job="myjob"}
absent(nonexistent{job="myjob",instance=~".*"}) => {job="myjob"}
absent(sum(nonexistent{job="myjob"})) => {}
That is, if the passed vector is a literal vector selector, it takes all
"=" label matchers as the basis for the output labels, but ignores all
non-equals or regex matchers. Also, if the passed vector results from a
non-selector expression, no labels can be derived.
Change-Id: I948505a1488d50265ab5692a3286bd7c8c70cd78
2014-10-15 10:13:52 -07:00
|
|
|
// === absent(vector VectorNode) Vector ===
|
|
|
|
func absentImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
n := args[0].(VectorNode)
|
|
|
|
if len(n.Eval(timestamp)) > 0 {
|
|
|
|
return Vector{}
|
|
|
|
}
|
|
|
|
m := clientmodel.Metric{}
|
|
|
|
if vs, ok := n.(*VectorSelector); ok {
|
|
|
|
for _, matcher := range vs.labelMatchers {
|
|
|
|
if matcher.Type == metric.Equal && matcher.Name != clientmodel.MetricNameLabel {
|
|
|
|
m[matcher.Name] = matcher.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Vector{
|
2014-12-08 07:55:49 -08:00
|
|
|
&Sample{
|
|
|
|
Metric: clientmodel.COWMetric{
|
|
|
|
Metric: m,
|
|
|
|
Copied: true,
|
|
|
|
},
|
Add absent() function.
A common problem in Prometheus alerting is to detect when no timeseries
exist for a given metric name and label combination. Unfortunately,
Prometheus alert expressions need to be of vector type, and
"count(nonexistent_metric)" results in an empty vector, yielding no
output vector elements to base an alert on. The newly introduced
absent() function solves this issue:
ALERT FooAbsent IF absent(foo{job="myjob"}) [...]
absent() has the following behavior:
- if the vector passed to it has any elements, it returns an empty
vector.
- if the vector passed to it has no elements, it returns a 1-element
vector with the value 1.
In the second case, absent() tries to be smart about deriving labels of
the 1-element output vector from the input vector:
absent(nonexistent{job="myjob"}) => {job="myjob"}
absent(nonexistent{job="myjob",instance=~".*"}) => {job="myjob"}
absent(sum(nonexistent{job="myjob"})) => {}
That is, if the passed vector is a literal vector selector, it takes all
"=" label matchers as the basis for the output labels, but ignores all
non-equals or regex matchers. Also, if the passed vector results from a
non-selector expression, no labels can be derived.
Change-Id: I948505a1488d50265ab5692a3286bd7c8c70cd78
2014-10-15 10:13:52 -07:00
|
|
|
Value: 1,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 03:24:00 -08:00
|
|
|
// === ceil(vector VectorNode) Vector ===
|
|
|
|
func ceilImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
n := args[0].(VectorNode)
|
|
|
|
vector := n.Eval(timestamp)
|
|
|
|
for _, el := range vector {
|
|
|
|
el.Metric.Delete(clientmodel.MetricNameLabel)
|
|
|
|
el.Value = clientmodel.SampleValue(math.Ceil(float64(el.Value)))
|
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
2015-01-22 03:52:30 -08:00
|
|
|
// === deriv(node MatrixNode) Vector ===
|
|
|
|
func derivImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
matrixNode := args[0].(MatrixNode)
|
|
|
|
resultVector := Vector{}
|
|
|
|
|
|
|
|
matrixValue := matrixNode.Eval(timestamp)
|
|
|
|
for _, samples := range matrixValue {
|
|
|
|
// No sense in trying to compute a derivative without at least two points.
|
|
|
|
// Drop this vector element.
|
|
|
|
if len(samples.Values) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Least squares.
|
|
|
|
n := clientmodel.SampleValue(0)
|
|
|
|
sumY := clientmodel.SampleValue(0)
|
|
|
|
sumX := clientmodel.SampleValue(0)
|
|
|
|
sumXY := clientmodel.SampleValue(0)
|
|
|
|
sumX2 := clientmodel.SampleValue(0)
|
|
|
|
for _, sample := range samples.Values {
|
|
|
|
x := clientmodel.SampleValue(sample.Timestamp.UnixNano() / 1e9)
|
|
|
|
n += 1.0
|
|
|
|
sumY += sample.Value
|
|
|
|
sumX += x
|
|
|
|
sumXY += x * sample.Value
|
|
|
|
sumX2 += x * x
|
|
|
|
}
|
|
|
|
numerator := sumXY - sumX*sumY/n
|
|
|
|
denominator := sumX2 - (sumX*sumX)/n
|
|
|
|
|
|
|
|
resultValue := numerator / denominator
|
|
|
|
|
|
|
|
resultSample := &Sample{
|
|
|
|
Metric: samples.Metric,
|
|
|
|
Value: resultValue,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
}
|
|
|
|
resultSample.Metric.Delete(clientmodel.MetricNameLabel)
|
|
|
|
resultVector = append(resultVector, resultSample)
|
|
|
|
}
|
|
|
|
return resultVector
|
|
|
|
}
|
|
|
|
|
2015-02-19 10:56:45 -08:00
|
|
|
// === histogram_quantile(k ScalarNode, vector VectorNode) Vector ===
|
|
|
|
func histogramQuantileImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
|
|
|
q := args[0].(ScalarNode).Eval(timestamp)
|
|
|
|
inVec := args[1].(VectorNode).Eval(timestamp)
|
|
|
|
outVec := Vector{}
|
2015-03-03 08:31:49 -08:00
|
|
|
signatureToMetricWithBuckets := map[uint64]*metricWithBuckets{}
|
2015-02-19 10:56:45 -08:00
|
|
|
for _, el := range inVec {
|
|
|
|
upperBound, err := strconv.ParseFloat(
|
|
|
|
string(el.Metric.Metric[clientmodel.BucketLabel]), 64,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
// Oops, no bucket label or malformed label value. Skip.
|
|
|
|
// TODO(beorn7): Issue a warning somehow.
|
|
|
|
continue
|
|
|
|
}
|
2015-03-03 08:31:49 -08:00
|
|
|
signature := clientmodel.SignatureWithoutLabels(el.Metric.Metric, excludedLabels)
|
|
|
|
mb, ok := signatureToMetricWithBuckets[signature]
|
2015-02-19 10:56:45 -08:00
|
|
|
if !ok {
|
2015-02-21 15:25:02 -08:00
|
|
|
el.Metric.Delete(clientmodel.BucketLabel)
|
|
|
|
el.Metric.Delete(clientmodel.MetricNameLabel)
|
2015-02-19 10:56:45 -08:00
|
|
|
mb = &metricWithBuckets{el.Metric, nil}
|
2015-03-03 08:31:49 -08:00
|
|
|
signatureToMetricWithBuckets[signature] = mb
|
2015-02-19 10:56:45 -08:00
|
|
|
}
|
|
|
|
mb.buckets = append(mb.buckets, bucket{upperBound, el.Value})
|
|
|
|
}
|
|
|
|
|
2015-03-03 08:31:49 -08:00
|
|
|
for _, mb := range signatureToMetricWithBuckets {
|
2015-02-19 10:56:45 -08:00
|
|
|
outVec = append(outVec, &Sample{
|
|
|
|
Metric: mb.metric,
|
|
|
|
Value: clientmodel.SampleValue(quantile(q, mb.buckets)),
|
|
|
|
Timestamp: timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return outVec
|
|
|
|
}
|
|
|
|
|
2013-01-07 14:24:26 -08:00
|
|
|
var functions = map[string]*Function{
|
2014-07-28 07:12:58 -07:00
|
|
|
"abs": {
|
|
|
|
name: "abs",
|
2014-12-24 16:28:35 -08:00
|
|
|
argTypes: []ExprType{VectorType},
|
|
|
|
returnType: VectorType,
|
2014-07-28 07:12:58 -07:00
|
|
|
callFn: absImpl,
|
|
|
|
},
|
Add absent() function.
A common problem in Prometheus alerting is to detect when no timeseries
exist for a given metric name and label combination. Unfortunately,
Prometheus alert expressions need to be of vector type, and
"count(nonexistent_metric)" results in an empty vector, yielding no
output vector elements to base an alert on. The newly introduced
absent() function solves this issue:
ALERT FooAbsent IF absent(foo{job="myjob"}) [...]
absent() has the following behavior:
- if the vector passed to it has any elements, it returns an empty
vector.
- if the vector passed to it has no elements, it returns a 1-element
vector with the value 1.
In the second case, absent() tries to be smart about deriving labels of
the 1-element output vector from the input vector:
absent(nonexistent{job="myjob"}) => {job="myjob"}
absent(nonexistent{job="myjob",instance=~".*"}) => {job="myjob"}
absent(sum(nonexistent{job="myjob"})) => {}
That is, if the passed vector is a literal vector selector, it takes all
"=" label matchers as the basis for the output labels, but ignores all
non-equals or regex matchers. Also, if the passed vector results from a
non-selector expression, no labels can be derived.
Change-Id: I948505a1488d50265ab5692a3286bd7c8c70cd78
2014-10-15 10:13:52 -07:00
|
|
|
"absent": {
|
|
|
|
name: "absent",
|
2014-12-24 16:28:35 -08:00
|
|
|
argTypes: []ExprType{VectorType},
|
|
|
|
returnType: VectorType,
|
Add absent() function.
A common problem in Prometheus alerting is to detect when no timeseries
exist for a given metric name and label combination. Unfortunately,
Prometheus alert expressions need to be of vector type, and
"count(nonexistent_metric)" results in an empty vector, yielding no
output vector elements to base an alert on. The newly introduced
absent() function solves this issue:
ALERT FooAbsent IF absent(foo{job="myjob"}) [...]
absent() has the following behavior:
- if the vector passed to it has any elements, it returns an empty
vector.
- if the vector passed to it has no elements, it returns a 1-element
vector with the value 1.
In the second case, absent() tries to be smart about deriving labels of
the 1-element output vector from the input vector:
absent(nonexistent{job="myjob"}) => {job="myjob"}
absent(nonexistent{job="myjob",instance=~".*"}) => {job="myjob"}
absent(sum(nonexistent{job="myjob"})) => {}
That is, if the passed vector is a literal vector selector, it takes all
"=" label matchers as the basis for the output labels, but ignores all
non-equals or regex matchers. Also, if the passed vector results from a
non-selector expression, no labels can be derived.
Change-Id: I948505a1488d50265ab5692a3286bd7c8c70cd78
2014-10-15 10:13:52 -07:00
|
|
|
callFn: absentImpl,
|
|
|
|
},
|
2014-07-28 07:12:58 -07:00
|
|
|
"avg_over_time": {
|
|
|
|
name: "avg_over_time",
|
2014-12-24 16:28:35 -08:00
|
|
|
argTypes: []ExprType{MatrixType},
|
|
|
|
returnType: VectorType,
|
2014-07-28 07:12:58 -07:00
|
|
|
callFn: avgOverTimeImpl,
|
|
|
|
},
|
2014-08-05 09:57:47 -07:00
|
|
|
"bottomk": {
|
|
|
|
name: "bottomk",
|
2014-12-24 16:28:35 -08:00
|
|
|
argTypes: []ExprType{ScalarType, VectorType},
|
|
|
|
returnType: VectorType,
|
2014-08-05 09:57:47 -07:00
|
|
|
callFn: bottomkImpl,
|
|
|
|
},
|
2015-02-04 03:24:00 -08:00
|
|
|
"ceil": {
|
|
|
|
name: "ceil",
|
|
|
|
argTypes: []ExprType{VectorType},
|
|
|
|
returnType: VectorType,
|
|
|
|
callFn: ceilImpl,
|
|
|
|
},
|
2014-07-28 07:12:58 -07:00
|
|
|
"count_over_time": {
|
|
|
|
name: "count_over_time",
|
2014-12-24 16:28:35 -08:00
|
|
|
argTypes: []ExprType{MatrixType},
|
|
|
|
returnType: VectorType,
|
2014-07-28 07:12:58 -07:00
|
|
|
callFn: countOverTimeImpl,
|
|
|
|
},
|
2014-01-30 04:07:26 -08:00
|
|
|
"count_scalar": {
|
|
|
|
name: "count_scalar",
|
2014-12-24 16:28:35 -08:00
|
|
|
argTypes: []ExprType{VectorType},
|
|
|
|
returnType: ScalarType,
|
2014-01-30 04:07:26 -08:00
|
|
|
callFn: countScalarImpl,
|
|
|
|
},
|
2013-01-07 14:24:26 -08:00
|
|
|
"delta": {
|
2015-01-22 03:52:30 -08:00
|
|
|
name: "delta",
|
|
|
|
argTypes: []ExprType{MatrixType, ScalarType},
|
|
|
|
optionalArgs: 1, // The 2nd argument is deprecated.
|
|
|
|
returnType: VectorType,
|
|
|
|
callFn: deltaImpl,
|
2013-01-07 14:24:26 -08:00
|
|
|
},
|
2015-02-19 10:56:45 -08:00
|
|
|
"deriv": {
|
|
|
|
name: "deriv",
|
|
|
|
argTypes: []ExprType{MatrixType},
|
|
|
|
returnType: VectorType,
|
|
|
|
callFn: derivImpl,
|
|
|
|
},
|
2014-08-05 10:56:35 -07:00
|
|
|
"drop_common_labels": {
|
|
|
|
name: "drop_common_labels",
|
2014-12-24 16:28:35 -08:00
|
|
|
argTypes: []ExprType{VectorType},
|
|
|
|
returnType: VectorType,
|
2014-08-05 10:56:35 -07:00
|
|
|
callFn: dropCommonLabelsImpl,
|
|
|
|
},
|
2015-02-04 03:24:00 -08:00
|
|
|
"floor": {
|
|
|
|
name: "floor",
|
|
|
|
argTypes: []ExprType{VectorType},
|
|
|
|
returnType: VectorType,
|
|
|
|
callFn: floorImpl,
|
|
|
|
},
|
2015-02-19 10:56:45 -08:00
|
|
|
"histogram_quantile": {
|
|
|
|
name: "histogram_quantile",
|
|
|
|
argTypes: []ExprType{ScalarType, VectorType},
|
|
|
|
returnType: VectorType,
|
|
|
|
callFn: histogramQuantileImpl,
|
|
|
|
},
|
2014-07-28 07:12:58 -07:00
|
|
|
"max_over_time": {
|
|
|
|
name: "max_over_time",
|
2014-12-24 16:28:35 -08:00
|
|
|
argTypes: []ExprType{MatrixType},
|
|
|
|
returnType: VectorType,
|
2014-07-28 07:12:58 -07:00
|
|
|
callFn: maxOverTimeImpl,
|
|
|
|
},
|
|
|
|
"min_over_time": {
|
|
|
|
name: "min_over_time",
|
2014-12-24 16:28:35 -08:00
|
|
|
argTypes: []ExprType{MatrixType},
|
|
|
|
returnType: VectorType,
|
2014-07-28 07:12:58 -07:00
|
|
|
callFn: minOverTimeImpl,
|
|
|
|
},
|
2013-01-07 14:24:26 -08:00
|
|
|
"rate": {
|
|
|
|
name: "rate",
|
2014-12-24 16:28:35 -08:00
|
|
|
argTypes: []ExprType{MatrixType},
|
|
|
|
returnType: VectorType,
|
2013-01-07 14:24:26 -08:00
|
|
|
callFn: rateImpl,
|
|
|
|
},
|
2015-02-04 03:24:00 -08:00
|
|
|
"round": {
|
|
|
|
name: "round",
|
|
|
|
argTypes: []ExprType{VectorType, ScalarType},
|
|
|
|
optionalArgs: 1,
|
|
|
|
returnType: VectorType,
|
|
|
|
callFn: roundImpl,
|
|
|
|
},
|
2013-09-17 05:59:31 -07:00
|
|
|
"scalar": {
|
|
|
|
name: "scalar",
|
2014-12-24 16:28:35 -08:00
|
|
|
argTypes: []ExprType{VectorType},
|
|
|
|
returnType: ScalarType,
|
2013-09-17 05:59:31 -07:00
|
|
|
callFn: scalarImpl,
|
|
|
|
},
|
2013-04-10 01:44:13 -07:00
|
|
|
"sort": {
|
|
|
|
name: "sort",
|
2014-12-24 16:28:35 -08:00
|
|
|
argTypes: []ExprType{VectorType},
|
|
|
|
returnType: VectorType,
|
2013-04-10 01:44:13 -07:00
|
|
|
callFn: sortImpl,
|
|
|
|
},
|
|
|
|
"sort_desc": {
|
|
|
|
name: "sort_desc",
|
2014-12-24 16:28:35 -08:00
|
|
|
argTypes: []ExprType{VectorType},
|
|
|
|
returnType: VectorType,
|
2013-04-10 01:44:13 -07:00
|
|
|
callFn: sortDescImpl,
|
|
|
|
},
|
2014-07-28 07:12:58 -07:00
|
|
|
"sum_over_time": {
|
|
|
|
name: "sum_over_time",
|
2014-12-24 16:28:35 -08:00
|
|
|
argTypes: []ExprType{MatrixType},
|
|
|
|
returnType: VectorType,
|
2014-07-28 07:12:58 -07:00
|
|
|
callFn: sumOverTimeImpl,
|
|
|
|
},
|
2013-04-10 01:44:13 -07:00
|
|
|
"time": {
|
|
|
|
name: "time",
|
|
|
|
argTypes: []ExprType{},
|
2014-12-24 16:28:35 -08:00
|
|
|
returnType: ScalarType,
|
2013-04-10 01:44:13 -07:00
|
|
|
callFn: timeImpl,
|
|
|
|
},
|
2014-08-05 09:57:47 -07:00
|
|
|
"topk": {
|
|
|
|
name: "topk",
|
2014-12-24 16:28:35 -08:00
|
|
|
argTypes: []ExprType{ScalarType, VectorType},
|
|
|
|
returnType: VectorType,
|
2014-08-05 09:57:47 -07:00
|
|
|
callFn: topkImpl,
|
|
|
|
},
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2014-02-13 09:48:56 -08:00
|
|
|
// GetFunction returns a predefined Function object for the given
|
|
|
|
// name.
|
2013-01-07 14:24:26 -08:00
|
|
|
func GetFunction(name string) (*Function, error) {
|
|
|
|
function, ok := functions[name]
|
|
|
|
if !ok {
|
2014-02-13 09:48:56 -08:00
|
|
|
return nil, fmt.Errorf("couldn't find function %v()", name)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
return function, nil
|
|
|
|
}
|