diff --git a/CHANGELOG.md b/CHANGELOG.md index 8328ea318ad..51154667de2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * [CHANGE] Upgrade Dockerfile Node version from 14x to 18x. #5906 * [CHANGE] Ingester: Remove `-querier.query-store-for-labels-enabled` flag. Querying long-term store for labels is always enabled. #5984 * [FEATURE] Ingester: Experimental: Enable native histogram ingestion via `-blocks-storage.tsdb.enable-native-histograms` flag. #5986 +* [FEATURE] Ruler: Add support for filtering out alerts in ListRules API. #6011 * [ENHANCEMENT] rulers: Add support to persist tokens in rulers. #5987 * [ENHANCEMENT] Query Frontend/Querier: Added store gateway postings touched count and touched size in Querier stats and log in Query Frontend. #5892 * [ENHANCEMENT] Query Frontend/Querier: Returns `warnings` on prometheus query responses. #5916 diff --git a/pkg/ruler/api.go b/pkg/ruler/api.go index f535e737aef..d9b8138d615 100644 --- a/pkg/ruler/api.go +++ b/pkg/ruler/api.go @@ -135,11 +135,18 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) { return } + excludeAlerts, err := parseExcludeAlerts(req) + if err != nil { + util_api.RespondError(logger, w, v1.ErrBadData, "invalid boolean value for exclude_alerts", http.StatusBadRequest) + return + } + rulesRequest := RulesRequest{ RuleNames: req.Form["rule_name[]"], RuleGroupNames: req.Form["rule_group[]"], Files: req.Form["file[]"], Type: typ, + ExcludeAlerts: excludeAlerts, } w.Header().Set("Content-Type", "application/json") @@ -234,6 +241,20 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) { } } +func parseExcludeAlerts(r *http.Request) (bool, error) { + excludeAlertsParam := strings.ToLower(r.URL.Query().Get("exclude_alerts")) + + if excludeAlertsParam == "" { + return false, nil + } + + excludeAlerts, err := strconv.ParseBool(excludeAlertsParam) + if err != nil { + return false, fmt.Errorf("error converting exclude_alerts: %w", err) + } + return excludeAlerts, nil +} + func (a *API) PrometheusAlerts(w http.ResponseWriter, req *http.Request) { logger := util_log.WithContext(req.Context(), a.logger) userID, err := tenant.TenantID(req.Context()) diff --git a/pkg/ruler/ruler.go b/pkg/ruler/ruler.go index 3e72fa16297..272fd603846 100644 --- a/pkg/ruler/ruler.go +++ b/pkg/ruler/ruler.go @@ -927,19 +927,21 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB continue } alerts := []*AlertStateDesc{} - for _, a := range rule.ActiveAlerts() { - alerts = append(alerts, &AlertStateDesc{ - State: a.State.String(), - Labels: cortexpb.FromLabelsToLabelAdapters(a.Labels), - Annotations: cortexpb.FromLabelsToLabelAdapters(a.Annotations), - Value: a.Value, - ActiveAt: a.ActiveAt, - FiredAt: a.FiredAt, - ResolvedAt: a.ResolvedAt, - LastSentAt: a.LastSentAt, - ValidUntil: a.ValidUntil, - KeepFiringSince: a.KeepFiringSince, - }) + if !rulesRequest.ExcludeAlerts { + for _, a := range rule.ActiveAlerts() { + alerts = append(alerts, &AlertStateDesc{ + State: a.State.String(), + Labels: cortexpb.FromLabelsToLabelAdapters(a.Labels), + Annotations: cortexpb.FromLabelsToLabelAdapters(a.Annotations), + Value: a.Value, + ActiveAt: a.ActiveAt, + FiredAt: a.FiredAt, + ResolvedAt: a.ResolvedAt, + LastSentAt: a.LastSentAt, + ValidUntil: a.ValidUntil, + KeepFiringSince: a.KeepFiringSince, + }) + } } ruleDesc = &RuleStateDesc{ Rule: &rulespb.RuleDesc{ @@ -1146,6 +1148,7 @@ func (r *Ruler) getShardedRules(ctx context.Context, userID string, rulesRequest RuleGroupNames: rulesRequest.GetRuleGroupNames(), Files: rulesRequest.GetFiles(), Type: rulesRequest.GetType(), + ExcludeAlerts: rulesRequest.GetExcludeAlerts(), }) if err != nil { diff --git a/pkg/ruler/ruler.pb.go b/pkg/ruler/ruler.pb.go index 412fe757f2f..621da3752f1 100644 --- a/pkg/ruler/ruler.pb.go +++ b/pkg/ruler/ruler.pb.go @@ -43,6 +43,7 @@ type RulesRequest struct { RuleGroupNames []string `protobuf:"bytes,2,rep,name=ruleGroupNames,proto3" json:"ruleGroupNames,omitempty"` Files []string `protobuf:"bytes,3,rep,name=files,proto3" json:"files,omitempty"` Type string `protobuf:"bytes,4,opt,name=type,proto3" json:"type,omitempty"` + ExcludeAlerts bool `protobuf:"varint,5,opt,name=excludeAlerts,proto3" json:"excludeAlerts,omitempty"` } func (m *RulesRequest) Reset() { *m = RulesRequest{} } @@ -105,6 +106,13 @@ func (m *RulesRequest) GetType() string { return "" } +func (m *RulesRequest) GetExcludeAlerts() bool { + if m != nil { + return m.ExcludeAlerts + } + return false +} + type RulesResponse struct { Groups []*GroupStateDesc `protobuf:"bytes,1,rep,name=groups,proto3" json:"groups,omitempty"` } @@ -420,54 +428,56 @@ func init() { func init() { proto.RegisterFile("ruler.proto", fileDescriptor_9ecbec0a4cfddea6) } var fileDescriptor_9ecbec0a4cfddea6 = []byte{ - // 750 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x55, 0x4f, 0x6b, 0x13, 0x4f, - 0x18, 0xde, 0x49, 0x9b, 0x34, 0x99, 0xf4, 0xd7, 0xf2, 0x9b, 0x46, 0x59, 0x83, 0x4c, 0x4a, 0x04, - 0x29, 0x82, 0x1b, 0x88, 0x05, 0xf1, 0x50, 0x25, 0xa5, 0xad, 0x17, 0x91, 0xb2, 0x51, 0xaf, 0x61, - 0x92, 0x4c, 0xb6, 0x6b, 0x37, 0x3b, 0xeb, 0xcc, 0x6c, 0xa8, 0x17, 0xf1, 0x23, 0xf4, 0xe8, 0xd9, - 0x93, 0x1f, 0xa5, 0xc7, 0xe2, 0xa9, 0x88, 0x54, 0x9b, 0x5e, 0x3c, 0xf6, 0x23, 0xc8, 0xcc, 0xec, - 0x9a, 0xa4, 0x56, 0x70, 0x91, 0x5e, 0x92, 0x79, 0xff, 0x3c, 0xcf, 0x3b, 0xef, 0xfb, 0xcc, 0xcc, - 0xc2, 0x32, 0x8f, 0x03, 0xca,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0x1d, 0xe1, 0x87, 0x3d, 0x6a, 0xc3, 0x0c, 0x64, 0xcb, 0x0a, - 0xbe, 0xa3, 0xd1, 0x6d, 0x05, 0x6e, 0x6e, 0xc0, 0xbc, 0x7a, 0x0e, 0x38, 0x5a, 0x37, 0x0b, 0x81, - 0x56, 0xa6, 0x5e, 0xc5, 0xf4, 0x7b, 0x51, 0xad, 0xcc, 0x3a, 0xcd, 0x23, 0x5e, 0xb7, 0x36, 0xd7, - 0x8f, 0xcf, 0xb0, 0x75, 0x72, 0x86, 0xad, 0x8b, 0x33, 0x0c, 0xde, 0x8f, 0x31, 0xf8, 0x34, 0xc6, - 0xe0, 0x68, 0x8c, 0xc1, 0xf1, 0x18, 0x83, 0xef, 0x63, 0x0c, 0x7e, 0x8c, 0xb1, 0x75, 0x31, 0xc6, - 0xe0, 0xf0, 0x1c, 0x5b, 0xc7, 0xe7, 0xd8, 0x3a, 0x39, 0xc7, 0x56, 0xb7, 0xa0, 0xf7, 0xf8, 0xe0, - 0x67, 0x00, 0x00, 0x00, 0xff, 0xff, 0x77, 0xf3, 0x36, 0x0c, 0x7b, 0x07, 0x00, 0x00, + // 769 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x55, 0x4f, 0x6b, 0x13, 0x41, + 0x14, 0xdf, 0x49, 0x9b, 0x34, 0x99, 0xf4, 0x0f, 0x4e, 0xa3, 0xac, 0x41, 0x36, 0x21, 0x8a, 0x04, + 0xc1, 0x0d, 0xc4, 0x82, 0x78, 0xa8, 0x92, 0xd2, 0xd6, 0x8b, 0x48, 0xd9, 0xa8, 0xd7, 0x30, 0x49, + 0x26, 0xdb, 0xb5, 0x9b, 0x9d, 0x75, 0x66, 0x36, 0xd4, 0x9b, 0x1f, 0xa1, 0x47, 0xcf, 0xe2, 0xc1, + 0x8f, 0xd2, 0x63, 0xf1, 0x54, 0x44, 0xaa, 0xdd, 0x5e, 0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 0xdd, 0xe1, 0x24, + 0x10, 0x32, 0x4e, 0x7e, 0x96, 0x38, 0x12, 0xd9, 0x26, 0x81, 0xd0, 0xe5, 0x8c, 0xb0, 0xef, 0xf5, + 0x3b, 0x51, 0x20, 0x3c, 0xdf, 0x2c, 0xcc, 0x12, 0x46, 0x01, 0x5f, 0x4b, 0x1c, 0xda, 0x81, 0x37, + 0xf6, 0x08, 0x09, 0x3b, 0x03, 0x8f, 0x79, 0x81, 0xdb, 0xe1, 0x5e, 0xd0, 0x23, 0x26, 0x9c, 0x21, + 0xd8, 0x8a, 0x84, 0x6f, 0x2b, 0x74, 0x5b, 0x82, 0x9b, 0xeb, 0x30, 0x2b, 0xcf, 0x01, 0x43, 0x6b, + 0xfa, 0xc1, 0xd1, 0xea, 0xc4, 0x55, 0x4c, 0x7f, 0x55, 0xca, 0xa5, 0x69, 0xa5, 0x3e, 0xe2, 0x35, + 0x63, 0x63, 0xed, 0xe8, 0xd4, 0x32, 0x8e, 0x4f, 0x2d, 0xe3, 0xfc, 0xd4, 0x02, 0x1f, 0x62, 0x0b, + 0x7c, 0x89, 0x2d, 0x70, 0x18, 0x5b, 0xe0, 0x28, 0xb6, 0xc0, 0xcf, 0xd8, 0x02, 0xbf, 0x62, 0xcb, + 0x38, 0x8f, 0x2d, 0x70, 0x70, 0x66, 0x19, 0x47, 0x67, 0x96, 0x71, 0x7c, 0x66, 0x19, 0xdd, 0x9c, + 0xaa, 0xf1, 0xd1, 0xef, 0x00, 0x00, 0x00, 0xff, 0xff, 0xcc, 0x89, 0xc3, 0x51, 0xa2, 0x07, 0x00, + 0x00, } func (this *RulesRequest) Equal(that interface{}) bool { @@ -516,6 +526,9 @@ func (this *RulesRequest) Equal(that interface{}) bool { if this.Type != that1.Type { return false } + if this.ExcludeAlerts != that1.ExcludeAlerts { + return false + } return true } func (this *RulesResponse) Equal(that interface{}) bool { @@ -697,12 +710,13 @@ func (this *RulesRequest) GoString() string { if this == nil { return "nil" } - s := make([]string, 0, 8) + s := make([]string, 0, 9) s = append(s, "&ruler.RulesRequest{") s = append(s, "RuleNames: "+fmt.Sprintf("%#v", this.RuleNames)+",\n") s = append(s, "RuleGroupNames: "+fmt.Sprintf("%#v", this.RuleGroupNames)+",\n") s = append(s, "Files: "+fmt.Sprintf("%#v", this.Files)+",\n") s = append(s, "Type: "+fmt.Sprintf("%#v", this.Type)+",\n") + s = append(s, "ExcludeAlerts: "+fmt.Sprintf("%#v", this.ExcludeAlerts)+",\n") s = append(s, "}") return strings.Join(s, "") } @@ -883,6 +897,16 @@ func (m *RulesRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.ExcludeAlerts { + i-- + if m.ExcludeAlerts { + dAtA[i] = 1 + } else { + dAtA[i] = 0 + } + i-- + dAtA[i] = 0x28 + } if len(m.Type) > 0 { i -= len(m.Type) copy(dAtA[i:], m.Type) @@ -1259,6 +1283,9 @@ func (m *RulesRequest) Size() (n int) { if l > 0 { n += 1 + l + sovRuler(uint64(l)) } + if m.ExcludeAlerts { + n += 2 + } return n } @@ -1390,6 +1417,7 @@ func (this *RulesRequest) String() string { `RuleGroupNames:` + fmt.Sprintf("%v", this.RuleGroupNames) + `,`, `Files:` + fmt.Sprintf("%v", this.Files) + `,`, `Type:` + fmt.Sprintf("%v", this.Type) + `,`, + `ExcludeAlerts:` + fmt.Sprintf("%v", this.ExcludeAlerts) + `,`, `}`, }, "") return s @@ -1632,6 +1660,26 @@ func (m *RulesRequest) Unmarshal(dAtA []byte) error { } m.Type = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex + case 5: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field ExcludeAlerts", wireType) + } + var v int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowRuler + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + m.ExcludeAlerts = bool(v != 0) default: iNdEx = preIndex skippy, err := skipRuler(dAtA[iNdEx:]) diff --git a/pkg/ruler/ruler.proto b/pkg/ruler/ruler.proto index da653b8651c..ea432c4b08f 100644 --- a/pkg/ruler/ruler.proto +++ b/pkg/ruler/ruler.proto @@ -24,6 +24,7 @@ message RulesRequest { repeated string ruleGroupNames = 2; repeated string files = 3; string type = 4; + bool excludeAlerts = 5; } message RulesResponse { diff --git a/pkg/ruler/ruler_test.go b/pkg/ruler/ruler_test.go index f04d02d6d40..6af9debef8a 100644 --- a/pkg/ruler/ruler_test.go +++ b/pkg/ruler/ruler_test.go @@ -351,6 +351,15 @@ func TestGetRules(t *testing.T) { expectedError error replicationFactor int enableZoneAwareReplication bool + alertsCountChecker func(t *testing.T, count int) + } + + noAlertsExpected := func(t *testing.T, count int) { + require.Equal(t, 0, count) + } + + alertsExpected := func(t *testing.T, count int) { + require.GreaterOrEqual(t, count, 1) } ruleMap := rulesMap{ @@ -361,7 +370,7 @@ func TestGetRules(t *testing.T) { }, { Alert: "atest_user1_1", - Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Expr: "1 < bool 2", }, }, "ruler1-user1-rule-group2": []*rulespb.RuleDesc{ @@ -383,7 +392,7 @@ func TestGetRules(t *testing.T) { }, { Alert: "atest_user1_1", - Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Expr: "1 < bool 2", }, }, "ruler2-user2-rule-group1": []*rulespb.RuleDesc{ @@ -393,7 +402,7 @@ func TestGetRules(t *testing.T) { }, { Alert: "atest_user1_1", - Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Expr: "1 < bool 2", }, }, "ruler2-user2-rule-group2": []*rulespb.RuleDesc{ @@ -403,13 +412,13 @@ func TestGetRules(t *testing.T) { }, { Alert: "atest_user2_1", - Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Expr: "1 < bool 2", }, }, "ruler2-user3-rule-group1": []*rulespb.RuleDesc{ { Alert: "atest_user3_1", - Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Expr: "1 < bool 2", }, }, "ruler3-user2-rule-group1": []*rulespb.RuleDesc{ @@ -419,7 +428,7 @@ func TestGetRules(t *testing.T) { }, { Alert: "atest_user1_1", - Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Expr: "1 < bool 2", }, }, "ruler3-user2-rule-group2": []*rulespb.RuleDesc{ @@ -429,7 +438,7 @@ func TestGetRules(t *testing.T) { }, { Alert: "atest_user1_1", - Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Expr: "1 < bool 2", }, }, "ruler3-user3-rule-group1": []*rulespb.RuleDesc{ @@ -439,7 +448,7 @@ func TestGetRules(t *testing.T) { }, { Alert: "atest_user1_1", - Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Expr: "1 < bool 2", }, }, } @@ -482,32 +491,32 @@ func TestGetRules(t *testing.T) { expectedRules := expectedRulesMap{ "ruler1": map[string]rulespb.RuleGroupList{ "user1": { - &rulespb.RuleGroupDesc{User: "user1", Namespace: "namespace", Name: "first", Interval: 10 * time.Second, Rules: ruleMap["ruler1-user1-rule-group1"]}, - &rulespb.RuleGroupDesc{User: "user1", Namespace: "namespace", Name: "second", Interval: 10 * time.Second, Rules: ruleMap["ruler1-user1-rule-group2"]}, + &rulespb.RuleGroupDesc{User: "user1", Namespace: "namespace", Name: "first", Interval: 10 * time.Millisecond, Rules: ruleMap["ruler1-user1-rule-group1"]}, + &rulespb.RuleGroupDesc{User: "user1", Namespace: "namespace", Name: "second", Interval: 10 * time.Millisecond, Rules: ruleMap["ruler1-user1-rule-group2"]}, }, "user2": { - &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "third", Interval: 10 * time.Second, Rules: ruleMap["ruler1-user2-rule-group1"]}, + &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "third", Interval: 10 * time.Millisecond, Rules: ruleMap["ruler1-user2-rule-group1"]}, }, }, "ruler2": map[string]rulespb.RuleGroupList{ "user1": { - &rulespb.RuleGroupDesc{User: "user1", Namespace: "namespace", Name: "third", Interval: 10 * time.Second, Rules: ruleMap["ruler2-user1-rule-group3"]}, + &rulespb.RuleGroupDesc{User: "user1", Namespace: "namespace", Name: "third", Interval: 10 * time.Millisecond, Rules: ruleMap["ruler2-user1-rule-group3"]}, }, "user2": { - &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "first", Interval: 10 * time.Second, Rules: ruleMap["ruler2-user2-rule-group1"]}, - &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "second", Interval: 10 * time.Second, Rules: ruleMap["ruler2-user2-rule-group2"]}, + &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "first", Interval: 10 * time.Millisecond, Rules: ruleMap["ruler2-user2-rule-group1"]}, + &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "second", Interval: 10 * time.Millisecond, Rules: ruleMap["ruler2-user2-rule-group2"]}, }, "user3": { - &rulespb.RuleGroupDesc{User: "user3", Namespace: "latency-test", Name: "first", Interval: 10 * time.Second, Rules: ruleMap["ruler2-user3-rule-group1"]}, + &rulespb.RuleGroupDesc{User: "user3", Namespace: "latency-test", Name: "first", Interval: 10 * time.Millisecond, Rules: ruleMap["ruler2-user3-rule-group1"]}, }, }, "ruler3": map[string]rulespb.RuleGroupList{ "user3": { - &rulespb.RuleGroupDesc{User: "user3", Namespace: "namespace", Name: "third", Interval: 10 * time.Second, Rules: ruleMap["ruler3-user3-rule-group1"]}, + &rulespb.RuleGroupDesc{User: "user3", Namespace: "namespace", Name: "third", Interval: 10 * time.Millisecond, Rules: ruleMap["ruler3-user3-rule-group1"]}, }, "user2": { - &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "forth", Interval: 10 * time.Second, Rules: ruleMap["ruler3-user2-rule-group1"]}, - &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "fifty", Interval: 10 * time.Second, Rules: ruleMap["ruler3-user2-rule-group2"]}, + &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "forth", Interval: 10 * time.Millisecond, Rules: ruleMap["ruler3-user2-rule-group1"]}, + &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "fifty", Interval: 10 * time.Millisecond, Rules: ruleMap["ruler3-user2-rule-group2"]}, }, }, } @@ -525,6 +534,21 @@ func TestGetRules(t *testing.T) { "user3": 2, }, expectedClientCallCount: len(expectedRules), + alertsCountChecker: alertsExpected, + }, + "No Sharding with Exclude Alerts Filter": { + sharding: false, + rulesRequest: RulesRequest{ + ExcludeAlerts: true, + }, + rulerStateMap: rulerStateMapAllActive, + expectedCount: map[string]int{ + "user1": 5, + "user2": 9, + "user3": 3, + }, + expectedClientCallCount: len(expectedRules), + alertsCountChecker: noAlertsExpected, }, "Default Sharding with No Filter": { sharding: true, @@ -537,6 +561,19 @@ func TestGetRules(t *testing.T) { }, expectedClientCallCount: len(expectedRules), }, + "Default Sharding with Exclude Alerts Filter": { + sharding: true, + shardingStrategy: util.ShardingStrategyDefault, + rulesRequest: RulesRequest{ExcludeAlerts: true}, + rulerStateMap: rulerStateMapAllActive, + expectedCount: map[string]int{ + "user1": 5, + "user2": 9, + "user3": 3, + }, + expectedClientCallCount: len(expectedRules), + alertsCountChecker: noAlertsExpected, + }, "Default Sharding with No Filter but with API Rules backup enabled": { sharding: true, shardingStrategy: util.ShardingStrategyDefault, @@ -564,6 +601,20 @@ func TestGetRules(t *testing.T) { }, expectedClientCallCount: 2, }, + "Shuffle Sharding and ShardSize = 2 with Exclude Alerts Filter": { + sharding: true, + shuffleShardSize: 2, + shardingStrategy: util.ShardingStrategyShuffle, + rulerStateMap: rulerStateMapAllActive, + rulesRequest: RulesRequest{ExcludeAlerts: true}, + expectedCount: map[string]int{ + "user1": 5, + "user2": 9, + "user3": 3, + }, + expectedClientCallCount: 2, + alertsCountChecker: noAlertsExpected, + }, "Shuffle Sharding and ShardSize = 2 and Rule Group Name Filter": { sharding: true, shuffleShardSize: 2, @@ -844,6 +895,10 @@ func TestGetRules(t *testing.T) { time.Sleep(100 * time.Millisecond) } + // Just a bit of time to give rulers to evaluate the rules. Exact rule evaluation iteration count + // does not matter but requires at least 2 + time.Sleep(50 * time.Millisecond) + for u := range allRulesByUser { ctx := user.InjectOrgID(context.Background(), u) forEachRuler(func(_ string, r *Ruler) { @@ -855,10 +910,18 @@ func TestGetRules(t *testing.T) { require.NoError(t, err) } rct := 0 + alertsCount := 0 for _, ruleStateDesc := range ruleStateDescriptions { rct += len(ruleStateDesc.ActiveRules) + for _, r := range ruleStateDesc.GetActiveRules() { + alertsCount += len(r.Alerts) + } } require.Equal(t, tc.expectedCount[u], rct) + if tc.alertsCountChecker != nil { + tc.alertsCountChecker(t, alertsCount) + } + if tc.sharding { mockPoolClient := r.clientsPool.(*mockRulerClientsPool)