diff --git a/CHANGELOG.md b/CHANGELOG.md index da1dd4cc3b..ef662a6269 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,11 +22,12 @@ * [ENHANCEMENT] Upgrade go to 1.22.5 #6014 #6072 * [ENHANCEMENT] Ingester: Add a new experimental `-ingester.labels-string-interning-enabled` flag to enable string interning for metrics labels. #6057 * [ENHANCEMENT] Ingester: Add link to renew 10% of the ingesters tokens in the admin page. #6063 +* [ENHANCEMENT] Ruler: Add support for filtering by `state` and `health` field on Rules API. #6040 +* [ENHANCEMENT] Ruler: Add support for filtering by `match` field on Rules API. #6083 * [BUGFIX] Configsdb: Fix endline issue in db password. #5920 * [BUGFIX] Ingester: Fix `user` and `type` labels for the `cortex_ingester_tsdb_head_samples_appended_total` TSDB metric. #5952 * [BUGFIX] Querier: Enforce max query length check for `/api/v1/series` API even though `ignoreMaxQueryLength` is set to true. #6018 * [BUGFIX] Ingester: Fix issue with the minimize token generator where it was not taking in consideration the current ownerhip of an instance when generating extra tokens. #6062 -* [ENHANCEMENT] Ruler: Add support for filtering by `state` and `health` field on Rules API. #6040 ## 1.17.1 2024-05-20 diff --git a/pkg/ruler/api.go b/pkg/ruler/api.go index 64d4e67626..6c1a868966 100644 --- a/pkg/ruler/api.go +++ b/pkg/ruler/api.go @@ -147,6 +147,13 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) { return } + _, err = parseMatchersParam(req.Form["match[]"]) + if err != nil { + level.Error(logger).Log("msg", "error parsing match query params", "err", err) + util_api.RespondError(logger, w, v1.ErrBadData, fmt.Sprintf("error parsing match params %s", err), http.StatusBadRequest) + return + } + rulesRequest := RulesRequest{ RuleNames: req.Form["rule_name[]"], RuleGroupNames: req.Form["rule_group[]"], @@ -154,6 +161,7 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) { Type: typ, State: state, Health: health, + Matchers: req.Form["match[]"], } w.Header().Set("Content-Type", "application/json") diff --git a/pkg/ruler/ruler.go b/pkg/ruler/ruler.go index d07cd86033..f37bc3ce93 100644 --- a/pkg/ruler/ruler.go +++ b/pkg/ruler/ruler.go @@ -884,6 +884,10 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB ruleType := rulesRequest.Type alertState := rulesRequest.State health := rulesRequest.Health + matcherSets, err := parseMatchersParam(rulesRequest.Matchers) + if err != nil { + return nil, errors.Wrap(err, "error parsing matcher values") + } returnAlerts := ruleType == "" || ruleType == alertingRuleFilter returnRecording := (ruleType == "" || ruleType == recordingRuleFilter) && alertState == "" @@ -928,6 +932,9 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB if !returnByHealth(health, string(r.Health())) { continue } + if !matchesMatcherSets(matcherSets, r.Labels()) { + continue + } lastError := "" if r.LastError() != nil { lastError = r.LastError().Error() @@ -1009,6 +1016,7 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB fileSet, returnAlerts, returnRecording, + matcherSets, }) if err != nil { return nil, err @@ -1023,6 +1031,7 @@ type groupListFilter struct { fileSet map[string]struct{} returnAlerts bool returnRecording bool + matcherSets [][]*labels.Matcher } // ruleGroupListToGroupStateDesc converts rulespb.RuleGroupList to []*GroupStateDesc while accepting filters to control what goes to the @@ -1068,6 +1077,9 @@ func (r *Ruler) ruleGroupListToGroupStateDesc(userID string, backupGroups rulesp continue } } + if !matchesMatcherSets(filters.matcherSets, cortexpb.FromLabelAdaptersToLabels(r.Labels)) { + continue + } var ruleDesc *RuleStateDesc query, err := parser.ParseExpr(r.GetExpr()) @@ -1162,6 +1174,7 @@ func (r *Ruler) getShardedRules(ctx context.Context, userID string, rulesRequest RuleGroupNames: rulesRequest.GetRuleGroupNames(), Files: rulesRequest.GetFiles(), Type: rulesRequest.GetType(), + Matchers: rulesRequest.GetMatchers(), }) if err != nil { @@ -1320,3 +1333,48 @@ func returnByState(requestState string, alertState string) bool { func returnByHealth(requestHealth string, ruleHealth string) bool { return requestHealth == "" || requestHealth == ruleHealth } + +func parseMatchersParam(matchers []string) ([][]*labels.Matcher, error) { + var matcherSets [][]*labels.Matcher + for _, s := range matchers { + matchers, err := parser.ParseMetricSelector(s) + if err != nil { + return nil, err + } + matcherSets = append(matcherSets, matchers) + } + +OUTER: + for _, ms := range matcherSets { + for _, lm := range ms { + if lm != nil && !lm.Matches("") { + continue OUTER + } + } + return nil, errors.New("match[] must contain at least one non-empty matcher") + } + return matcherSets, nil +} + +func matches(l labels.Labels, matchers ...*labels.Matcher) bool { + for _, m := range matchers { + if v := l.Get(m.Name); !m.Matches(v) { + return false + } + } + return true +} + +// matchesMatcherSets ensures all matches in each matcher set are ANDed and the set of those is ORed. +func matchesMatcherSets(matcherSets [][]*labels.Matcher, l labels.Labels) bool { + if len(matcherSets) == 0 { + return true + } + + for _, matchers := range matcherSets { + if matches(l, matchers...) { + return true + } + } + return false +} diff --git a/pkg/ruler/ruler.pb.go b/pkg/ruler/ruler.pb.go index 192c8f3e25..213c8ee827 100644 --- a/pkg/ruler/ruler.pb.go +++ b/pkg/ruler/ruler.pb.go @@ -45,6 +45,7 @@ type RulesRequest struct { Type string `protobuf:"bytes,4,opt,name=type,proto3" json:"type,omitempty"` State string `protobuf:"bytes,5,opt,name=state,proto3" json:"state,omitempty"` Health string `protobuf:"bytes,6,opt,name=health,proto3" json:"health,omitempty"` + Matchers []string `protobuf:"bytes,7,rep,name=matchers,proto3" json:"matchers,omitempty"` } func (m *RulesRequest) Reset() { *m = RulesRequest{} } @@ -121,6 +122,13 @@ func (m *RulesRequest) GetHealth() string { return "" } +func (m *RulesRequest) GetMatchers() []string { + if m != nil { + return m.Matchers + } + return nil +} + type RulesResponse struct { Groups []*GroupStateDesc `protobuf:"bytes,1,rep,name=groups,proto3" json:"groups,omitempty"` } @@ -436,55 +444,56 @@ func init() { func init() { proto.RegisterFile("ruler.proto", fileDescriptor_9ecbec0a4cfddea6) } var fileDescriptor_9ecbec0a4cfddea6 = []byte{ - // 759 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x55, 0x4f, 0x4f, 0x13, 0x41, - 0x14, 0xdf, 0x69, 0x69, 0x69, 0xa7, 0x08, 0x71, 0xa8, 0x66, 0x6d, 0xcc, 0x94, 0xd4, 0xc4, 0x10, - 0x13, 0xb7, 0x49, 0x25, 0x31, 0x1e, 0xd0, 0x94, 0x00, 0x5e, 0x8c, 0x21, 0x5b, 0xf5, 0xda, 0x4c, - 0xdb, 0xe9, 0xb2, 0xb2, 0xdd, 0x59, 0x67, 0x66, 0x1b, 0xbc, 0xf9, 0x11, 0x38, 0x7a, 0xf6, 0xe4, - 0xc1, 0x0f, 0xc2, 0x91, 0x78, 0x22, 0xc6, 0xa0, 0x94, 0x8b, 0x47, 0x3e, 0x82, 0x99, 0x99, 0x5d, - 0xdb, 0x22, 0x26, 0x34, 0x86, 0x0b, 0xcc, 0xfb, 0xf3, 0x7b, 0x7f, 0x7e, 0xef, 0xed, 0x2b, 0x2c, - 0xf1, 0x38, 0xa0, 0xdc, 0x89, 0x38, 0x93, 0x0c, 0xe5, 0xb4, 0x50, 0x29, 0x7b, 0xcc, 0x63, 0x5a, - 0x53, 0x57, 0x2f, 0x63, 0xac, 0x60, 0x8f, 0x31, 0x2f, 0xa0, 0x75, 0x2d, 0x75, 0xe2, 0x7e, 0xbd, - 0x17, 0x73, 0x22, 0x7d, 0x16, 0x26, 0xf6, 0xea, 0x45, 0xbb, 0xf4, 0x07, 0x54, 0x48, 0x32, 0x88, - 0x12, 0x87, 0x27, 0x9e, 0x2f, 0x77, 0xe3, 0x8e, 0xd3, 0x65, 0x83, 0x7a, 0x97, 0x71, 0x49, 0xf7, - 0x23, 0xce, 0xde, 0xd2, 0xae, 0x4c, 0xa4, 0x7a, 0xb4, 0xe7, 0xa5, 0x86, 0x4e, 0xf2, 0x48, 0xa0, - 0xeb, 0x57, 0x81, 0xea, 0xe2, 0xf5, 0x5f, 0x11, 0x75, 0xcc, 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ce, 0xb0, 0x75, 0x74, 0x86, - 0xad, 0xe3, 0x33, 0x6c, 0x75, 0xf2, 0xba, 0xc6, 0x47, 0xbf, 0x03, 0x00, 0x00, 0xff, 0xff, 0x7c, - 0x42, 0x1b, 0x9b, 0xaa, 0x07, 0x00, 0x00, + // 774 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x55, 0x4d, 0x4f, 0x13, 0x4f, + 0x18, 0xdf, 0x69, 0xe9, 0xdb, 0x94, 0x3f, 0xe4, 0x3f, 0x54, 0xb3, 0x36, 0x64, 0x4b, 0x6a, 0x62, + 0x88, 0x89, 0xdb, 0xa4, 0x92, 0x18, 0x0f, 0x68, 0x4a, 0x00, 0x2f, 0xc6, 0x90, 0xad, 0x7a, 0x6d, + 0xa6, 0xed, 0x74, 0xbb, 0xb2, 0xdd, 0x59, 0x67, 0x66, 0x1b, 0xbc, 0xf9, 0x11, 0x38, 0x7a, 0xf6, + 0xe4, 0x47, 0xe1, 0x48, 0x3c, 0x11, 0x63, 0x50, 0xca, 0xc5, 0x23, 0x1f, 0xc1, 0xcc, 0xcc, 0x2e, + 0xdb, 0x22, 0x26, 0x34, 0x86, 0x0b, 0xcc, 0xf3, 0xf2, 0x7b, 0x5e, 0x7e, 0xcf, 0xb3, 0x4f, 0x61, + 0x99, 0x45, 0x3e, 0x61, 0x76, 0xc8, 0xa8, 0xa0, 0x28, 0xa7, 0x84, 0x6a, 0xc5, 0xa5, 0x2e, 0x55, + 0x9a, 0x86, 0x7c, 0x69, 0x63, 0xd5, 0x72, 0x29, 0x75, 0x7d, 0xd2, 0x50, 0x52, 0x37, 0x1a, 0x34, + 0xfa, 0x11, 0xc3, 0xc2, 0xa3, 0x41, 0x6c, 0xaf, 0x5d, 0xb5, 0x0b, 0x6f, 0x44, 0xb8, 0xc0, 0xa3, + 0x30, 0x76, 0x78, 0xea, 0x7a, 0x62, 0x18, 0x75, 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, 0xa9, 0x56, 0x66, + 0x95, 0xfa, 0x88, 0xd7, 0x8d, 0xad, 0x8d, 0xe3, 0x33, 0xcb, 0x38, 0x39, 0xb3, 0x8c, 0x8b, 0x33, + 0x0b, 0x7c, 0x9c, 0x58, 0xe0, 0xcb, 0xc4, 0x02, 0x47, 0x13, 0x0b, 0x1c, 0x4f, 0x2c, 0xf0, 0x73, + 0x62, 0x81, 0x5f, 0x13, 0xcb, 0xb8, 0x98, 0x58, 0xe0, 0xf0, 0xdc, 0x32, 0x8e, 0xcf, 0x2d, 0xe3, + 0xe4, 0xdc, 0x32, 0xba, 0x79, 0x55, 0xe3, 0xe3, 0xdf, 0x01, 0x00, 0x00, 0xff, 0xff, 0xb1, 0xdb, + 0xbe, 0x81, 0xc6, 0x07, 0x00, 0x00, } func (this *RulesRequest) Equal(that interface{}) bool { @@ -539,6 +548,14 @@ func (this *RulesRequest) Equal(that interface{}) bool { if this.Health != that1.Health { return false } + if len(this.Matchers) != len(that1.Matchers) { + return false + } + for i := range this.Matchers { + if this.Matchers[i] != that1.Matchers[i] { + return false + } + } return true } func (this *RulesResponse) Equal(that interface{}) bool { @@ -720,7 +737,7 @@ func (this *RulesRequest) GoString() string { if this == nil { return "nil" } - s := make([]string, 0, 10) + s := make([]string, 0, 11) 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") @@ -728,6 +745,7 @@ func (this *RulesRequest) GoString() string { s = append(s, "Type: "+fmt.Sprintf("%#v", this.Type)+",\n") s = append(s, "State: "+fmt.Sprintf("%#v", this.State)+",\n") s = append(s, "Health: "+fmt.Sprintf("%#v", this.Health)+",\n") + s = append(s, "Matchers: "+fmt.Sprintf("%#v", this.Matchers)+",\n") s = append(s, "}") return strings.Join(s, "") } @@ -908,6 +926,15 @@ func (m *RulesRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.Matchers) > 0 { + for iNdEx := len(m.Matchers) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.Matchers[iNdEx]) + copy(dAtA[i:], m.Matchers[iNdEx]) + i = encodeVarintRuler(dAtA, i, uint64(len(m.Matchers[iNdEx]))) + i-- + dAtA[i] = 0x3a + } + } if len(m.Health) > 0 { i -= len(m.Health) copy(dAtA[i:], m.Health) @@ -1306,6 +1333,12 @@ func (m *RulesRequest) Size() (n int) { if l > 0 { n += 1 + l + sovRuler(uint64(l)) } + if len(m.Matchers) > 0 { + for _, s := range m.Matchers { + l = len(s) + n += 1 + l + sovRuler(uint64(l)) + } + } return n } @@ -1439,6 +1472,7 @@ func (this *RulesRequest) String() string { `Type:` + fmt.Sprintf("%v", this.Type) + `,`, `State:` + fmt.Sprintf("%v", this.State) + `,`, `Health:` + fmt.Sprintf("%v", this.Health) + `,`, + `Matchers:` + fmt.Sprintf("%v", this.Matchers) + `,`, `}`, }, "") return s @@ -1745,6 +1779,38 @@ func (m *RulesRequest) Unmarshal(dAtA []byte) error { } m.Health = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex + case 7: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Matchers", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowRuler + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthRuler + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthRuler + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Matchers = append(m.Matchers, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipRuler(dAtA[iNdEx:]) diff --git a/pkg/ruler/ruler.proto b/pkg/ruler/ruler.proto index 15f3b0bccb..0d1aa45525 100644 --- a/pkg/ruler/ruler.proto +++ b/pkg/ruler/ruler.proto @@ -26,6 +26,7 @@ message RulesRequest { string type = 4; string state = 5; string health = 6; + repeated string matchers = 7; } message RulesResponse { diff --git a/pkg/ruler/ruler_test.go b/pkg/ruler/ruler_test.go index 937e2d1b01..7c8b7d9296 100644 --- a/pkg/ruler/ruler_test.go +++ b/pkg/ruler/ruler_test.go @@ -358,88 +358,139 @@ func TestGetRules(t *testing.T) { { Record: "rtest_user1_1", Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Labels: []cortexpb.LabelAdapter{ + {Name: "rulename", Value: "rtest_user1_group1_rule_1"}, + }, }, { Alert: "atest_user1_1", Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Labels: []cortexpb.LabelAdapter{ + {Name: "alertname", Value: "atest_user1_group1_rule_1"}, + }, }, }, "ruler1-user1-rule-group2": []*rulespb.RuleDesc{ { Record: "rtest_user1_1", Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Labels: []cortexpb.LabelAdapter{ + {Name: "rulename", Value: "rtest_user1_group2_rule_1"}, + }, }, }, "ruler1-user2-rule-group1": []*rulespb.RuleDesc{ { Record: "rtest_user1_1", Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Labels: []cortexpb.LabelAdapter{ + {Name: "rulename", Value: "rtest_user2_group1_rule_1"}, + }, }, }, "ruler2-user1-rule-group3": []*rulespb.RuleDesc{ { Record: "rtest_user1_1", Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Labels: []cortexpb.LabelAdapter{ + {Name: "rulename", Value: "rtest_user1_group3_rule_1"}, + }, }, { Alert: "atest_user1_1", Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Labels: []cortexpb.LabelAdapter{ + {Name: "alertname", Value: "atest_user1_group3_rule_1"}, + }, }, }, "ruler2-user2-rule-group1": []*rulespb.RuleDesc{ { Record: "rtest_user1_1", Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Labels: []cortexpb.LabelAdapter{ + {Name: "rulename", Value: "rtest_user2_group1_rule_1"}, + }, }, { Alert: "atest_user1_1", Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Labels: []cortexpb.LabelAdapter{ + {Name: "alertname", Value: "atest_user2_group1_rule_1"}, + }, }, }, "ruler2-user2-rule-group2": []*rulespb.RuleDesc{ { Record: "rtest_user2_1", Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Labels: []cortexpb.LabelAdapter{ + {Name: "rulename", Value: "rtest_user2_group2_rule_1"}, + }, }, { Alert: "atest_user2_1", Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Labels: []cortexpb.LabelAdapter{ + {Name: "alertname", Value: "atest_user2_group2_rule_1"}, + }, }, }, "ruler2-user3-rule-group1": []*rulespb.RuleDesc{ { Alert: "atest_user3_1", Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Labels: []cortexpb.LabelAdapter{ + {Name: "alertname", Value: "atest_user3_group1_rule_1"}, + }, }, }, "ruler3-user2-rule-group1": []*rulespb.RuleDesc{ { Record: "rtest_user1_1", Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Labels: []cortexpb.LabelAdapter{ + {Name: "rulename", Value: "rtest_user2_group1_rule_1"}, + }, }, { Alert: "atest_user1_1", Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Labels: []cortexpb.LabelAdapter{ + {Name: "alertname", Value: "atest_user2_group1_rule_1"}, + }, }, }, "ruler3-user2-rule-group2": []*rulespb.RuleDesc{ { Record: "rtest_user1_1", Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Labels: []cortexpb.LabelAdapter{ + {Name: "rulename", Value: "rtest_user2_group2_rule_1"}, + }, }, { Alert: "atest_user1_1", Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Labels: []cortexpb.LabelAdapter{ + {Name: "alertname", Value: "atest_user2_group2_rule_1"}, + }, }, }, "ruler3-user3-rule-group1": []*rulespb.RuleDesc{ { Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", Record: "rtest_user1_1", + Labels: []cortexpb.LabelAdapter{ + {Name: "templatedlabel", Value: "{{ $externalURL }}"}, + }, }, { Alert: "atest_user1_1", Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Labels: []cortexpb.LabelAdapter{ + {Name: "alertname", Value: "atest_user3_group1_rule_1"}, + }, }, }, } @@ -574,6 +625,30 @@ func TestGetRules(t *testing.T) { "user3": 3, }, }, + "No Sharding with Rule label matcher filter - match 1 rule": { + sharding: false, + rulesRequest: RulesRequest{ + Matchers: []string{`{alertname="atest_user1_group1_rule_1"}`}, + }, + rulerStateMap: rulerStateMapAllActive, + expectedCount: map[string]int{ + "user1": 1, + "user2": 0, + "user3": 0, + }, + }, + "No Sharding with Rule label matcher filter - label match all alerting rule": { + sharding: false, + rulesRequest: RulesRequest{ + Matchers: []string{`{alertname=~"atest_.*"}`}, + }, + rulerStateMap: rulerStateMapAllActive, + expectedCount: map[string]int{ + "user1": 2, + "user2": 4, + "user3": 2, + }, + }, "Default Sharding with No Filter": { sharding: true, shardingStrategy: util.ShardingStrategyDefault, @@ -685,6 +760,67 @@ func TestGetRules(t *testing.T) { expectedError: ring.ErrTooManyUnhealthyInstances, expectedClientCallCount: 0, }, + "Shuffle Sharding and ShardSize = 2 with Rule label Filter": { + sharding: true, + shuffleShardSize: 2, + shardingStrategy: util.ShardingStrategyShuffle, + rulesRequest: RulesRequest{ + Matchers: []string{`{alertname="atest_user1_group1_rule_1"}`}, + }, + rulerStateMap: rulerStateMapAllActive, + expectedCount: map[string]int{ + "user1": 1, + "user2": 0, + "user3": 0, + }, + expectedClientCallCount: 2, + }, + "Shuffle Sharding and ShardSize = 2 with Rule label Filter match 2 rules": { + sharding: true, + shuffleShardSize: 2, + shardingStrategy: util.ShardingStrategyShuffle, + rulesRequest: RulesRequest{ + Matchers: []string{`{alertname="atest_user1_group1_rule_1"}`, `{alertname="atest_user2_group1_rule_1"}`}, + }, + rulerStateMap: rulerStateMapAllActive, + expectedCount: map[string]int{ + "user1": 1, + "user2": 2, + "user3": 0, + }, + expectedClientCallCount: 2, + }, + "Shuffle Sharding and ShardSize = 2 with Rule label Filter match templating label": { + sharding: true, + shuffleShardSize: 2, + shardingStrategy: util.ShardingStrategyShuffle, + rulesRequest: RulesRequest{ + Matchers: []string{`{templatedlabel="{{ $externalURL }}"}`}, + }, + rulerStateMap: rulerStateMapAllActive, + expectedCount: map[string]int{ + "user1": 0, + "user2": 0, + "user3": 1, + }, + expectedClientCallCount: 2, + }, + "Shuffle Sharding and ShardSize = 3 with API Rules backup enabled with labels filter": { + sharding: true, + shuffleShardSize: 3, + shardingStrategy: util.ShardingStrategyShuffle, + rulerStateMap: rulerStateMapAllActive, + replicationFactor: 3, + rulesRequest: RulesRequest{ + Matchers: []string{`{alertname="atest_user1_group1_rule_1"}`, `{alertname="atest_user2_group1_rule_1"}`}, + }, + expectedCount: map[string]int{ + "user1": 1, + "user2": 2, + "user3": 0, + }, + expectedClientCallCount: 3, + }, "Shuffle Sharding and ShardSize = 3 with API Rules backup enabled": { sharding: true, shuffleShardSize: 3,