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:
Matthew Jacobson
2024-01-10 14:40:00 -05:00
committed by GitHub
parent 31921bbb01
commit f365d35cf8
2 changed files with 93 additions and 2 deletions
+20 -2
View File
@@ -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")