MCPcopy Create free account
hub / github.com/cortexproject/cortex / getLocalRules

Method getLocalRules

pkg/ruler/ruler.go:1130–1320  ·  view source on GitHub ↗
(userID string, rulesRequest RulesRequest, includeBackups bool)

Source from the content-addressed store, hash-verified

1128}
1129
1130func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeBackups bool) (RulesResponse, error) {
1131 groups := r.manager.GetRules(userID)
1132
1133 groupDescs := make([]*GroupStateDesc, 0, len(groups))
1134 prefix := filepath.Join(r.cfg.RulePath, userID) + "/"
1135
1136 sliceToSet := func(values []string) map[string]struct{} {
1137 set := make(map[string]struct{}, len(values))
1138 for _, v := range values {
1139 set[v] = struct{}{}
1140 }
1141 return set
1142 }
1143
1144 ruleNameSet := sliceToSet(rulesRequest.RuleNames)
1145 ruleGroupNameSet := sliceToSet(rulesRequest.RuleGroupNames)
1146 fileSet := sliceToSet(rulesRequest.Files)
1147 ruleType := rulesRequest.Type
1148 alertState := rulesRequest.State
1149 health := rulesRequest.Health
1150 matcherSets, err := parseMatchersParam(rulesRequest.Matchers)
1151 if err != nil {
1152 return RulesResponse{}, errors.Wrap(err, "error parsing matcher values")
1153 }
1154
1155 returnAlerts := ruleType == "" || ruleType == alertingRuleFilter
1156 returnRecording := (ruleType == "" || ruleType == recordingRuleFilter) && alertState == ""
1157
1158 for _, group := range groups {
1159 // The mapped filename is url path escaped encoded to make handling `/` characters easier
1160 decodedNamespace, err := url.PathUnescape(strings.TrimPrefix(group.File(), prefix))
1161 if err != nil {
1162 return RulesResponse{}, errors.Wrap(err, "unable to decode rule filename")
1163 }
1164 if len(fileSet) > 0 {
1165 if _, OK := fileSet[decodedNamespace]; !OK {
1166 continue
1167 }
1168 }
1169
1170 if len(ruleGroupNameSet) > 0 {
1171 if _, OK := ruleGroupNameSet[group.Name()]; !OK {
1172 continue
1173 }
1174 }
1175 interval := group.Interval()
1176
1177 queryOffset := group.QueryOffset()
1178 groupDesc := &GroupStateDesc{
1179 Group: &rulespb.RuleGroupDesc{
1180 Name: group.Name(),
1181 Namespace: string(decodedNamespace),
1182 Interval: interval,
1183 User: userID,
1184 Limit: int64(group.Limit()),
1185 QueryOffset: &queryOffset,
1186 },
1187

Callers 2

GetRulesMethod · 0.95
RulesMethod · 0.95

Calls 15

parseMatchersParamFunction · 0.85
returnByHealthFunction · 0.85
matchesMatcherSetsFunction · 0.85
returnByStateFunction · 0.85
PaginatedGroupStatesTypeAlias · 0.85
GetRuleGroupNextTokenFunction · 0.85
generatePageFunction · 0.85
JoinMethod · 0.80
GetRulesMethod · 0.65
WrapMethod · 0.65

Tested by

no test coverage detected