Alerting: Refactor rule interval validation to be reusable (#57792)

This commit is contained in:
Yuriy Tseretyan 2022-10-28 10:40:11 -04:00 committed by GitHub
parent e4d1d8d70c
commit d848cc629b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,16 +21,9 @@ func validateRuleNode(
namespace *models.Folder,
conditionValidator func(ngmodels.Condition) error,
cfg *setting.UnifiedAlertingSettings) (*ngmodels.AlertRule, error) {
intervalSeconds := int64(interval.Seconds())
baseIntervalSeconds := int64(cfg.BaseInterval.Seconds())
if interval <= 0 {
return nil, fmt.Errorf("rule evaluation interval must be positive duration that is multiple of the base interval %d seconds", baseIntervalSeconds)
}
if intervalSeconds%baseIntervalSeconds != 0 {
return nil, fmt.Errorf("rule evaluation interval %d should be multiple of the base interval of %d seconds", int64(interval.Seconds()), baseIntervalSeconds)
intervalSeconds, err := validateInterval(cfg, interval)
if err != nil {
return nil, err
}
if ruleNode.GrafanaManagedAlert == nil {
@ -54,7 +47,6 @@ func validateRuleNode(
}
if ruleNode.GrafanaManagedAlert.NoDataState != "" {
var err error
noDataState, err = ngmodels.NoDataStateFromString(string(ruleNode.GrafanaManagedAlert.NoDataState))
if err != nil {
return nil, err
@ -68,7 +60,6 @@ func validateRuleNode(
}
if ruleNode.GrafanaManagedAlert.ExecErrState != "" {
var err error
errorState, err = ngmodels.ErrStateFromString(string(ruleNode.GrafanaManagedAlert.ExecErrState))
if err != nil {
return nil, err
@ -90,7 +81,7 @@ func validateRuleNode(
Condition: ruleNode.GrafanaManagedAlert.Condition,
Data: ruleNode.GrafanaManagedAlert.Data,
}
if err := conditionValidator(cond); err != nil {
if err = conditionValidator(cond); err != nil {
return nil, fmt.Errorf("failed to validate condition of alert rule %s: %w", ruleNode.GrafanaManagedAlert.Title, err)
}
}
@ -108,7 +99,6 @@ func validateRuleNode(
ExecErrState: errorState,
}
var err error
newAlertRule.For, err = validateForInterval(ruleNode)
if err != nil {
return nil, err
@ -126,6 +116,22 @@ func validateRuleNode(
return &newAlertRule, nil
}
func validateInterval(cfg *setting.UnifiedAlertingSettings, interval time.Duration) (int64, error) {
intervalSeconds := int64(interval.Seconds())
baseIntervalSeconds := int64(cfg.BaseInterval.Seconds())
if interval <= 0 {
return 0, fmt.Errorf("rule evaluation interval must be positive duration that is multiple of the base interval %d seconds", baseIntervalSeconds)
}
if intervalSeconds%baseIntervalSeconds != 0 {
return 0, fmt.Errorf("rule evaluation interval %d should be multiple of the base interval of %d seconds", int64(interval.Seconds()), baseIntervalSeconds)
}
return intervalSeconds, nil
}
// validateForInterval validates ApiRuleNode.For and converts it to time.Duration. If the field is not specified returns 0 if GrafanaManagedAlert.UID is empty and -1 if it is not.
func validateForInterval(ruleNode *apimodels.PostableExtendedRuleNode) (time.Duration, error) {
if ruleNode.ApiRuleNode == nil || ruleNode.ApiRuleNode.For == nil {