Skip to content

Commit

Permalink
Merge pull request #13713 from charleskorn/query-engine-interface
Browse files Browse the repository at this point in the history
rules: allow using alternative PromQL engines for rule evaluation by callers using Prometheus as a lib.
  • Loading branch information
bwplotka committed Mar 13, 2024
2 parents f1e9ec2 + 26262a1 commit 312e3fd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
6 changes: 6 additions & 0 deletions promql/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ func (e ErrStorage) Error() string {
return e.Err.Error()
}

// QueryEngine defines the interface for the *promql.Engine, so it can be replaced, wrapped or mocked.
type QueryEngine interface {
NewInstantQuery(ctx context.Context, q storage.Queryable, opts QueryOpts, qs string, ts time.Time) (Query, error)
NewRangeQuery(ctx context.Context, q storage.Queryable, opts QueryOpts, qs string, start, end time.Time, interval time.Duration) (Query, error)
}

// QueryLogger is an interface that can be used to log all the queries logged
// by the engine.
type QueryLogger interface {
Expand Down
2 changes: 1 addition & 1 deletion rules/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type QueryFunc func(ctx context.Context, q string, t time.Time) (promql.Vector,
// EngineQueryFunc returns a new query function that executes instant queries against
// the given engine.
// It converts scalar into vector results.
func EngineQueryFunc(engine *promql.Engine, q storage.Queryable) QueryFunc {
func EngineQueryFunc(engine promql.QueryEngine, q storage.Queryable) QueryFunc {
return func(ctx context.Context, qs string, t time.Time) (promql.Vector, error) {
q, err := engine.NewInstantQuery(ctx, q, nil, qs, t)
if err != nil {
Expand Down
11 changes: 2 additions & 9 deletions web/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,6 @@ type TSDBAdminStats interface {
WALReplayStatus() (tsdb.WALReplayStatus, error)
}

// QueryEngine defines the interface for the *promql.Engine, so it can be replaced, wrapped or mocked.
type QueryEngine interface {
SetQueryLogger(l promql.QueryLogger)
NewInstantQuery(ctx context.Context, q storage.Queryable, opts promql.QueryOpts, qs string, ts time.Time) (promql.Query, error)
NewRangeQuery(ctx context.Context, q storage.Queryable, opts promql.QueryOpts, qs string, start, end time.Time, interval time.Duration) (promql.Query, error)
}

type QueryOpts interface {
EnablePerStepStats() bool
LookbackDelta() time.Duration
Expand All @@ -193,7 +186,7 @@ type QueryOpts interface {
// them using the provided storage and query engine.
type API struct {
Queryable storage.SampleAndChunkQueryable
QueryEngine QueryEngine
QueryEngine promql.QueryEngine
ExemplarQueryable storage.ExemplarQueryable

scrapePoolsRetriever func(context.Context) ScrapePoolsRetriever
Expand Down Expand Up @@ -226,7 +219,7 @@ type API struct {

// NewAPI returns an initialized API type.
func NewAPI(
qe QueryEngine,
qe promql.QueryEngine,
q storage.SampleAndChunkQueryable,
ap storage.Appendable,
eq storage.ExemplarQueryable,
Expand Down
2 changes: 0 additions & 2 deletions web/api/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3881,8 +3881,6 @@ type fakeEngine struct {
query fakeQuery
}

func (e *fakeEngine) SetQueryLogger(promql.QueryLogger) {}

func (e *fakeEngine) NewInstantQuery(ctx context.Context, q storage.Queryable, opts promql.QueryOpts, qs string, ts time.Time) (promql.Query, error) {
return &e.query, nil
}
Expand Down

0 comments on commit 312e3fd

Please sign in to comment.