diff --git a/rules/ast/functions.go b/rules/ast/functions.go index ea95a1966..5c7ae096a 100644 --- a/rules/ast/functions.go +++ b/rules/ast/functions.go @@ -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}, diff --git a/rules/rules_test.go b/rules/rules_test.go index 5966c21ec..1ff76a070 100644 --- a/rules/rules_test.go +++ b/rules/rules_test.go @@ -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{