Alerting: Support recording rule struct in provisioning API (#87849)
* Support record struct in provisioning API * Update api spec * Use record field * Restrict API endpoints following toggle * Fix swagger spec * Add recording rule validation to store validator
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
prommodels "github.com/prometheus/common/model"
|
||||
|
||||
alertingModels "github.com/grafana/alerting/models"
|
||||
|
||||
@@ -510,14 +511,14 @@ func (alertRule *AlertRule) ValidateAlertRule(cfg setting.UnifiedAlertingSetting
|
||||
return fmt.Errorf("%w: cannot have Panel ID without a Dashboard UID", ErrAlertRuleFailedValidation)
|
||||
}
|
||||
|
||||
if !alertRule.IsRecordingRule() {
|
||||
if _, err := ErrStateFromString(string(alertRule.ExecErrState)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := NoDataStateFromString(string(alertRule.NoDataState)); err != nil {
|
||||
return err
|
||||
}
|
||||
var err error
|
||||
if alertRule.IsRecordingRule() {
|
||||
err = validateRecordingRuleFields(alertRule)
|
||||
} else {
|
||||
err = validateAlertRuleFields(alertRule)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if alertRule.For < 0 {
|
||||
@@ -543,6 +544,29 @@ func (alertRule *AlertRule) ValidateAlertRule(cfg setting.UnifiedAlertingSetting
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateAlertRuleFields(rule *AlertRule) error {
|
||||
if _, err := ErrStateFromString(string(rule.ExecErrState)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := NoDataStateFromString(string(rule.NoDataState)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateRecordingRuleFields(rule *AlertRule) error {
|
||||
metricName := prommodels.LabelValue(rule.Record.Metric)
|
||||
if !metricName.IsValid() {
|
||||
return fmt.Errorf("%w: %s", ErrAlertRuleFailedValidation, "metric name for recording rule must be a valid utf8 string")
|
||||
}
|
||||
if !prommodels.IsValidMetricName(metricName) {
|
||||
return fmt.Errorf("%w: %s", ErrAlertRuleFailedValidation, "metric name for recording rule must be a valid Prometheus metric name")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (alertRule *AlertRule) ResourceType() string {
|
||||
return "alertRule"
|
||||
}
|
||||
|
||||
@@ -603,6 +603,7 @@ func CopyRule(r *AlertRule, mutators ...AlertRuleMutator) *AlertRule {
|
||||
NoDataState: r.NoDataState,
|
||||
ExecErrState: r.ExecErrState,
|
||||
For: r.For,
|
||||
Record: r.Record,
|
||||
}
|
||||
|
||||
if r.DashboardUID != nil {
|
||||
|
||||
Reference in New Issue
Block a user