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
fix: apply having and limit clause on formulas (#5201)
Srikanth Chekuricacf4b99c2cb
diff --git a/pkg/query-service/app/http_handler.go b/pkg/query-service/app/http_handler.goindex f5671f4d0e..1f0769bb08 100644--- a/pkg/query-service/app/http_handler.go+++ b/pkg/query-service/app/http_handler.go@@ -3056,6 +3056,7 @@ func (aH *APIHandler) queryRangeV3(ctx context.Context, queryRangeParams *v3.Quereturn}+ postprocess.ApplyHavingClause(result, queryRangeParams)postprocess.ApplyMetricLimit(result, queryRangeParams)sendQueryResultEvents(r, result, queryRangeParams)@@ -3065,6 +3066,10 @@ func (aH *APIHandler) queryRangeV3(ctx context.Context, queryRangeParams *v3.Quepostprocess.ApplyFunctions(result, queryRangeParams)}+ if queryRangeParams.CompositeQuery.FillGaps {+ postprocess.FillGaps(result, queryRangeParams)+ }+resp := v3.QueryRangeResponse{Result: result,}diff --git a/pkg/query-service/postprocess/having.go b/pkg/query-service/postprocess/having.goindex a37ba70379..5c2be7ce8b 100644--- a/pkg/query-service/postprocess/having.go+++ b/pkg/query-service/postprocess/having.go@@ -6,14 +6,17 @@ import (v3 "go.signoz.io/signoz/pkg/query-service/model/v3")-// applyHavingClause applies the having clause to the result+// ApplyHavingClause applies the having clause to the result// each query has its own having clause// there can be multiple having clauses for each query-func applyHavingClause(result []*v3.Result, queryRangeParams *v3.QueryRangeParamsV3) {+func ApplyHavingClause(result []*v3.Result, queryRangeParams *v3.QueryRangeParamsV3) {for _, result := range result {builderQueries := queryRangeParams.CompositeQuery.BuilderQueries- if builderQueries != nil && (builderQueries[result.QueryName].DataSource == v3.DataSourceMetrics) {+ // apply having clause for metrics and formula+ if builderQueries != nil &&+ (builderQueries[result.QueryName].DataSource == v3.DataSourceMetrics ||+ builderQueries[result.QueryName].QueryName != builderQueries[result.QueryName].Expression) {havingClause := builderQueries[result.QueryName].Havingfor i := 0; i < len(result.Series); i++ {@@ -37,46 +40,53 @@ func evaluateHavingClause(having []v3.Having, value float64) bool {return true}+ satisfied := true+for _, h := range having {switch h.Operator {case v3.HavingOperatorEqual:- if value == h.Value.(float64) {- return true+ if value != h.Value.(float64) {+ satisfied = false}case v3.HavingOperatorNotEqual:- if value != h.Value.(float64) {- return true+ if value == h.Value.(float64) {+ satisfied = false}case v3.HavingOperatorGreaterThan:- if value > h.Value.(float64) {- return true+ if value <= h.Value.(float64) {+ satisfied = false}case v3.HavingOperatorGreaterThanOrEq:- if value >= h.Value.(float64) {- return true+ if value < h.Value.(float64) {+ satisfied = false}case v3.HavingOperatorLessThan:- if value < h.Value.(float64) {- return true+ if value >= h.Value.(float64) {+ satisfied = false}case v3.HavingOperatorLessThanOrEq:- if value <= h.Value.(float64) {- return true+ if value > h.Value.(float64) {+ satisfied = false}case v3.HavingOperatorIn, v3.HavingOperator(strings.ToLower(string(v3.HavingOperatorIn))):values, ok := h.Value.([]interface{})if !ok {- return false+ satisfied = false}+ found := falsefor _, v := range values {if value == v.(float64) {- return true+ found = true+ break}}+ if !found {+ satisfied = false+ }case v3.HavingOperatorNotIn, v3.HavingOperator(strings.ToLower(string(v3.HavingOperatorNotIn))):values, ok := h.Value.([]interface{})if !ok {- return true+ satisfied = false}found := falsefor _, v := range values {@@ -85,10 +95,10 @@ func evaluateHavingClause(having []v3.Having, value float64) bool {break}}- if !found {- return true+ if found {+ satisfied = false}}}- return false+ return satisfied}diff --git a/pkg/query-service/postprocess/having_test.go b/pkg/query-service/postprocess/having_test.goindex e7e37095fb..90986d14a9 100644--- a/pkg/query-service/postprocess/having_test.go+++ b/pkg/query-service/postprocess/having_test.go@@ -248,22 +248,86 @@ func TestApplyHavingCaluse(t *testing.T) {},},},+ {+ name: "test multiple having clause",+ results: []*v3.Result{+ {+ QueryName: "A",+ Series: []*v3.Series{+ {+ Points: []v3.Point{+ {+ Value: 0.5,+ },+ {+ Value: 0.4,+ },+ {+ Value: 0.3,+ },+ {+ Value: 0.2,+ },+ {+ Value: 0.1,+ },+ },+ },+ },+ },+ },+ params: &v3.QueryRangeParamsV3{+ CompositeQuery: &v3.CompositeQuery{+ BuilderQueries: map[string]*v3.BuilderQuery{+ "A": {+ DataSource: v3.DataSourceMetrics,+ Having: []v3.Having{+ {+ Operator: v3.HavingOperatorGreaterThanOrEq,+ Value: 0.3,+ },+ {+ Operator: v3.HavingOperatorLessThanOrEq,+ Value: 0.4,+ },+ },+ },+ },+ },+ },+ want: []*v3.Result{+ {+ Series: []*v3.Series{+ {+ Points: []v3.Point{+ {+ Value: 0.4,+ },+ {+ Value: 0.3,+ },+ },+ },+ },+ },+ },+ },}for _, tc := range testCases {t.Run(tc.name, func(t *testing.T) {- applyHavingClause(tc.results, tc.params)+ ApplyHavingClause(tc.results, tc.params)got := tc.resultsif len(got) != len(tc.want) {- t.Errorf("got %v, want %v", got, tc.want)+ t.Errorf("got %v, want %v", len(got), len(tc.want))}for i := range got {if len(got[i].Series) != len(tc.want[i].Series) {- t.Errorf("got %v, want %v", got, tc.want)+ t.Errorf("got %v, want %v", len(got[i].Series), len(tc.want[i].Series))}for j := range got[i].Series {@@ -273,7 +337,7 @@ func TestApplyHavingCaluse(t *testing.T) {for k := range got[i].Series[j].Points {if got[i].Series[j].Points[k].Value != tc.want[i].Series[j].Points[k].Value {- t.Errorf("got %v, want %v", got, tc.want)+ t.Errorf("got %v, want %v", got[i].Series[j].Points[k].Value, tc.want[i].Series[j].Points[k].Value)}}}diff --git a/pkg/query-service/postprocess/limit.go b/pkg/query-service/postprocess/limit.goindex 491c91da54..49cd5d04b9 100644--- a/pkg/query-service/postprocess/limit.go+++ b/pkg/query-service/postprocess/limit.go@@ -17,7 +17,10 @@ func ApplyMetricLimit(results []*v3.Result, queryRangeParams *v3.QueryRangeParamfor _, result := range results {builderQueries := queryRangeParams.CompositeQuery.BuilderQueries- if builderQueries != nil && (builderQueries[result.QueryName].DataSource == v3.DataSourceMetrics) {+ // apply limit for metrics and formula+ if builderQueries != nil &&+ (builderQueries[result.QueryName].DataSource == v3.DataSourceMetrics ||+ builderQueries[result.QueryName].QueryName != builderQueries[result.QueryName].Expression) {limit := builderQueries[result.QueryName].LimitorderByList := builderQueries[result.QueryName].OrderBydiff --git a/pkg/query-service/postprocess/query_range.go b/pkg/query-service/postprocess/process.gosimilarity index 96%rename from pkg/query-service/postprocess/query_range.gorename to pkg/query-service/postprocess/process.goindex 890c37148e..fc35b404de 100644--- a/pkg/query-service/postprocess/query_range.go+++ b/pkg/query-service/postprocess/process.go@@ -18,7 +18,7 @@ func PostProcessResult(result []*v3.Result, queryRangeParams *v3.QueryRangeParam// It's not included in the query because it doesn't work nicely with caching// With this change, if you have a query with a having clause, and then you change the having clause// to something else, the query will still be cached.- applyHavingClause(result, queryRangeParams)+ ApplyHavingClause(result, queryRangeParams)// We apply the metric limit here because it's not part of the clickhouse query// The limit in the context of the time series query is the number of time series// So for the limit to work, we need to know what series to keep and what to discard@@ -64,6 +64,8 @@ func PostProcessResult(result []*v3.Result, queryRangeParams *v3.QueryRangeParamreturn nil, err}formulaResult.QueryName = query.QueryName+ ApplyHavingClause([]*v3.Result{formulaResult}, queryRangeParams)+ ApplyMetricLimit([]*v3.Result{formulaResult}, queryRangeParams)result = append(result, formulaResult)}}diff --git a/pkg/query-service/postprocess/process_test.go b/pkg/query-service/postprocess/process_test.gonew file mode 100644index 0000000000..5f599f7be0--- /dev/null+++ b/pkg/query-service/postprocess/process_test.go@@ -0,0 +1,222 @@+package postprocess++import (+ "reflect"+ "testing"++ v3 "go.signoz.io/signoz/pkg/query-service/model/v3"+)++func TestPostProcessResult(t *testing.T) {+ testCases := []struct {+ name string+ results []*v3.Result+ queryRangeParams *v3.QueryRangeParamsV3+ want []*v3.Result+ }{+ {+ name: "test1",+ results: []*v3.Result{+ {+ QueryName: "A",+ Series: []*v3.Series{+ {+ Labels: map[string]string{+ "service_name": "frontend",+ },+ Points: []v3.Point{+ {+ Timestamp: 1,+ Value: 10,+ },+ {+ Timestamp: 2,+ Value: 20,+ },+ },+ },+ {+ Labels: map[string]string{+ "service_name": "redis",+ },+ Points: []v3.Point{+ {+ Timestamp: 1,+ Value: 12,+ },+ {+ Timestamp: 2,+ Value: 45,+ },+ },+ },+ {+ Labels: map[string]string{+ "service_name": "route",+ },+ Points: []v3.Point{+ {+ Timestamp: 1,+ Value: 2,+ },+ {+ Timestamp: 2,+ Value: 45,+ },+ },+ },+ },+ },+ {+ QueryName: "B",+ Series: []*v3.Series{+ {+ Labels: map[string]string{+ "service_name": "redis",+ },+ Points: []v3.Point{+ {+ Timestamp: 1,+ Value: 6,+ },+ {+ Timestamp: 2,+ Value: 9,+ },+ },+ },+ },+ },+ },+ want: []*v3.Result{+ {+ QueryName: "A",+ Series: []*v3.Series{+ {+ Labels: map[string]string{+ "service_name": "redis",+ },+ Points: []v3.Point{+ {+ Timestamp: 1,+ Value: 12,+ },+ {+ Timestamp: 2,+ Value: 45,+ },+ },+ },+ {+ Labels: map[string]string{+ "service_name": "route",+ },+ Points: []v3.Point{+ {+ Timestamp: 1,+ Value: 2,+ },+ {+ Timestamp: 2,+ Value: 45,+ },+ },+ },+ {+ Labels: map[string]string{+ "service_name": "frontend",+ },+ Points: []v3.Point{+ {+ Timestamp: 1,+ Value: 10,+ },+ {+ Timestamp: 2,+ Value: 20,+ },+ },+ },+ },+ },+ {+ QueryName: "B",+ Series: []*v3.Series{+ {+ Labels: map[string]string{+ "service_name": "redis",+ },+ Points: []v3.Point{+ {+ Timestamp: 1,+ Value: 6,+ },+ {+ Timestamp: 2,+ Value: 9,+ },+ },+ },+ },+ },+ {+ QueryName: "F1",+ Series: []*v3.Series{+ {+ Labels: map[string]string{+ "service_name": "redis",+ },+ LabelsArray: []map[string]string{+ {+ "service_name": "redis",+ },+ },+ Points: []v3.Point{+ {+ Timestamp: 1,+ Value: 0.5,+ },+ {+ Timestamp: 2,+ Value: 0.2,+ },+ },+ },+ },+ },+ },+ queryRangeParams: &v3.QueryRangeParamsV3{+ CompositeQuery: &v3.CompositeQuery{+ BuilderQueries: map[string]*v3.BuilderQuery{+ "A": {+ DataSource: v3.DataSourceMetrics,+ QueryName: "A",+ Expression: "A",+ },+ "B": {+ DataSource: v3.DataSourceMetrics,+ QueryName: "B",+ Expression: "B",+ },+ "F1": {+ Expression: "B/A",+ QueryName: "F1",+ Limit: 1,+ },+ },+ },+ },+ },+ }+ for _, tt := range testCases {+ t.Run(tt.name, func(t *testing.T) {+ got, err := PostProcessResult(tt.results, tt.queryRangeParams)+ if err != nil {+ t.Errorf("PostProcessResult() error = %v", err)+ }+ if !reflect.DeepEqual(got, tt.want) {+ t.Errorf("PostProcessResult() = %v, want %v", got, tt.want)+ }+ })+ }+}