diff --git a/promql/engine.go b/promql/engine.go index 9b0a328797..bddc665727 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -207,13 +207,29 @@ func contextErr(err error, env string) error { } } +// QueryTracker provides access to two features: +// 1) tracking of active queries that are logged on restart if PromQL engine crashes while executing the query, and +// 2) enforcement of the maximum number of concurrent requests. +type QueryTracker interface { + // GetMaxConcurrent returns maximum number of concurrent queries that are allowed by this tracker. + GetMaxConcurrent() int + + // Insert inserts query into query tracker. This call must block if maximum number of queries is already running. + // If Insert doesn't return error then returned integer value should be used in subsequent Delete call. + // Insert should return error if context is finished before query can proceed, and integer value returned in this case should be ignored by caller. + Insert(ctx context.Context, query string) (int, error) + + // Delete removes query from activity tracker. InsertIndex is value returned by Insert call. + Delete(insertIndex int) +} + // EngineOpts contains configuration options used when creating a new Engine. type EngineOpts struct { Logger log.Logger Reg prometheus.Registerer MaxSamples int Timeout time.Duration - ActiveQueryTracker *ActiveQueryTracker + ActiveQueryTracker QueryTracker // LookbackDelta determines the time since the last sample after which a time // series is considered stale. LookbackDelta time.Duration @@ -236,7 +252,7 @@ type Engine struct { metrics *engineMetrics timeout time.Duration maxSamplesPerQuery int - activeQueryTracker *ActiveQueryTracker + activeQueryTracker QueryTracker queryLogger QueryLogger queryLoggerLock sync.RWMutex lookbackDelta time.Duration