Skip to content

Commit fddf1f5

Browse files
committed
Refactor getLocalRules to make the method shorter
Signed-off-by: Emmanuel Lodovice <[email protected]>
1 parent cf3b288 commit fddf1f5

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

pkg/ruler/ruler.go

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -980,15 +980,41 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
980980
}
981981

982982
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))
9831009
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 {
9861012
continue
9871013
}
9881014
}
9891015

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 {
9921018
continue
9931019
}
9941020
}
@@ -1014,20 +1040,19 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
10141040
name = r.GetAlert()
10151041
isAlertingRule = true
10161042
}
1017-
if len(ruleNameSet) > 0 {
1018-
if _, OK := ruleNameSet[name]; !OK {
1043+
if len(filters.ruleNameSet) > 0 {
1044+
if _, OK := filters.ruleNameSet[name]; !OK {
10191045
continue
10201046
}
10211047
}
1022-
lastError := ""
10231048

10241049
var ruleDesc *RuleStateDesc
10251050
query, err := parser.ParseExpr(r.GetExpr())
10261051
if err != nil {
10271052
return nil, errors.Errorf("failed to parse rule query '%v'", r.GetExpr())
10281053
}
10291054
if isAlertingRule {
1030-
if !returnAlerts {
1055+
if !filters.returnAlerts {
10311056
continue
10321057
}
10331058
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
10421067
},
10431068
State: promRules.StateInactive.String(), // backup rules are not evaluated so they are inactive
10441069
Health: string(promRules.HealthUnknown),
1045-
LastError: lastError,
10461070
Alerts: alerts,
10471071
EvaluationTimestamp: time.Time{},
10481072
EvaluationDuration: time.Duration(0),
10491073
}
10501074
} else {
1051-
if !returnRecording {
1075+
if !filters.returnRecording {
10521076
continue
10531077
}
10541078
ruleDesc = &RuleStateDesc{
@@ -1058,7 +1082,6 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
10581082
Labels: r.Labels,
10591083
},
10601084
Health: string(promRules.HealthUnknown),
1061-
LastError: lastError,
10621085
EvaluationTimestamp: time.Time{},
10631086
EvaluationDuration: time.Duration(0),
10641087
}
@@ -1069,7 +1092,6 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
10691092
groupDescs = append(groupDescs, groupDesc)
10701093
}
10711094
}
1072-
10731095
return groupDescs, nil
10741096
}
10751097

0 commit comments

Comments
 (0)