mirror of
https://github.com/grafana/grafana.git
synced 2024-11-29 12:14:08 -06:00
update Evaluator interface to accept context (#52151)
This commit is contained in:
parent
d71735d431
commit
0d4c503d3d
@ -55,7 +55,7 @@ func (srv TestingApiSrv) RouteTestGrafanaRuleConfig(c *models.ReqContext, body a
|
|||||||
now = timeNow()
|
now = timeNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
evalResults := srv.evaluator.ConditionEval(evalCond, now)
|
evalResults := srv.evaluator.ConditionEval(c.Req.Context(), evalCond, now)
|
||||||
|
|
||||||
frame := evalResults.AsDataFrame()
|
frame := evalResults.AsDataFrame()
|
||||||
return response.JSONStreaming(http.StatusOK, util.DynMap{
|
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")
|
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 {
|
if err != nil {
|
||||||
return ErrResp(http.StatusBadRequest, err, "Failed to evaluate queries and expressions")
|
return ErrResp(http.StatusBadRequest, err, "Failed to evaluate queries and expressions")
|
||||||
}
|
}
|
||||||
|
@ -28,9 +28,9 @@ import (
|
|||||||
//go:generate mockery --name Evaluator --structname FakeEvaluator --inpackage --filename evaluator_mock.go --with-expecter
|
//go:generate mockery --name Evaluator --structname FakeEvaluator --inpackage --filename evaluator_mock.go --with-expecter
|
||||||
type Evaluator interface {
|
type Evaluator interface {
|
||||||
// ConditionEval executes conditions and evaluates the result.
|
// 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 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 {
|
type evaluatorImpl struct {
|
||||||
@ -592,8 +592,8 @@ func (evalResults Results) AsDataFrame() data.Frame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ConditionEval executes conditions and evaluates the result.
|
// ConditionEval executes conditions and evaluates the result.
|
||||||
func (e *evaluatorImpl) ConditionEval(condition models.Condition, now time.Time) Results {
|
func (e *evaluatorImpl) ConditionEval(ctx context.Context, condition models.Condition, now time.Time) Results {
|
||||||
execResp, err := e.QueriesAndExpressionsEval(condition.OrgID, condition.Data, now)
|
execResp, err := e.QueriesAndExpressionsEval(ctx, condition.OrgID, condition.Data, now)
|
||||||
var execResults ExecutionResults
|
var execResults ExecutionResults
|
||||||
if err != nil {
|
if err != nil {
|
||||||
execResults = ExecutionResults{Error: err}
|
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.
|
// QueriesAndExpressionsEval executes queries and expressions and returns the result.
|
||||||
func (e *evaluatorImpl) QueriesAndExpressionsEval(orgID int64, data []models.AlertQuery, now time.Time) (*backend.QueryDataResponse, error) {
|
func (e *evaluatorImpl) QueriesAndExpressionsEval(ctx context.Context, orgID int64, data []models.AlertQuery, now time.Time) (*backend.QueryDataResponse, error) {
|
||||||
alertCtx, cancelFn := context.WithTimeout(context.Background(), e.cfg.UnifiedAlerting.EvaluationTimeout)
|
alertCtx, cancelFn := context.WithTimeout(ctx, e.cfg.UnifiedAlerting.EvaluationTimeout)
|
||||||
defer cancelFn()
|
defer cancelFn()
|
||||||
|
|
||||||
alertExecCtx := AlertExecCtx{OrgID: orgID, Ctx: alertCtx, ExpressionsEnabled: e.cfg.ExpressionsEnabled, Log: e.log}
|
alertExecCtx := AlertExecCtx{OrgID: orgID, Ctx: alertCtx, ExpressionsEnabled: e.cfg.ExpressionsEnabled, Log: e.log}
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
package eval
|
package eval
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
backend "github.com/grafana/grafana-plugin-sdk-go/backend"
|
backend "github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||||
mock "github.com/stretchr/testify/mock"
|
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
|
// 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)
|
ret := _m.Called(condition, now)
|
||||||
|
|
||||||
var r0 Results
|
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
|
// 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)
|
ret := _m.Called(orgID, data, now)
|
||||||
|
|
||||||
var r0 *backend.QueryDataResponse
|
var r0 *backend.QueryDataResponse
|
||||||
|
@ -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)
|
logger := logger.New("version", r.Version, "attempt", attempt, "now", e.scheduledAt)
|
||||||
start := sch.clock.Now()
|
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)
|
dur := sch.clock.Now().Sub(start)
|
||||||
evalTotal.Inc()
|
evalTotal.Inc()
|
||||||
evalDuration.Observe(dur.Seconds())
|
evalDuration.Observe(dur.Seconds())
|
||||||
|
Loading…
Reference in New Issue
Block a user