mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-08-17 12:35:57 +08:00
fix: check alert rule queries are all disabled if at least one query is set (#5966)
This commit is contained in:
parent
b49ed913c7
commit
0feab5aa93
@ -170,18 +170,62 @@ func isValidLabelValue(v string) bool {
|
|||||||
return utf8.ValidString(v)
|
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
|
||||||
|
}
|
||||||
|
switch compositeQuery.QueryType {
|
||||||
|
case v3.QueryTypeBuilder:
|
||||||
|
if len(compositeQuery.BuilderQueries) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, query := range compositeQuery.BuilderQueries {
|
||||||
|
if !query.Disabled {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case v3.QueryTypePromQL:
|
||||||
|
if len(compositeQuery.PromQueries) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, query := range compositeQuery.PromQueries {
|
||||||
|
if !query.Disabled {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case v3.QueryTypeClickHouseSQL:
|
||||||
|
if len(compositeQuery.ClickHouseQueries) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, query := range compositeQuery.ClickHouseQueries {
|
||||||
|
if !query.Disabled {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (r *PostableRule) Validate() error {
|
func (r *PostableRule) Validate() error {
|
||||||
|
|
||||||
var errs []error
|
var errs []error
|
||||||
|
|
||||||
if r.RuleCondition == nil {
|
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 errors.Errorf("rule condition is required")
|
||||||
} else {
|
} else {
|
||||||
if r.RuleCondition.CompositeQuery == nil {
|
if r.RuleCondition.CompositeQuery == nil {
|
||||||
errs = append(errs, errors.Errorf("composite metric query is required"))
|
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.RuleType == RuleTypeThreshold {
|
||||||
if r.RuleCondition.Target == nil {
|
if r.RuleCondition.Target == nil {
|
||||||
errs = append(errs, errors.Errorf("rule condition missing the threshold"))
|
errs = append(errs, errors.Errorf("rule condition missing the threshold"))
|
||||||
|
86
pkg/query-service/rules/api_params_test.go
Normal file
86
pkg/query-service/rules/api_params_test.go
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
package rules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIsAllQueriesDisabled(t *testing.T) {
|
||||||
|
testCases := []*v3.CompositeQuery{
|
||||||
|
&v3.CompositeQuery{
|
||||||
|
BuilderQueries: map[string]*v3.BuilderQuery{
|
||||||
|
"query1": {
|
||||||
|
Disabled: true,
|
||||||
|
},
|
||||||
|
"query2": {
|
||||||
|
Disabled: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
QueryType: v3.QueryTypeBuilder,
|
||||||
|
},
|
||||||
|
nil,
|
||||||
|
&v3.CompositeQuery{
|
||||||
|
QueryType: v3.QueryTypeBuilder,
|
||||||
|
},
|
||||||
|
&v3.CompositeQuery{
|
||||||
|
QueryType: v3.QueryTypeBuilder,
|
||||||
|
BuilderQueries: map[string]*v3.BuilderQuery{
|
||||||
|
"query1": {
|
||||||
|
Disabled: true,
|
||||||
|
},
|
||||||
|
"query2": {
|
||||||
|
Disabled: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&v3.CompositeQuery{
|
||||||
|
QueryType: v3.QueryTypePromQL,
|
||||||
|
},
|
||||||
|
&v3.CompositeQuery{
|
||||||
|
QueryType: v3.QueryTypePromQL,
|
||||||
|
PromQueries: map[string]*v3.PromQuery{
|
||||||
|
"query3": {
|
||||||
|
Disabled: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&v3.CompositeQuery{
|
||||||
|
QueryType: v3.QueryTypePromQL,
|
||||||
|
PromQueries: map[string]*v3.PromQuery{
|
||||||
|
"query3": {
|
||||||
|
Disabled: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&v3.CompositeQuery{
|
||||||
|
QueryType: v3.QueryTypeClickHouseSQL,
|
||||||
|
},
|
||||||
|
&v3.CompositeQuery{
|
||||||
|
QueryType: v3.QueryTypeClickHouseSQL,
|
||||||
|
ClickHouseQueries: map[string]*v3.ClickHouseQuery{
|
||||||
|
"query4": {
|
||||||
|
Disabled: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&v3.CompositeQuery{
|
||||||
|
QueryType: v3.QueryTypeClickHouseSQL,
|
||||||
|
ClickHouseQueries: map[string]*v3.ClickHouseQuery{
|
||||||
|
"query4": {
|
||||||
|
Disabled: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedResult := []bool{true, false, false, false, false, false, true, false, false, true}
|
||||||
|
|
||||||
|
for index, compositeQuery := range testCases {
|
||||||
|
expected := expectedResult[index]
|
||||||
|
actual := isAllQueriesDisabled(compositeQuery)
|
||||||
|
if actual != expected {
|
||||||
|
t.Errorf("Expected %v, but got %v", expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user