ruleGroupListToGroupStateDesc converts rulespb.RuleGroupList to []*GroupStateDesc while accepting filters to control what goes to the resulting []*GroupStateDesc
(userID string, backupGroups rulespb.RuleGroupList, filters groupListFilter)
| 1331 | // ruleGroupListToGroupStateDesc converts rulespb.RuleGroupList to []*GroupStateDesc while accepting filters to control what goes to the |
| 1332 | // resulting []*GroupStateDesc |
| 1333 | func (r *Ruler) ruleGroupListToGroupStateDesc(userID string, backupGroups rulespb.RuleGroupList, filters groupListFilter) ([]*GroupStateDesc, error) { |
| 1334 | groupDescs := make([]*GroupStateDesc, 0, len(backupGroups)) |
| 1335 | for _, group := range backupGroups { |
| 1336 | if len(filters.fileSet) > 0 { |
| 1337 | if _, OK := filters.fileSet[group.GetNamespace()]; !OK { |
| 1338 | continue |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | if len(filters.ruleGroupNameSet) > 0 { |
| 1343 | if _, OK := filters.ruleGroupNameSet[group.GetName()]; !OK { |
| 1344 | continue |
| 1345 | } |
| 1346 | } |
| 1347 | interval := r.cfg.EvaluationInterval |
| 1348 | if group.Interval != 0 { |
| 1349 | interval = group.Interval |
| 1350 | } |
| 1351 | |
| 1352 | groupDesc := &GroupStateDesc{ |
| 1353 | Group: &rulespb.RuleGroupDesc{ |
| 1354 | Name: group.GetName(), |
| 1355 | Namespace: group.GetNamespace(), |
| 1356 | Interval: interval, |
| 1357 | User: userID, |
| 1358 | Limit: group.Limit, |
| 1359 | QueryOffset: group.QueryOffset, |
| 1360 | Labels: group.Labels, |
| 1361 | }, |
| 1362 | // We are keeping default value for EvaluationTimestamp and EvaluationDuration since the backup is not evaluating |
| 1363 | } |
| 1364 | for _, r := range group.GetRules() { |
| 1365 | name := r.GetRecord() |
| 1366 | isAlertingRule := false |
| 1367 | if name == "" { |
| 1368 | name = r.GetAlert() |
| 1369 | isAlertingRule = true |
| 1370 | } |
| 1371 | if len(filters.ruleNameSet) > 0 { |
| 1372 | if _, OK := filters.ruleNameSet[name]; !OK { |
| 1373 | continue |
| 1374 | } |
| 1375 | } |
| 1376 | if !matchesMatcherSets(filters.matcherSets, cortexpb.FromLabelAdaptersToLabels(r.Labels)) { |
| 1377 | continue |
| 1378 | } |
| 1379 | |
| 1380 | var ruleDesc *RuleStateDesc |
| 1381 | query, err := cortexparser.ParseExpr(r.GetExpr()) |
| 1382 | if err != nil { |
| 1383 | return nil, errors.Errorf("failed to parse rule query '%v'", r.GetExpr()) |
| 1384 | } |
| 1385 | if isAlertingRule { |
| 1386 | if !filters.returnAlerts { |
| 1387 | continue |
| 1388 | } |
| 1389 | alerts := []*AlertStateDesc{} // backup rules are not evaluated so there will be no active alerts |
| 1390 | ruleDesc = &RuleStateDesc{ |
no test coverage detected