SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
feat: add abs value function in formula (#9315)
Niladri Adhikary43a6c7dcd618
diff --git a/pkg/query-service/app/queryBuilder/query_builder.go b/pkg/query-service/app/queryBuilder/query_builder.goindex 2a9aa2a5e5..9f119d4945 100644--- a/pkg/query-service/app/queryBuilder/query_builder.go+++ b/pkg/query-service/app/queryBuilder/query_builder.go@@ -13,6 +13,7 @@ import ()var SupportedFunctions = []string{+ "abs","exp","log","ln",diff --git a/pkg/query-service/postprocess/formula.go b/pkg/query-service/postprocess/formula.goindex 6d09a4d7b1..4e665dd216 100644--- a/pkg/query-service/postprocess/formula.go+++ b/pkg/query-service/postprocess/formula.go@@ -197,10 +197,14 @@ func processResults(}, nil}-var SupportedFunctions = []string{"exp", "log", "ln", "exp2", "log2", "exp10", "log10", "sqrt", "cbrt", "erf", "erfc", "lgamma", "tgamma", "sin", "cos", "tan", "asin", "acos", "atan", "degrees", "radians", "now", "toUnixTimestamp"}+var SupportedFunctions = []string{"abs", "exp", "log", "ln", "exp2", "log2", "exp10", "log10", "sqrt", "cbrt", "erf", "erfc", "lgamma", "tgamma", "sin", "cos", "tan", "asin", "acos", "atan", "degrees", "radians", "now", "toUnixTimestamp"}func EvalFuncs() map[string]govaluate.ExpressionFunction {GoValuateFuncs := make(map[string]govaluate.ExpressionFunction)+ // Returns the absolute value of the given argument.+ GoValuateFuncs["abs"] = func(args ...interface{}) (interface{}, error) {+ return math.Abs(args[0].(float64)), nil+ }// Returns e to the power of the given argument.GoValuateFuncs["exp"] = func(args ...interface{}) (interface{}, error) {return math.Exp(args[0].(float64)), nildiff --git a/pkg/types/querybuildertypes/querybuildertypesv5/formula.go b/pkg/types/querybuildertypes/querybuildertypesv5/formula.goindex beb1d14b55..bd010fb56f 100644--- a/pkg/types/querybuildertypes/querybuildertypesv5/formula.go+++ b/pkg/types/querybuildertypes/querybuildertypesv5/formula.go@@ -545,6 +545,9 @@ func EvalFuncs() map[string]govaluate.ExpressionFunction {rad180 := 180 / math.Pi// Mathematical functions+ funcs["abs"] = func(args ...any) (any, error) {+ return math.Abs(args[0].(float64)), nil+ }funcs["exp"] = func(args ...any) (any, error) {return math.Exp(args[0].(float64)), nil}@@ -623,7 +626,7 @@ func EvalFuncs() map[string]govaluate.ExpressionFunction {// GetSupportedFunctions returns the list of supported function namesfunc GetSupportedFunctions() []string {return []string{- "exp", "log", "ln", "exp2", "log2", "exp10", "log10",+ "abs", "exp", "log", "ln", "exp2", "log2", "exp10", "log10","sqrt", "cbrt", "erf", "erfc", "lgamma", "tgamma","sin", "cos", "tan", "asin", "acos", "atan","degrees", "radians", "now",diff --git a/pkg/types/querybuildertypes/querybuildertypesv5/formula_test.go b/pkg/types/querybuildertypes/querybuildertypesv5/formula_test.goindex 74361619b2..bcae592f07 100644--- a/pkg/types/querybuildertypes/querybuildertypesv5/formula_test.go+++ b/pkg/types/querybuildertypes/querybuildertypesv5/formula_test.go@@ -863,3 +863,39 @@ func TestComplexExpression(t *testing.T) {}}}++func TestAbsValueExpression(t *testing.T) {+ tsData := map[string]*TimeSeriesData{+ "A": createFormulaTestTimeSeriesData("A", []*TimeSeries{+ {+ Labels: createLabels(map[string]string{"service_name": "frontend"}),+ Values: createValues(map[int64]float64{+ 1: -10,+ 2: 20,+ }),+ },+ }),+ "B": createFormulaTestTimeSeriesData("B", []*TimeSeries{+ {+ Labels: createLabels(map[string]string{"service_name": "frontend"}),+ Values: createValues(map[int64]float64{+ 1: 5,+ 2: -4,+ }),+ },+ }),+ }++ evaluator, err := NewFormulaEvaluator("abs(A) + abs(B)", map[string]bool{"A": true, "B": true})+ require.NoError(t, err)++ result, err := evaluator.EvaluateFormula(tsData)+ require.NoError(t, err)+ require.NotNil(t, result)+ require.Equal(t, 1, len(result))++ series := result[0]+ require.Equal(t, 2, len(series.Values))+ assert.Equal(t, 15.0, series.Values[0].Value) // |−10| + |5| = 15+ assert.Equal(t, 24.0, series.Values[1].Value) // |20| + |−4| = 24+}