AlertingNG: POC of evaluator under feature flag. (#27922)
* New feature toggle for enabling alerting NG * Initial commit * Modify evaluate alert API request * Check for unique labels in alert execution result dataframes * Remove print statement * Additional minor fixes/comments * Fix lint issues * Add API endpoint for evaluating panel queries * Push missing renaming * add refId for condition to API * add refId for condition to API * switch dashboard based eval to get method * add from/to params to dashboard based eval * add from/to params to eval endpoint Co-authored-by: kyle <kyle@grafana.com>
This commit is contained in:
co-authored by
kyle
parent
3928d0c531
commit
4acbcd7053
@@ -351,6 +351,13 @@ func (hs *HTTPServer) registerRoutes() {
|
||||
alertsRoute.Get("/states-for-dashboard", Wrap(GetAlertStatesForDashboard))
|
||||
})
|
||||
|
||||
if hs.Cfg.IsNgAlertEnabled() {
|
||||
apiRoute.Group("/alert-definitions", func(alertDefinitions routing.RouteRegister) {
|
||||
alertDefinitions.Get("/eval/:dashboardID/:panelID/:refID", reqEditorRole, Wrap(hs.AlertDefinitionEval))
|
||||
alertDefinitions.Post("/eval", reqEditorRole, bind(dtos.EvalAlertConditionsCommand{}), Wrap(hs.ConditionsEval))
|
||||
})
|
||||
}
|
||||
|
||||
apiRoute.Get("/alert-notifiers", reqEditorRole, Wrap(GetAlertNotifiers))
|
||||
|
||||
apiRoute.Group("/alert-notifications", func(alertNotifications routing.RouteRegister) {
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package dtos
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
eval "github.com/grafana/grafana/pkg/services/ngalert"
|
||||
)
|
||||
|
||||
type EvalAlertConditionsCommand struct {
|
||||
Conditions eval.Conditions `json:"conditions"`
|
||||
Now time.Time `json:"now"`
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/hooks"
|
||||
"github.com/grafana/grafana/pkg/services/login"
|
||||
eval "github.com/grafana/grafana/pkg/services/ngalert"
|
||||
"github.com/grafana/grafana/pkg/services/provisioning"
|
||||
"github.com/grafana/grafana/pkg/services/quota"
|
||||
"github.com/grafana/grafana/pkg/services/rendering"
|
||||
@@ -70,6 +71,7 @@ type HTTPServer struct {
|
||||
BackendPluginManager backendplugin.Manager `inject:""`
|
||||
PluginManager *plugins.PluginManager `inject:""`
|
||||
SearchService *search.SearchService `inject:""`
|
||||
AlertNG *eval.AlertNG `inject:""`
|
||||
Live *live.GrafanaLive
|
||||
Listener net.Listener
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
eval "github.com/grafana/grafana/pkg/services/ngalert"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/tsdb"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
// POST /api/alert-definitions/eval
|
||||
func (hs *HTTPServer) ConditionsEval(c *models.ReqContext, dto dtos.EvalAlertConditionsCommand) Response {
|
||||
alertCtx, cancelFn := context.WithTimeout(context.Background(), setting.AlertingEvaluationTimeout)
|
||||
defer cancelFn()
|
||||
|
||||
alertExecCtx := eval.AlertExecCtx{Ctx: alertCtx, SignedInUser: c.SignedInUser}
|
||||
|
||||
fromStr := c.Query("from")
|
||||
if fromStr == "" {
|
||||
fromStr = "now-3h"
|
||||
}
|
||||
|
||||
toStr := c.Query("to")
|
||||
if toStr == "" {
|
||||
toStr = "now"
|
||||
}
|
||||
|
||||
execResult, err := dto.Conditions.Execute(alertExecCtx, fromStr, toStr)
|
||||
if err != nil {
|
||||
return Error(400, "Failed to execute conditions", err)
|
||||
}
|
||||
|
||||
evalResults, err := eval.EvaluateExecutionResult(execResult)
|
||||
if err != nil {
|
||||
return Error(400, "Failed to evaluate results", err)
|
||||
}
|
||||
|
||||
frame := evalResults.AsDataFrame()
|
||||
df := tsdb.NewDecodedDataFrames([]*data.Frame{&frame})
|
||||
instances, err := df.Encoded()
|
||||
if err != nil {
|
||||
return Error(400, "Failed to encode result dataframes", err)
|
||||
}
|
||||
|
||||
return JSON(200, util.DynMap{
|
||||
"instances": instances,
|
||||
})
|
||||
}
|
||||
|
||||
// GET /api/alert-definitions/eval/:dashboardId/:panelId/:refId"
|
||||
func (hs *HTTPServer) AlertDefinitionEval(c *models.ReqContext) Response {
|
||||
dashboardID := c.ParamsInt64(":dashboardID")
|
||||
panelID := c.ParamsInt64(":panelID")
|
||||
conditionRefID := c.Params(":refID")
|
||||
|
||||
fromStr := c.Query("from")
|
||||
if fromStr == "" {
|
||||
fromStr = "now-3h"
|
||||
}
|
||||
|
||||
toStr := c.Query("to")
|
||||
if toStr == "" {
|
||||
toStr = "now"
|
||||
}
|
||||
|
||||
conditions, err := hs.AlertNG.LoadAlertConditions(dashboardID, panelID, conditionRefID, c.SignedInUser, c.SkipCache)
|
||||
if err != nil {
|
||||
return Error(400, "Failed to load conditions", err)
|
||||
}
|
||||
|
||||
alertCtx, cancelFn := context.WithTimeout(context.Background(), setting.AlertingEvaluationTimeout)
|
||||
defer cancelFn()
|
||||
|
||||
alertExecCtx := eval.AlertExecCtx{Ctx: alertCtx, SignedInUser: c.SignedInUser}
|
||||
|
||||
execResult, err := conditions.Execute(alertExecCtx, fromStr, toStr)
|
||||
if err != nil {
|
||||
return Error(400, "Failed to execute conditions", err)
|
||||
}
|
||||
|
||||
evalResults, err := eval.EvaluateExecutionResult(execResult)
|
||||
if err != nil {
|
||||
return Error(400, "Failed to evaluate results", err)
|
||||
}
|
||||
|
||||
frame := evalResults.AsDataFrame()
|
||||
|
||||
df := tsdb.NewDecodedDataFrames([]*data.Frame{&frame})
|
||||
instances, err := df.Encoded()
|
||||
if err != nil {
|
||||
return Error(400, "Failed to encode result dataframes", err)
|
||||
}
|
||||
|
||||
return JSON(200, util.DynMap{
|
||||
"instances": instances,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user