Expressions: use datasource model from the query (#41376)

* refactor datasource loading

* refactor datasource loading

* pass uid

* use dscache in alerting to get DS

* remove expr/translate pacakge

* remove dup injection entry

* fix DS type on metrics endpoint, remove SQL DS lookup inside SSE

* update test and adapter

* comment fix

* Make eval run as admin when getting datasource info

Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>

* fmt and comment

* remove unncessary/redundant code

Co-authored-by: Kyle Brandt <kyle@grafana.com>
Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
Co-authored-by: Santiago <santiagohernandez.1997@gmail.com>
This commit is contained in:
Ryan McKinley
2021-12-16 13:51:46 -03:00
committed by GitHub
co-authored by Marcus Efraimsson Kyle Brandt Santiago
parent 1745cd8186
commit 2754e4fdf0
15 changed files with 119 additions and 1088 deletions
+31 -11
View File
@@ -13,6 +13,8 @@ import (
"github.com/grafana/grafana/pkg/expr/classic"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/setting"
@@ -23,8 +25,9 @@ import (
)
type Evaluator struct {
Cfg *setting.Cfg
Log log.Logger
Cfg *setting.Cfg
Log log.Logger
DataSourceCache datasources.CacheService
}
// invalidEvalResultFormatError is an error for invalid format of the alert definition evaluation results.
@@ -120,8 +123,8 @@ type AlertExecCtx struct {
Ctx context.Context
}
// GetExprRequest validates the condition and creates a expr.Request from it.
func GetExprRequest(ctx AlertExecCtx, data []models.AlertQuery, now time.Time) (*expr.Request, error) {
// GetExprRequest validates the condition, gets the datasource information and creates an expr.Request from it.
func GetExprRequest(ctx AlertExecCtx, data []models.AlertQuery, now time.Time, dsCacheService datasources.CacheService) (*expr.Request, error) {
req := &expr.Request{
OrgId: ctx.OrgID,
Headers: map[string]string{
@@ -131,6 +134,8 @@ func GetExprRequest(ctx AlertExecCtx, data []models.AlertQuery, now time.Time) (
},
}
datasources := make(map[string]*m.DataSource, len(data))
for i := range data {
q := data[i]
model, err := q.GetModel()
@@ -147,12 +152,27 @@ func GetExprRequest(ctx AlertExecCtx, data []models.AlertQuery, now time.Time) (
return nil, fmt.Errorf("failed to retrieve maxDatapoints from the model: %w", err)
}
ds, ok := datasources[q.DatasourceUID]
if !ok {
if expr.IsDataSource(q.DatasourceUID) {
ds = expr.DataSourceModel()
} else {
ds, err = dsCacheService.GetDatasourceByUID(q.DatasourceUID, &m.SignedInUser{
OrgId: ctx.OrgID,
OrgRole: m.ROLE_ADMIN, // Get DS as admin for service, API calls (test/post) must check permissions based on user.
}, true)
if err != nil {
return nil, err
}
}
datasources[q.DatasourceUID] = ds
}
req.Queries = append(req.Queries, expr.Query{
TimeRange: expr.TimeRange{
From: q.RelativeTimeRange.ToTimeRange(now).From,
To: q.RelativeTimeRange.ToTimeRange(now).To,
},
DatasourceUID: q.DatasourceUID,
DataSource: ds,
JSON: model,
Interval: interval,
RefID: q.RefID,
@@ -169,8 +189,8 @@ type NumberValueCapture struct {
Value *float64
}
func executeCondition(ctx AlertExecCtx, c *models.Condition, now time.Time, exprService *expr.Service) ExecutionResults {
execResp, err := executeQueriesAndExpressions(ctx, c.Data, now, exprService)
func executeCondition(ctx AlertExecCtx, c *models.Condition, now time.Time, exprService *expr.Service, dsCacheService datasources.CacheService) ExecutionResults {
execResp, err := executeQueriesAndExpressions(ctx, c.Data, now, exprService, dsCacheService)
if err != nil {
return ExecutionResults{Error: err}
}
@@ -253,7 +273,7 @@ func executeCondition(ctx AlertExecCtx, c *models.Condition, now time.Time, expr
return result
}
func executeQueriesAndExpressions(ctx AlertExecCtx, data []models.AlertQuery, now time.Time, exprService *expr.Service) (resp *backend.QueryDataResponse, err error) {
func executeQueriesAndExpressions(ctx AlertExecCtx, data []models.AlertQuery, now time.Time, exprService *expr.Service, dsCacheService datasources.CacheService) (resp *backend.QueryDataResponse, err error) {
defer func() {
if e := recover(); e != nil {
ctx.Log.Error("alert rule panic", "error", e, "stack", string(debug.Stack()))
@@ -266,7 +286,7 @@ func executeQueriesAndExpressions(ctx AlertExecCtx, data []models.AlertQuery, no
}
}()
queryDataReq, err := GetExprRequest(ctx, data, now)
queryDataReq, err := GetExprRequest(ctx, data, now, dsCacheService)
if err != nil {
return nil, err
}
@@ -507,7 +527,7 @@ func (e *Evaluator) ConditionEval(condition *models.Condition, now time.Time, ex
alertExecCtx := AlertExecCtx{OrgID: condition.OrgID, Ctx: alertCtx, ExpressionsEnabled: e.Cfg.ExpressionsEnabled, Log: e.Log}
execResult := executeCondition(alertExecCtx, condition, now, expressionService)
execResult := executeCondition(alertExecCtx, condition, now, expressionService, e.DataSourceCache)
evalResults := evaluateExecutionResult(execResult, now)
return evalResults, nil
@@ -520,7 +540,7 @@ func (e *Evaluator) QueriesAndExpressionsEval(orgID int64, data []models.AlertQu
alertExecCtx := AlertExecCtx{OrgID: orgID, Ctx: alertCtx, ExpressionsEnabled: e.Cfg.ExpressionsEnabled, Log: e.Log}
execResult, err := executeQueriesAndExpressions(alertExecCtx, data, now, expressionService)
execResult, err := executeQueriesAndExpressions(alertExecCtx, data, now, expressionService, e.DataSourceCache)
if err != nil {
return nil, fmt.Errorf("failed to execute conditions: %w", err)
}