Merge pull request #640 from prometheus/pow

Add square root function
This commit is contained in:
Julius Volz 2015-04-17 00:23:33 +02:00
commit f35e5b54cc
2 changed files with 24 additions and 0 deletions

View file

@ -500,6 +500,17 @@ func log10Impl(timestamp clientmodel.Timestamp, args []Node) interface{} {
return vector
}
// === sqrt(vector VectorNode) Vector ===
func sqrtImpl(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.Sqrt(float64(el.Value)))
}
return vector
}
// === deriv(node MatrixNode) Vector ===
func derivImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
matrixNode := args[0].(MatrixNode)
@ -721,6 +732,12 @@ var functions = map[string]*Function{
returnType: VectorType,
callFn: sortDescImpl,
},
"sqrt": {
name: "sqrt",
argTypes: []ExprType{VectorType},
returnType: VectorType,
callFn: sqrtImpl,
},
"sum_over_time": {
name: "sum_over_time",
argTypes: []ExprType{MatrixType},

View file

@ -1197,6 +1197,13 @@ func TestExpressions(t *testing.T) {
`{group="canary", instance="0", job="api-server"} => NaN @[%v]`,
},
},
{
expr: `sqrt(vector_matching_a)`,
output: []string{
`{l="x"} => 3.1622776601683795 @[%v]`,
`{l="y"} => 4.47213595499958 @[%v]`,
},
},
{
expr: `exp(vector_matching_a)`,
output: []string{