Alerting: Improve validation of query and expressions on rule submit (#53258)

* Improve error messages of server-side expression 
* move validation of alert queries and a condition to eval package
This commit is contained in:
Yuriy Tseretyan
2022-09-21 15:14:11 -04:00
committed by GitHub
parent 879241a48f
commit 2d38664fe6
19 changed files with 313 additions and 148 deletions

View File

@@ -1026,7 +1026,7 @@ func TestAlertRuleCRUD(t *testing.T) {
},
},
},
expectedMessage: "invalid rule specification at index [0]: failed to validate condition of alert rule AlwaysFiring: invalid query A: data source not found: unknown",
expectedMessage: "invalid rule specification at index [0]: failed to validate condition of alert rule AlwaysFiring: failed to build query 'A': data source not found",
},
{
desc: "alert rule with invalid condition",
@@ -1056,7 +1056,7 @@ func TestAlertRuleCRUD(t *testing.T) {
},
},
},
expectedMessage: "invalid rule specification at index [0]: failed to validate condition of alert rule AlwaysFiring: condition B not found in any query or expression: it should be one of: [A]",
expectedMessage: "invalid rule specification at index [0]: failed to validate condition of alert rule AlwaysFiring: condition B does not exist, must be one of [A]",
},
}
@@ -1074,7 +1074,7 @@ func TestAlertRuleCRUD(t *testing.T) {
err = json.Unmarshal([]byte(body), &res)
require.NoError(t, err)
assert.Equal(t, res.Message, tc.expectedMessage)
assert.Equal(t, tc.expectedMessage, res.Message)
assert.Equal(t, http.StatusBadRequest, status)
})
@@ -2264,7 +2264,7 @@ func TestEval(t *testing.T) {
`,
expectedStatusCode: func() int { return http.StatusBadRequest },
expectedMessage: func() string {
return "invalid condition: condition B not found in any query or expression: it should be one of: [A]"
return "invalid condition: condition B does not exist, must be one of [A]"
},
expectedResponse: func() string { return "" },
},
@@ -2300,7 +2300,7 @@ func TestEval(t *testing.T) {
if setting.IsEnterprise {
return "user is not authorized to query one or many data sources used by the rule"
}
return "invalid condition: invalid query A: data source not found: unknown"
return "invalid condition: failed to build query 'A': data source not found"
},
expectedResponse: func() string { return "" },
},
@@ -2483,7 +2483,7 @@ func TestEval(t *testing.T) {
if setting.IsEnterprise {
return "user is not authorized to query one or many data sources used by the rule"
}
return "invalid queries or expressions: invalid query A: data source not found: unknown"
return "Failed to evaluate queries and expressions: failed to execute conditions: failed to build query 'A': data source not found"
},
},
}