Alerting: Show warning when query optimized (#78751)
* Alerting: Show warning when query optimized * Use frame.AppendNotices * Improve warning to include why and a prompt for action
This commit is contained in:
@@ -8,9 +8,11 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/benbjohnson/clock"
|
||||
"github.com/grafana/alerting/models"
|
||||
amv2 "github.com/prometheus/alertmanager/api/v2/models"
|
||||
|
||||
"github.com/grafana/alerting/models"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
@@ -163,7 +165,7 @@ func (srv TestingApiSrv) RouteEvalQueries(c *contextmodel.ReqContext, cmd apimod
|
||||
cond.Condition = cond.Data[len(cond.Data)-1].RefID
|
||||
}
|
||||
|
||||
_, err := store.OptimizeAlertQueries(cond.Data)
|
||||
optimizations, err := store.OptimizeAlertQueries(cond.Data)
|
||||
if err != nil {
|
||||
return ErrResp(http.StatusInternalServerError, err, "Failed to optimize query")
|
||||
}
|
||||
@@ -185,9 +187,25 @@ func (srv TestingApiSrv) RouteEvalQueries(c *contextmodel.ReqContext, cmd apimod
|
||||
return ErrResp(http.StatusInternalServerError, err, "Failed to evaluate queries and expressions")
|
||||
}
|
||||
|
||||
addOptimizedQueryWarnings(evalResults, optimizations)
|
||||
return response.JSONStreaming(http.StatusOK, evalResults)
|
||||
}
|
||||
|
||||
// addOptimizedQueryWarnings adds warnings to the query results for any queries that were optimized.
|
||||
func addOptimizedQueryWarnings(evalResults *backend.QueryDataResponse, optimizations []store.Optimization) {
|
||||
for _, opt := range optimizations {
|
||||
if res, ok := evalResults.Responses[opt.RefID]; ok {
|
||||
if len(res.Frames) > 0 {
|
||||
res.Frames[0].AppendNotices(data.Notice{
|
||||
Severity: data.NoticeSeverityWarning,
|
||||
Text: "Query optimized from Range to Instant type; all uses exclusively require the last datapoint. " +
|
||||
"Consider modifying your query to Instant type to ensure accuracy.", // Currently this is the only optimization we do.
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (srv TestingApiSrv) BacktestAlertRule(c *contextmodel.ReqContext, cmd apimodels.BacktestConfig) response.Response {
|
||||
if !srv.featureManager.IsEnabled(c.Req.Context(), featuremgmt.FlagAlertingBacktesting) {
|
||||
return ErrResp(http.StatusNotFound, nil, "Backgtesting API is not enabled")
|
||||
|
||||
Reference in New Issue
Block a user