From e5f96ac89647ca5f9efbbad1c6ff21fd03bc1fc4 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Wed, 5 Jun 2024 19:35:48 +0530 Subject: [PATCH] fix: do not mutate the rule condition (#5141) * fix: do not mutate the rule condition * chore: add unit * chore: add test --- pkg/query-service/rules/thresholdRule.go | 26 ++++++++++++++----- pkg/query-service/rules/thresholdRule_test.go | 4 +++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/pkg/query-service/rules/thresholdRule.go b/pkg/query-service/rules/thresholdRule.go index c8aad7803c..0b8d080bd9 100644 --- a/pkg/query-service/rules/thresholdRule.go +++ b/pkg/query-service/rules/thresholdRule.go @@ -434,12 +434,19 @@ func (r *ThresholdRule) prepareQueryRange(ts time.Time) *v3.QueryRangeParamsV3 { if r.ruleCondition.QueryType() == v3.QueryTypeClickHouseSQL { params := &v3.QueryRangeParamsV3{ - Start: start, - End: end, - Step: int64(math.Max(float64(common.MinAllowedStepInterval(start, end)), 60)), - CompositeQuery: r.ruleCondition.CompositeQuery, - Variables: make(map[string]interface{}, 0), - NoCache: true, + Start: start, + End: end, + Step: int64(math.Max(float64(common.MinAllowedStepInterval(start, end)), 60)), + CompositeQuery: &v3.CompositeQuery{ + QueryType: r.ruleCondition.CompositeQuery.QueryType, + PanelType: r.ruleCondition.CompositeQuery.PanelType, + BuilderQueries: make(map[string]*v3.BuilderQuery), + ClickHouseQueries: make(map[string]*v3.ClickHouseQuery), + PromQueries: make(map[string]*v3.PromQuery), + Unit: r.ruleCondition.CompositeQuery.Unit, + }, + Variables: make(map[string]interface{}, 0), + NoCache: true, } querytemplate.AssignReservedVarsV3(params) for name, chQuery := range r.ruleCondition.CompositeQuery.ClickHouseQueries { @@ -460,8 +467,13 @@ func (r *ThresholdRule) prepareQueryRange(ts time.Time) *v3.QueryRangeParamsV3 { r.SetHealth(HealthBad) return params } - r.ruleCondition.CompositeQuery.ClickHouseQueries[name].Query = query.String() + params.CompositeQuery.ClickHouseQueries[name] = &v3.ClickHouseQuery{ + Query: query.String(), + Disabled: chQuery.Disabled, + Legend: chQuery.Legend, + } } + return params } if r.ruleCondition.CompositeQuery != nil && r.ruleCondition.CompositeQuery.BuilderQueries != nil { diff --git a/pkg/query-service/rules/thresholdRule_test.go b/pkg/query-service/rules/thresholdRule_test.go index 3f1af04a11..762666cbf3 100644 --- a/pkg/query-service/rules/thresholdRule_test.go +++ b/pkg/query-service/rules/thresholdRule_test.go @@ -876,5 +876,9 @@ func TestThresholdRuleClickHouseTmpl(t *testing.T) { params := rule.prepareQueryRange(ts) assert.Equal(t, c.expectedQuery, params.CompositeQuery.ClickHouseQueries["A"].Query, "Test case %d", idx) + + secondTimeParams := rule.prepareQueryRange(ts) + + assert.Equal(t, c.expectedQuery, secondTimeParams.CompositeQuery.ClickHouseQueries["A"].Query, "Test case %d", idx) } }