Alerting: Refactor rule interval validation to be reusable (#57792)
This commit is contained in:
@@ -21,16 +21,9 @@ func validateRuleNode(
|
|||||||
namespace *models.Folder,
|
namespace *models.Folder,
|
||||||
conditionValidator func(ngmodels.Condition) error,
|
conditionValidator func(ngmodels.Condition) error,
|
||||||
cfg *setting.UnifiedAlertingSettings) (*ngmodels.AlertRule, error) {
|
cfg *setting.UnifiedAlertingSettings) (*ngmodels.AlertRule, error) {
|
||||||
intervalSeconds := int64(interval.Seconds())
|
intervalSeconds, err := validateInterval(cfg, interval)
|
||||||
|
if err != nil {
|
||||||
baseIntervalSeconds := int64(cfg.BaseInterval.Seconds())
|
return nil, err
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ruleNode.GrafanaManagedAlert == nil {
|
if ruleNode.GrafanaManagedAlert == nil {
|
||||||
@@ -54,7 +47,6 @@ func validateRuleNode(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ruleNode.GrafanaManagedAlert.NoDataState != "" {
|
if ruleNode.GrafanaManagedAlert.NoDataState != "" {
|
||||||
var err error
|
|
||||||
noDataState, err = ngmodels.NoDataStateFromString(string(ruleNode.GrafanaManagedAlert.NoDataState))
|
noDataState, err = ngmodels.NoDataStateFromString(string(ruleNode.GrafanaManagedAlert.NoDataState))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -68,7 +60,6 @@ func validateRuleNode(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ruleNode.GrafanaManagedAlert.ExecErrState != "" {
|
if ruleNode.GrafanaManagedAlert.ExecErrState != "" {
|
||||||
var err error
|
|
||||||
errorState, err = ngmodels.ErrStateFromString(string(ruleNode.GrafanaManagedAlert.ExecErrState))
|
errorState, err = ngmodels.ErrStateFromString(string(ruleNode.GrafanaManagedAlert.ExecErrState))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -90,7 +81,7 @@ func validateRuleNode(
|
|||||||
Condition: ruleNode.GrafanaManagedAlert.Condition,
|
Condition: ruleNode.GrafanaManagedAlert.Condition,
|
||||||
Data: ruleNode.GrafanaManagedAlert.Data,
|
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)
|
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,
|
ExecErrState: errorState,
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
|
||||||
newAlertRule.For, err = validateForInterval(ruleNode)
|
newAlertRule.For, err = validateForInterval(ruleNode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -126,6 +116,22 @@ func validateRuleNode(
|
|||||||
return &newAlertRule, nil
|
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.
|
// 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) {
|
func validateForInterval(ruleNode *apimodels.PostableExtendedRuleNode) (time.Duration, error) {
|
||||||
if ruleNode.ApiRuleNode == nil || ruleNode.ApiRuleNode.For == nil {
|
if ruleNode.ApiRuleNode == nil || ruleNode.ApiRuleNode.For == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user