func()

in cmd/rule-evaluator/internal/rules.go [129:164]


func (api *API) groupToAPIGroup(group *rules.Group, ruleFilters []string, shouldReturnAlertRules, shouldReturnRecordingRules, shouldExcludeAlertsFromAlertRules bool) *apiv1.RuleGroup {
	apiGroupRules := []apiv1.Rule{}
	for _, groupRules := range group.Rules() {
		// If a rule_name parameter was specified, skip the rule if it doesn't match any of the specified values.
		if len(ruleFilters) > 0 && !slices.Contains(ruleFilters, groupRules.Name()) {
			continue
		}

		switch rule := groupRules.(type) {
		case *rules.AlertingRule:
			if !shouldReturnAlertRules {
				continue
			}
			apiGroupRules = append(apiGroupRules, alertingRuleToAPIRule(rule, shouldExcludeAlertsFromAlertRules))
		case *rules.RecordingRule:
			if !shouldReturnRecordingRules {
				continue
			}
			apiGroupRules = append(apiGroupRules, recordingRuleToAPIRule(rule))
		default:
			err := fmt.Errorf("alert rule %s is of unknown type %T", rule.Name(), rule)
			_ = level.Warn(api.logger).Log("msg", "failed to convert rule to API rule", "err", err)
			continue // ignore faulty rules - this should not break the endpoint.
		}
	}

	return &apiv1.RuleGroup{
		Name:           group.Name(),
		File:           group.File(),
		Interval:       group.Interval().Seconds(),
		Limit:          group.Limit(),
		Rules:          apiGroupRules,
		EvaluationTime: group.GetEvaluationTime().Seconds(),
		LastEvaluation: group.GetLastEvaluation(),
	}
}