Skip to content

Commit

Permalink
update Evaluator interface to accept context (grafana#52151)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuri-tceretian authored Jul 13, 2022
1 parent d71735d commit 0d4c503
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pkg/services/ngalert/api/api_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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")
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/services/ngalert/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}
Expand All @@ -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}
Expand Down
6 changes: 4 additions & 2 deletions pkg/services/ngalert/eval/evaluator_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/services/ngalert/schedule/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 0d4c503

Please sign in to comment.