From 3117005b22fdcc47b3518709fb709daddfb4e1ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20=C5=A0tibran=C3=BD?= Date: Wed, 5 Jan 2022 10:34:15 +0100 Subject: [PATCH] Extract interface from ActivityQueryTracker and allows passing custom implementation (#102) * Extract QueryTracker interface. * Update from Prometheus PR 10071. * Update promql/engine.go Co-authored-by: Mauro Stettler * Update promql/engine.go Co-authored-by: Mauro Stettler Co-authored-by: Mauro Stettler --- promql/engine.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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