From 0d4c503d3d989924a66c02e8deb1e956f1f33371 Mon Sep 17 00:00:00 2001 From: Yuriy Tseretyan Date: Wed, 13 Jul 2022 10:21:11 -0400 Subject: [PATCH] update Evaluator interface to accept context (#52151) --- pkg/services/ngalert/api/api_testing.go | 4 ++-- pkg/services/ngalert/eval/eval.go | 12 ++++++------ pkg/services/ngalert/eval/evaluator_mock.go | 6 ++++-- pkg/services/ngalert/schedule/schedule.go | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/pkg/services/ngalert/api/api_testing.go b/pkg/services/ngalert/api/api_testing.go index 5ff65e3731c78..d10af199b4d39 100644 --- a/pkg/services/ngalert/api/api_testing.go +++ b/pkg/services/ngalert/api/api_testing.go @@ -55,7 +55,7 @@ func (srv TestingApiSrv) RouteTestGrafanaRuleConfig(c *models.ReqContext, body a now = timeNow() } - evalResults := srv.evaluator.ConditionEval(evalCond, now) + evalResults := srv.evaluator.ConditionEval(c.Req.Context(), evalCond, now) frame := evalResults.AsDataFrame() return response.JSONStreaming(http.StatusOK, util.DynMap{ @@ -118,7 +118,7 @@ func (srv TestingApiSrv) RouteEvalQueries(c *models.ReqContext, cmd apimodels.Ev return ErrResp(http.StatusBadRequest, err, "invalid queries or expressions") } - evalResults, err := srv.evaluator.QueriesAndExpressionsEval(c.SignedInUser.OrgId, cmd.Data, now) + evalResults, err := srv.evaluator.QueriesAndExpressionsEval(c.Req.Context(), c.SignedInUser.OrgId, cmd.Data, now) if err != nil { return ErrResp(http.StatusBadRequest, err, "Failed to evaluate queries and expressions") } diff --git a/pkg/services/ngalert/eval/eval.go b/pkg/services/ngalert/eval/eval.go index c5c942a207a7c..24f261b8b9b1c 100644 --- a/pkg/services/ngalert/eval/eval.go +++ b/pkg/services/ngalert/eval/eval.go @@ -28,9 +28,9 @@ import ( //go:generate mockery --name Evaluator --structname FakeEvaluator --inpackage --filename evaluator_mock.go --with-expecter type Evaluator interface { // ConditionEval executes conditions and evaluates the result. - ConditionEval(condition models.Condition, now time.Time) Results + ConditionEval(ctx context.Context, condition models.Condition, now time.Time) Results // QueriesAndExpressionsEval executes queries and expressions and returns the result. - QueriesAndExpressionsEval(orgID int64, data []models.AlertQuery, now time.Time) (*backend.QueryDataResponse, error) + QueriesAndExpressionsEval(ctx context.Context, orgID int64, data []models.AlertQuery, now time.Time) (*backend.QueryDataResponse, error) } type evaluatorImpl struct { @@ -592,8 +592,8 @@ func (evalResults Results) AsDataFrame() data.Frame { } // ConditionEval executes conditions and evaluates the result. -func (e *evaluatorImpl) ConditionEval(condition models.Condition, now time.Time) Results { - execResp, err := e.QueriesAndExpressionsEval(condition.OrgID, condition.Data, now) +func (e *evaluatorImpl) ConditionEval(ctx context.Context, condition models.Condition, now time.Time) Results { + execResp, err := e.QueriesAndExpressionsEval(ctx, condition.OrgID, condition.Data, now) var execResults ExecutionResults if err != nil { execResults = ExecutionResults{Error: err} @@ -604,8 +604,8 @@ func (e *evaluatorImpl) ConditionEval(condition models.Condition, now time.Time) } // QueriesAndExpressionsEval executes queries and expressions and returns the result. -func (e *evaluatorImpl) QueriesAndExpressionsEval(orgID int64, data []models.AlertQuery, now time.Time) (*backend.QueryDataResponse, error) { - alertCtx, cancelFn := context.WithTimeout(context.Background(), e.cfg.UnifiedAlerting.EvaluationTimeout) +func (e *evaluatorImpl) QueriesAndExpressionsEval(ctx context.Context, orgID int64, data []models.AlertQuery, now time.Time) (*backend.QueryDataResponse, error) { + alertCtx, cancelFn := context.WithTimeout(ctx, e.cfg.UnifiedAlerting.EvaluationTimeout) defer cancelFn() alertExecCtx := AlertExecCtx{OrgID: orgID, Ctx: alertCtx, ExpressionsEnabled: e.cfg.ExpressionsEnabled, Log: e.log} diff --git a/pkg/services/ngalert/eval/evaluator_mock.go b/pkg/services/ngalert/eval/evaluator_mock.go index 94e6064d11b07..6c80a3e581807 100644 --- a/pkg/services/ngalert/eval/evaluator_mock.go +++ b/pkg/services/ngalert/eval/evaluator_mock.go @@ -3,6 +3,8 @@ package eval import ( + "context" + backend "github.com/grafana/grafana-plugin-sdk-go/backend" mock "github.com/stretchr/testify/mock" @@ -25,7 +27,7 @@ func (_m *FakeEvaluator) EXPECT() *FakeEvaluator_Expecter { } // ConditionEval provides a mock function with given fields: condition, now -func (_m *FakeEvaluator) ConditionEval(condition models.Condition, now time.Time) Results { +func (_m *FakeEvaluator) ConditionEval(ctx context.Context, condition models.Condition, now time.Time) Results { ret := _m.Called(condition, now) var r0 Results @@ -65,7 +67,7 @@ func (_c *FakeEvaluator_ConditionEval_Call) Return(_a0 Results) *FakeEvaluator_C } // QueriesAndExpressionsEval provides a mock function with given fields: orgID, data, now -func (_m *FakeEvaluator) QueriesAndExpressionsEval(orgID int64, data []models.AlertQuery, now time.Time) (*backend.QueryDataResponse, error) { +func (_m *FakeEvaluator) QueriesAndExpressionsEval(ctx context.Context, orgID int64, data []models.AlertQuery, now time.Time) (*backend.QueryDataResponse, error) { ret := _m.Called(orgID, data, now) var r0 *backend.QueryDataResponse diff --git a/pkg/services/ngalert/schedule/schedule.go b/pkg/services/ngalert/schedule/schedule.go index 8d4fbf9874384..61f6ce7edb25b 100644 --- a/pkg/services/ngalert/schedule/schedule.go +++ b/pkg/services/ngalert/schedule/schedule.go @@ -392,7 +392,7 @@ func (sch *schedule) ruleRoutine(grafanaCtx context.Context, key ngmodels.AlertR logger := logger.New("version", r.Version, "attempt", attempt, "now", e.scheduledAt) start := sch.clock.Now() - results := sch.evaluator.ConditionEval(r.GetEvalCondition(), e.scheduledAt) + results := sch.evaluator.ConditionEval(ctx, r.GetEvalCondition(), e.scheduledAt) dur := sch.clock.Now().Sub(start) evalTotal.Inc() evalDuration.Observe(dur.Seconds())