mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* Alerting NG: prototype v2 (WIP) * Separate eval package * Modify eval alert definition endpoint * Disable migration if ngalert is not enabled * Remove premature test * Fix lint issues * Delete obsolete struct * Apply suggestions from code review * Update pkg/services/ngalert/ngalert.go Co-authored-by: Kyle Brandt <kyle@grafana.com> * Add API endpoint for listing alert definitions * Introduce index for alert_definition table * make ds object for expression to avoid panic * wrap error * Update pkg/services/ngalert/eval/eval.go * Swith to backend.DataQuery * Export TransformWrapper callback * Fix lint issues * Update pkg/services/ngalert/ngalert.go Co-authored-by: Kyle Brandt <kyle@grafana.com> * Validate alert definitions before storing them * Introduce AlertQuery * Add test * Add QueryType in AlertQuery * Accept only float64 (seconds) durations * Apply suggestions from code review * Get rid of bus * Do not export symbols * Fix failing test * Fix failure due to service initialization order Introduce MediumHigh service priority and assign it to backendplugin service * Fix test * Apply suggestions from code review * Fix renamed reference Co-authored-by: Kyle Brandt <kyle@grafana.com>
184 lines
6.1 KiB
Go
184 lines
6.1 KiB
Go
package ngalert
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/eval"
|
|
|
|
"github.com/go-macaron/binding"
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
"github.com/grafana/grafana/pkg/api"
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
"github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
|
"github.com/grafana/grafana/pkg/util"
|
|
)
|
|
|
|
func (ng *AlertNG) registerAPIEndpoints() {
|
|
ng.RouteRegister.Group("/api/alert-definitions", func(alertDefinitions routing.RouteRegister) {
|
|
alertDefinitions.Get("", middleware.ReqSignedIn, api.Wrap(ng.listAlertDefinitions))
|
|
alertDefinitions.Get("/eval/:alertDefinitionId", ng.validateOrgAlertDefinition, api.Wrap(ng.alertDefinitionEval))
|
|
alertDefinitions.Post("/eval", middleware.ReqSignedIn, binding.Bind(evalAlertConditionCommand{}), api.Wrap(ng.conditionEval))
|
|
alertDefinitions.Get("/:alertDefinitionId", ng.validateOrgAlertDefinition, api.Wrap(ng.getAlertDefinitionEndpoint))
|
|
alertDefinitions.Delete("/:alertDefinitionId", ng.validateOrgAlertDefinition, api.Wrap(ng.deleteAlertDefinitionEndpoint))
|
|
alertDefinitions.Post("/", middleware.ReqSignedIn, binding.Bind(saveAlertDefinitionCommand{}), api.Wrap(ng.createAlertDefinitionEndpoint))
|
|
alertDefinitions.Put("/:alertDefinitionId", ng.validateOrgAlertDefinition, binding.Bind(updateAlertDefinitionCommand{}), api.Wrap(ng.updateAlertDefinitionEndpoint))
|
|
})
|
|
}
|
|
|
|
// conditionEval handles POST /api/alert-definitions/eval.
|
|
func (ng *AlertNG) conditionEval(c *models.ReqContext, dto evalAlertConditionCommand) api.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.Condition.Execute(alertExecCtx, fromStr, toStr)
|
|
if err != nil {
|
|
return api.Error(400, "Failed to execute conditions", err)
|
|
}
|
|
|
|
evalResults, err := eval.EvaluateExecutionResult(execResult)
|
|
if err != nil {
|
|
return api.Error(400, "Failed to evaluate results", err)
|
|
}
|
|
|
|
frame := evalResults.AsDataFrame()
|
|
df := tsdb.NewDecodedDataFrames([]*data.Frame{&frame})
|
|
instances, err := df.Encoded()
|
|
if err != nil {
|
|
return api.Error(400, "Failed to encode result dataframes", err)
|
|
}
|
|
|
|
return api.JSON(200, util.DynMap{
|
|
"instances": instances,
|
|
})
|
|
}
|
|
|
|
// alertDefinitionEval handles GET /api/alert-definitions/eval/:dashboardId/:panelId/:refId".
|
|
func (ng *AlertNG) alertDefinitionEval(c *models.ReqContext) api.Response {
|
|
alertDefinitionID := c.ParamsInt64(":alertDefinitionId")
|
|
|
|
fromStr := c.Query("from")
|
|
if fromStr == "" {
|
|
fromStr = "now-3h"
|
|
}
|
|
|
|
toStr := c.Query("to")
|
|
if toStr == "" {
|
|
toStr = "now"
|
|
}
|
|
|
|
conditions, err := ng.LoadAlertCondition(alertDefinitionID, c.SignedInUser, c.SkipCache)
|
|
if err != nil {
|
|
return api.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 api.Error(400, "Failed to execute conditions", err)
|
|
}
|
|
|
|
evalResults, err := eval.EvaluateExecutionResult(execResult)
|
|
if err != nil {
|
|
return api.Error(400, "Failed to evaluate results", err)
|
|
}
|
|
|
|
frame := evalResults.AsDataFrame()
|
|
|
|
df := tsdb.NewDecodedDataFrames([]*data.Frame{&frame})
|
|
instances, err := df.Encoded()
|
|
if err != nil {
|
|
return api.Error(400, "Failed to encode result dataframes", err)
|
|
}
|
|
|
|
return api.JSON(200, util.DynMap{
|
|
"instances": instances,
|
|
})
|
|
}
|
|
|
|
// getAlertDefinitionEndpoint handles GET /api/alert-definitions/:alertDefinitionId.
|
|
func (ng *AlertNG) getAlertDefinitionEndpoint(c *models.ReqContext) api.Response {
|
|
alertDefinitionID := c.ParamsInt64(":alertDefinitionId")
|
|
|
|
query := getAlertDefinitionByIDQuery{
|
|
ID: alertDefinitionID,
|
|
}
|
|
|
|
if err := ng.getAlertDefinitionByID(&query); err != nil {
|
|
return api.Error(500, "Failed to get alert definition", err)
|
|
}
|
|
|
|
return api.JSON(200, &query.Result)
|
|
}
|
|
|
|
// deleteAlertDefinitionEndpoint handles DELETE /api/alert-definitions/:alertDefinitionId.
|
|
func (ng *AlertNG) deleteAlertDefinitionEndpoint(c *models.ReqContext) api.Response {
|
|
alertDefinitionID := c.ParamsInt64(":alertDefinitionId")
|
|
|
|
query := deleteAlertDefinitionByIDQuery{
|
|
ID: alertDefinitionID,
|
|
OrgID: c.SignedInUser.OrgId,
|
|
}
|
|
|
|
if err := ng.deleteAlertDefinitionByID(&query); err != nil {
|
|
return api.Error(500, "Failed to delete alert definition", err)
|
|
}
|
|
|
|
return api.JSON(200, util.DynMap{"affectedRows": query.RowsAffected})
|
|
}
|
|
|
|
// updateAlertDefinitionEndpoint handles PUT /api/alert-definitions/:alertDefinitionId.
|
|
func (ng *AlertNG) updateAlertDefinitionEndpoint(c *models.ReqContext, cmd updateAlertDefinitionCommand) api.Response {
|
|
cmd.ID = c.ParamsInt64(":alertDefinitionId")
|
|
cmd.SignedInUser = c.SignedInUser
|
|
cmd.SkipCache = c.SkipCache
|
|
|
|
if err := ng.updateAlertDefinition(&cmd); err != nil {
|
|
return api.Error(500, "Failed to update alert definition", err)
|
|
}
|
|
|
|
return api.JSON(200, util.DynMap{"affectedRows": cmd.RowsAffected, "id": cmd.Result.Id})
|
|
}
|
|
|
|
// createAlertDefinitionEndpoint handles POST /api/alert-definitions.
|
|
func (ng *AlertNG) createAlertDefinitionEndpoint(c *models.ReqContext, cmd saveAlertDefinitionCommand) api.Response {
|
|
cmd.OrgID = c.SignedInUser.OrgId
|
|
cmd.SignedInUser = c.SignedInUser
|
|
cmd.SkipCache = c.SkipCache
|
|
|
|
if err := ng.saveAlertDefinition(&cmd); err != nil {
|
|
return api.Error(500, "Failed to create alert definition", err)
|
|
}
|
|
|
|
return api.JSON(200, util.DynMap{"id": cmd.Result.Id})
|
|
}
|
|
|
|
// listAlertDefinitions handles GET /api/alert-definitions.
|
|
func (ng *AlertNG) listAlertDefinitions(c *models.ReqContext) api.Response {
|
|
cmd := listAlertDefinitionsCommand{OrgID: c.SignedInUser.OrgId}
|
|
|
|
if err := ng.getAlertDefinitions(&cmd); err != nil {
|
|
return api.Error(500, "Failed to list alert definitions", err)
|
|
}
|
|
|
|
return api.JSON(200, util.DynMap{"results": cmd.Result})
|
|
}
|