Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check alert rule queries are all disabled if at least one query is set #5966

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions pkg/query-service/rules/api_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,49 @@ func isValidLabelValue(v string) bool {
return utf8.ValidString(v)
}

func isAllQueriesDisabled(compositeQuery *v3.CompositeQuery) bool {
if compositeQuery == nil {
return false
}
if compositeQuery.BuilderQueries == nil && compositeQuery.PromQueries == nil && compositeQuery.ClickHouseQueries == nil {
return false
}
for _, query := range compositeQuery.BuilderQueries {
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
if !query.Disabled {
return false
}
}
for _, query := range compositeQuery.PromQueries {
if !query.Disabled {
return false
}
}
for _, query := range compositeQuery.ClickHouseQueries {
if !query.Disabled {
return false
}
}
return true
}

func (r *PostableRule) Validate() error {

var errs []error

if r.RuleCondition == nil {
errs = append(errs, errors.Errorf("rule condition is required"))
// will get panic if we try to access CompositeQuery, so return here
return multierr.Combine(errs...)
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
} else {
if r.RuleCondition.CompositeQuery == nil {
errs = append(errs, errors.Errorf("composite metric query is required"))
}
}

if isAllQueriesDisabled(r.RuleCondition.CompositeQuery) {
errs = append(errs, errors.Errorf("all queries are disabled in rule condition"))
}

if r.RuleType == RuleTypeThreshold {
if r.RuleCondition.Target == nil {
errs = append(errs, errors.Errorf("rule condition missing the threshold"))
Expand Down
70 changes: 70 additions & 0 deletions pkg/query-service/rules/api_params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package rules

import (
"testing"

v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
)

func TestIsAllQueriesDisabled(t *testing.T) {
// Test case for all queries disabled
compositeQuery := &v3.CompositeQuery{
BuilderQueries: map[string]*v3.BuilderQuery{
"query1": {
Disabled: true,
},
"query2": {
Disabled: true,
},
},
PromQueries: map[string]*v3.PromQuery{
"query3": {
Disabled: true,
},
"query4": {
Disabled: true,
},
},
ClickHouseQueries: map[string]*v3.ClickHouseQuery{
"query5": {
Disabled: true,
},
"query6": {
Disabled: true,
},
},
}

testCases := []*v3.CompositeQuery{
compositeQuery,
nil,
&v3.CompositeQuery{},
&v3.CompositeQuery{
BuilderQueries: map[string]*v3.BuilderQuery{
"query1": {
Disabled: true,
},
"query2": {
Disabled: false,
},
},
},
&v3.CompositeQuery{
PromQueries: map[string]*v3.PromQuery{
"query3": {
Disabled: false,
},
},
},
}

expectedResult := []bool{true, false, false, false, false}

for index, compositeQuery := range testCases {
expected := expectedResult[index]
actual := isAllQueriesDisabled(compositeQuery)
if actual != expected {
t.Errorf("Expected %v, but got %v", expected, actual)
}
}
}
Loading