@@ -884,6 +884,10 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
884
884
ruleType := rulesRequest .Type
885
885
alertState := rulesRequest .State
886
886
health := rulesRequest .Health
887
+ matcherSets , err := parseMatchersParam (rulesRequest .Matches )
888
+ if err != nil {
889
+ return nil , errors .Wrap (err , "error parsing matcher values" )
890
+ }
887
891
888
892
returnAlerts := ruleType == "" || ruleType == alertingRuleFilter
889
893
returnRecording := (ruleType == "" || ruleType == recordingRuleFilter ) && alertState == ""
@@ -928,6 +932,9 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
928
932
if ! returnByHealth (health , string (r .Health ())) {
929
933
continue
930
934
}
935
+ if ! matches (matcherSets , r .Labels ()) {
936
+ continue
937
+ }
931
938
lastError := ""
932
939
if r .LastError () != nil {
933
940
lastError = r .LastError ().Error ()
@@ -1009,6 +1016,7 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
1009
1016
fileSet ,
1010
1017
returnAlerts ,
1011
1018
returnRecording ,
1019
+ matcherSets ,
1012
1020
})
1013
1021
if err != nil {
1014
1022
return nil , err
@@ -1023,6 +1031,7 @@ type groupListFilter struct {
1023
1031
fileSet map [string ]struct {}
1024
1032
returnAlerts bool
1025
1033
returnRecording bool
1034
+ matcherSets [][]* labels.Matcher
1026
1035
}
1027
1036
1028
1037
// 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
1068
1077
continue
1069
1078
}
1070
1079
}
1080
+ if ! matches (filters .matcherSets , cortexpb .FromLabelAdaptersToLabels (r .Labels )) {
1081
+ continue
1082
+ }
1071
1083
1072
1084
var ruleDesc * RuleStateDesc
1073
1085
query , err := parser .ParseExpr (r .GetExpr ())
@@ -1162,6 +1174,7 @@ func (r *Ruler) getShardedRules(ctx context.Context, userID string, rulesRequest
1162
1174
RuleGroupNames : rulesRequest .GetRuleGroupNames (),
1163
1175
Files : rulesRequest .GetFiles (),
1164
1176
Type : rulesRequest .GetType (),
1177
+ Matches : rulesRequest .GetMatches (),
1165
1178
})
1166
1179
1167
1180
if err != nil {
@@ -1320,3 +1333,40 @@ func returnByState(requestState string, alertState string) bool {
1320
1333
func returnByHealth (requestHealth string , ruleHealth string ) bool {
1321
1334
return requestHealth == "" || requestHealth == ruleHealth
1322
1335
}
1336
+
1337
+ func parseMatchersParam (matchers []string ) ([][]* labels.Matcher , error ) {
1338
+ var matcherSets [][]* labels.Matcher
1339
+ for _ , s := range matchers {
1340
+ matchers , err := parser .ParseMetricSelector (s )
1341
+ if err != nil {
1342
+ return nil , err
1343
+ }
1344
+ matcherSets = append (matcherSets , matchers )
1345
+ }
1346
+
1347
+ OUTER:
1348
+ for _ , ms := range matcherSets {
1349
+ for _ , lm := range ms {
1350
+ if lm != nil && ! lm .Matches ("" ) {
1351
+ continue OUTER
1352
+ }
1353
+ }
1354
+ return nil , errors .New ("match[] must contain at least one non-empty matcher" )
1355
+ }
1356
+ return matcherSets , nil
1357
+ }
1358
+
1359
+ func matches (matcherSets [][]* labels.Matcher , l labels.Labels ) bool {
1360
+ if len (matcherSets ) == 0 {
1361
+ return true
1362
+ }
1363
+
1364
+ for _ , matchers := range matcherSets {
1365
+ for _ , m := range matchers {
1366
+ if v := l .Get (m .Name ); m .Matches (v ) {
1367
+ return true
1368
+ }
1369
+ }
1370
+ }
1371
+ return false
1372
+ }
0 commit comments