@@ -980,15 +980,41 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
980
980
}
981
981
982
982
backupGroups := r .manager .GetBackupRules (userID )
983
+ backupGroupDescs , err := r .ruleGroupListToGroupStateDesc (userID , backupGroups , groupListFilter {
984
+ ruleNameSet ,
985
+ ruleGroupNameSet ,
986
+ fileSet ,
987
+ returnAlerts ,
988
+ returnRecording ,
989
+ })
990
+ if err != nil {
991
+ return nil , err
992
+ }
993
+
994
+ return append (groupDescs , backupGroupDescs ... ), nil
995
+ }
996
+
997
+ type groupListFilter struct {
998
+ ruleNameSet map [string ]struct {}
999
+ ruleGroupNameSet map [string ]struct {}
1000
+ fileSet map [string ]struct {}
1001
+ returnAlerts bool
1002
+ returnRecording bool
1003
+ }
1004
+
1005
+ // ruleGroupListToGroupStateDesc converts rulespb.RuleGroupList to []*GroupStateDesc while accepting filters to control what goes to the
1006
+ // resulting []*GroupStateDesc
1007
+ func (r * Ruler ) ruleGroupListToGroupStateDesc (userID string , backupGroups rulespb.RuleGroupList , filters groupListFilter ) ([]* GroupStateDesc , error ) {
1008
+ groupDescs := make ([]* GroupStateDesc , 0 , len (backupGroups ))
983
1009
for _ , group := range backupGroups {
984
- if len (fileSet ) > 0 {
985
- if _ , OK := fileSet [group .GetNamespace ()]; ! OK {
1010
+ if len (filters . fileSet ) > 0 {
1011
+ if _ , OK := filters . fileSet [group .GetNamespace ()]; ! OK {
986
1012
continue
987
1013
}
988
1014
}
989
1015
990
- if len (ruleGroupNameSet ) > 0 {
991
- if _ , OK := ruleGroupNameSet [group .GetName ()]; ! OK {
1016
+ if len (filters . ruleGroupNameSet ) > 0 {
1017
+ if _ , OK := filters . ruleGroupNameSet [group .GetName ()]; ! OK {
992
1018
continue
993
1019
}
994
1020
}
@@ -1014,20 +1040,19 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
1014
1040
name = r .GetAlert ()
1015
1041
isAlertingRule = true
1016
1042
}
1017
- if len (ruleNameSet ) > 0 {
1018
- if _ , OK := ruleNameSet [name ]; ! OK {
1043
+ if len (filters . ruleNameSet ) > 0 {
1044
+ if _ , OK := filters . ruleNameSet [name ]; ! OK {
1019
1045
continue
1020
1046
}
1021
1047
}
1022
- lastError := ""
1023
1048
1024
1049
var ruleDesc * RuleStateDesc
1025
1050
query , err := parser .ParseExpr (r .GetExpr ())
1026
1051
if err != nil {
1027
1052
return nil , errors .Errorf ("failed to parse rule query '%v'" , r .GetExpr ())
1028
1053
}
1029
1054
if isAlertingRule {
1030
- if ! returnAlerts {
1055
+ if ! filters . returnAlerts {
1031
1056
continue
1032
1057
}
1033
1058
alerts := []* AlertStateDesc {} // backup rules are not evaluated so there will be no active alerts
@@ -1042,13 +1067,12 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
1042
1067
},
1043
1068
State : promRules .StateInactive .String (), // backup rules are not evaluated so they are inactive
1044
1069
Health : string (promRules .HealthUnknown ),
1045
- LastError : lastError ,
1046
1070
Alerts : alerts ,
1047
1071
EvaluationTimestamp : time.Time {},
1048
1072
EvaluationDuration : time .Duration (0 ),
1049
1073
}
1050
1074
} else {
1051
- if ! returnRecording {
1075
+ if ! filters . returnRecording {
1052
1076
continue
1053
1077
}
1054
1078
ruleDesc = & RuleStateDesc {
@@ -1058,7 +1082,6 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
1058
1082
Labels : r .Labels ,
1059
1083
},
1060
1084
Health : string (promRules .HealthUnknown ),
1061
- LastError : lastError ,
1062
1085
EvaluationTimestamp : time.Time {},
1063
1086
EvaluationDuration : time .Duration (0 ),
1064
1087
}
@@ -1069,7 +1092,6 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
1069
1092
groupDescs = append (groupDescs , groupDesc )
1070
1093
}
1071
1094
}
1072
-
1073
1095
return groupDescs , nil
1074
1096
}
1075
1097
0 commit comments