[release-11.6.2] Alerting: Ensure field validators return the proper type (#105284)
Backend Unit Tests / Grafana (push) Waiting to run Details
Backend Unit Tests / Grafana Enterprise (push) Waiting to run Details
CodeQL checks / Analyze (go) (push) Waiting to run Details
CodeQL checks / Analyze (javascript) (push) Waiting to run Details
CodeQL checks / Analyze (python) (push) Waiting to run Details
Lint Frontend / Verify i18n (push) Waiting to run Details
Lint Frontend / Lint (push) Waiting to run Details
Lint Frontend / Typecheck (push) Waiting to run Details
Lint Frontend / Betterer (push) Waiting to run Details
End-to-end tests / Build & Package Grafana (push) Waiting to run Details
End-to-end tests / ${{ matrix.suite }} (dashboards-suite) (push) Blocked by required conditions Details
End-to-end tests / ${{ matrix.suite }} (panels-suite) (push) Blocked by required conditions Details
End-to-end tests / ${{ matrix.suite }} (smoke-tests-suite) (push) Blocked by required conditions Details
End-to-end tests / ${{ matrix.suite }} (various-suite) (push) Blocked by required conditions Details
End-to-end tests / ${{ matrix.suite }} (old arch) (old-arch/dashboards-suite) (push) Blocked by required conditions Details
End-to-end tests / ${{ matrix.suite }} (old arch) (old-arch/panels-suite) (push) Blocked by required conditions Details
End-to-end tests / ${{ matrix.suite }} (old arch) (old-arch/smoke-tests-suite) (push) Blocked by required conditions Details
End-to-end tests / ${{ matrix.suite }} (old arch) (old-arch/various-suite) (push) Blocked by required conditions Details
Frontend tests / Unit tests (${{ matrix.chunk }} / 8) (1) (push) Waiting to run Details
Frontend tests / Unit tests (${{ matrix.chunk }} / 8) (2) (push) Waiting to run Details
Frontend tests / Unit tests (${{ matrix.chunk }} / 8) (3) (push) Waiting to run Details
Frontend tests / Unit tests (${{ matrix.chunk }} / 8) (4) (push) Waiting to run Details
Frontend tests / Unit tests (${{ matrix.chunk }} / 8) (5) (push) Waiting to run Details
Frontend tests / Unit tests (${{ matrix.chunk }} / 8) (6) (push) Waiting to run Details
Frontend tests / Unit tests (${{ matrix.chunk }} / 8) (7) (push) Waiting to run Details
Frontend tests / Unit tests (${{ matrix.chunk }} / 8) (8) (push) Waiting to run Details
Integration Tests / Sqlite (push) Waiting to run Details
Integration Tests / MySQL (push) Waiting to run Details
Integration Tests / Postgres (push) Waiting to run Details
Dispatch sync to mirror / dispatch-job (push) Waiting to run Details

Alerting: Ensure field validators return the proper type (#104050)

* Ensure field validators return the proper type

This ensures correct error propagation through services up to
the API layer.

* Move error wrapping up to call site

(cherry picked from commit 820c338414)
This commit is contained in:
William Wernert 2025-05-13 08:49:43 -04:00 committed by GitHub
parent 28a2c941cc
commit df92cf28cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 51 additions and 3 deletions

View File

@ -624,7 +624,7 @@ func (alertRule *AlertRule) ValidateAlertRule(cfg setting.UnifiedAlertingSetting
err = validateAlertRuleFields(alertRule)
}
if err != nil {
return err
return fmt.Errorf("%w: %s", ErrAlertRuleFailedValidation, err)
}
if alertRule.For < 0 {
@ -665,10 +665,10 @@ func validateAlertRuleFields(rule *AlertRule) error {
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")
return errors.New("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 errors.New("metric name for recording rule must be a valid Prometheus metric name")
}
ClearRecordingRuleIgnoredFields(rule)

View File

@ -18,6 +18,7 @@ import (
"golang.org/x/exp/maps"
"gopkg.in/yaml.v3"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/util/cmputil"
)
@ -1018,6 +1019,53 @@ func TestGeneratorFillsAllFields(t *testing.T) {
require.FailNow(t, "AlertRule generator does not populate fields", "skipped fields: %v", maps.Keys(fields))
}
func TestValidateAlertRule(t *testing.T) {
t.Run("ExecErrState & NoDataState", func(t *testing.T) {
testCases := []struct {
name string
execErrState string
noDataState string
error bool
}{
{
name: "invalid error state",
execErrState: "invalid",
error: true,
},
{
name: "invalid no data state",
noDataState: "invalid",
error: true,
},
{
name: "valid states",
error: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
rule := RuleGen.With(
RuleMuts.WithIntervalSeconds(10),
).Generate()
if tc.execErrState != "" {
rule.ExecErrState = ExecutionErrorState(tc.execErrState)
}
if tc.noDataState != "" {
rule.NoDataState = NoDataState(tc.noDataState)
}
err := rule.ValidateAlertRule(setting.UnifiedAlertingSettings{BaseInterval: 10 * time.Second})
if tc.error {
require.Error(t, err)
require.ErrorIs(t, err, ErrAlertRuleFailedValidation)
} else {
require.NoError(t, err)
}
})
}
})
}
func TestAlertRule_PrometheusRuleDefinition(t *testing.T) {
tests := []struct {
name string