From 5895510a2e09a1d64ce0b680ee9f9af6443a44b0 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Thu, 9 Jan 2025 09:24:01 +0000 Subject: [PATCH] query: fix panic when selecting non-default engine Fix duplicate metrics registration when selecting non-default engine Signed-off-by: Michael Hoffmann --- pkg/api/query/v1.go | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/pkg/api/query/v1.go b/pkg/api/query/v1.go index 0eaec2de72..049dc482a7 100644 --- a/pkg/api/query/v1.go +++ b/pkg/api/query/v1.go @@ -133,26 +133,10 @@ type QueryEngineFactory struct { } func (f *QueryEngineFactory) GetPrometheusEngine() Engine { - f.createPrometheusEngine.Do(func() { - if f.prometheus != nil { - return - } - f.prometheus = &prometheusEngineAdapter{engine: promql.NewEngine(f.engineOpts.EngineOpts)} - }) return f.prometheus } func (f *QueryEngineFactory) GetThanosEngine() Engine { - f.createThanosEngine.Do(func() { - if f.thanos != nil { - return - } - if f.remoteEngineEndpoints == nil { - f.thanos = engine.New(f.engineOpts) - } else { - f.thanos = engine.NewDistributedEngine(f.engineOpts, f.remoteEngineEndpoints) - } - }) return f.thanos } @@ -160,9 +144,18 @@ func NewQueryEngineFactory( engineOpts engine.Opts, remoteEngineEndpoints promqlapi.RemoteEndpoints, ) *QueryEngineFactory { + promEngine := promql.NewEngine(engineOpts.EngineOpts) + // set engine here to avoid duplicate registration of metrics + engineOpts.Engine = promEngine + var thanosEngine Engine + if remoteEngineEndpoints == nil { + thanosEngine = engine.New(engineOpts) + } else { + thanosEngine = engine.NewDistributedEngine(engineOpts, remoteEngineEndpoints) + } return &QueryEngineFactory{ - engineOpts: engineOpts, - remoteEngineEndpoints: remoteEngineEndpoints, + prometheus: &prometheusEngineAdapter{promEngine}, + thanos: thanosEngine, } }