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: implement case-insensitive query name handling in formula evaluation (#9302)
Niladri Adhikaryb9f9c00da565
diff --git a/pkg/types/querybuildertypes/querybuildertypesv5/formula.go b/pkg/types/querybuildertypes/querybuildertypesv5/formula.goindex bd010fb56f..01e9393462 100644--- a/pkg/types/querybuildertypes/querybuildertypesv5/formula.go+++ b/pkg/types/querybuildertypes/querybuildertypesv5/formula.go@@ -153,10 +153,28 @@ func NewFormulaEvaluator(expressionStr string, canDefaultZero map[string]bool) (return nil, errors.NewInvalidInputf(errors.CodeInvalidInput, "failed to parse expression")}+ // Normalize canDefaultZero keys to match variable casing from expression+ normalizedCanDefaultZero := make(map[string]bool)+ vars := expression.Vars()+ for _, variable := range vars {+ // If exact match exists, use it+ if val, ok := canDefaultZero[variable]; ok {+ normalizedCanDefaultZero[variable] = val+ continue+ }+ // Otherwise try case-insensitive lookup+ for k, v := range canDefaultZero {+ if strings.EqualFold(k, variable) {+ normalizedCanDefaultZero[variable] = v+ break+ }+ }+ }+evaluator := &FormulaEvaluator{expression: expression,- variables: expression.Vars(),- canDefaultZero: canDefaultZero,+ variables: vars,+ canDefaultZero: normalizedCanDefaultZero,aggRefs: make(map[string]aggregationRef),}@@ -281,6 +299,16 @@ func (fe *FormulaEvaluator) buildSeriesLookup(timeSeriesData map[string]*TimeSer// We are only interested in the time series data for the queries that are// involved in the formula expression.data, exists := timeSeriesData[aggRef.QueryName]+ if !exists {+ // try case-insensitive lookup+ for k, v := range timeSeriesData {+ if strings.EqualFold(k, aggRef.QueryName) {+ data = v+ exists = true+ break+ }+ }+ }if !exists {continue}diff --git a/pkg/types/querybuildertypes/querybuildertypesv5/formula_test.go b/pkg/types/querybuildertypes/querybuildertypesv5/formula_test.goindex bcae592f07..c41807cfff 100644--- a/pkg/types/querybuildertypes/querybuildertypesv5/formula_test.go+++ b/pkg/types/querybuildertypes/querybuildertypesv5/formula_test.go@@ -864,6 +864,158 @@ func TestComplexExpression(t *testing.T) {}}+func TestCaseInsensitiveQueryNames(t *testing.T) {+ tests := []struct {+ name string+ expression string+ tsData map[string]*TimeSeriesData+ expectedValues []float64+ }{+ {+ name: "lowercase query names",+ expression: "a / b",+ tsData: map[string]*TimeSeriesData{+ "A": createFormulaTestTimeSeriesData("A", []*TimeSeries{+ {+ Labels: createLabels(map[string]string{}),+ Values: createValues(map[int64]float64{1: 10}),+ },+ }),+ "B": createFormulaTestTimeSeriesData("B", []*TimeSeries{+ {+ Labels: createLabels(map[string]string{}),+ Values: createValues(map[int64]float64{1: 2}),+ },+ }),+ },+ expectedValues: []float64{5.0},+ },+ {+ name: "mixed case query names",+ expression: "A / b",+ tsData: map[string]*TimeSeriesData{+ "A": createFormulaTestTimeSeriesData("A", []*TimeSeries{+ {+ Labels: createLabels(map[string]string{}),+ Values: createValues(map[int64]float64{1: 10}),+ },+ }),+ "B": createFormulaTestTimeSeriesData("B", []*TimeSeries{+ {+ Labels: createLabels(map[string]string{}),+ Values: createValues(map[int64]float64{1: 2}),+ },+ }),+ },+ expectedValues: []float64{5.0},+ },+ {+ name: "uppercase query names with lowercase data keys",+ expression: "A / B",+ tsData: map[string]*TimeSeriesData{+ "a": createFormulaTestTimeSeriesData("a", []*TimeSeries{+ {+ Labels: createLabels(map[string]string{}),+ Values: createValues(map[int64]float64{1: 10}),+ },+ }),+ "b": createFormulaTestTimeSeriesData("b", []*TimeSeries{+ {+ Labels: createLabels(map[string]string{}),+ Values: createValues(map[int64]float64{1: 2}),+ },+ }),+ },+ expectedValues: []float64{5.0},+ },+ {+ name: "all lowercase",+ expression: "a/b",+ tsData: map[string]*TimeSeriesData{+ "a": createFormulaTestTimeSeriesData("a", []*TimeSeries{+ {+ Labels: createLabels(map[string]string{}),+ Values: createValues(map[int64]float64{1: 100}),+ },+ }),+ "b": createFormulaTestTimeSeriesData("b", []*TimeSeries{+ {+ Labels: createLabels(map[string]string{}),+ Values: createValues(map[int64]float64{1: 10}),+ },+ }),+ },+ expectedValues: []float64{10.0},+ },+ {+ name: "complex expression with mixed case",+ expression: "a + B * c",+ tsData: map[string]*TimeSeriesData{+ "A": createFormulaTestTimeSeriesData("A", []*TimeSeries{+ {+ Labels: createLabels(map[string]string{}),+ Values: createValues(map[int64]float64{1: 5}),+ },+ }),+ "b": createFormulaTestTimeSeriesData("b", []*TimeSeries{+ {+ Labels: createLabels(map[string]string{}),+ Values: createValues(map[int64]float64{1: 3}),+ },+ }),+ "C": createFormulaTestTimeSeriesData("C", []*TimeSeries{+ {+ Labels: createLabels(map[string]string{}),+ Values: createValues(map[int64]float64{1: 2}),+ },+ }),+ },+ expectedValues: []float64{11.0}, // 5 + 3 * 2 = 11+ },+ {+ name: "lowercase variables with default zero missing point",+ expression: "a + b",+ tsData: map[string]*TimeSeriesData{+ "A": createFormulaTestTimeSeriesData("A", []*TimeSeries{+ {+ Labels: createLabels(map[string]string{}),+ Values: createValues(map[int64]float64{+ 1: 10,+ 2: 20,+ }),+ },+ }),+ "B": createFormulaTestTimeSeriesData("B", []*TimeSeries{+ {+ Labels: createLabels(map[string]string{}),+ Values: createValues(map[int64]float64{+ 1: 5,+ }),+ },+ }),+ },+ expectedValues: []float64{15.0, 20.0}, // t1: 10+5, t2: 20+0+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ evaluator, err := NewFormulaEvaluator(tt.expression, map[string]bool{"a": true, "A": true, "b": true, "B": true, "c": true, "C": true})+ require.NoError(t, err)++ result, err := evaluator.EvaluateFormula(tt.tsData)+ require.NoError(t, err)+ require.NotNil(t, result)++ assert.Equal(t, 1, len(result), "should have exactly one result series")+ assert.Equal(t, len(tt.expectedValues), len(result[0].Values), "should match expected number of values")+ for i, v := range tt.expectedValues {+ assert.InDelta(t, v, result[0].Values[i].Value, 0.0001, "value at index %d should match", i)+ }+ })+ }+}+func TestAbsValueExpression(t *testing.T) {tsData := map[string]*TimeSeriesData{"A": createFormulaTestTimeSeriesData("A", []*TimeSeries{