Skip to content

Commit

Permalink
fix: do not mutate the rule condition (#5141)
Browse files Browse the repository at this point in the history
* fix: do not mutate the rule condition

* chore: add unit

* chore: add test
  • Loading branch information
srikanthccv authored Jun 5, 2024
1 parent 694f256 commit e5f96ac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
26 changes: 19 additions & 7 deletions pkg/query-service/rules/thresholdRule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions pkg/query-service/rules/thresholdRule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit e5f96ac

Please sign in to comment.