Skip to content

Commit

Permalink
query: fix panic when selecting non-default engine
Browse files Browse the repository at this point in the history
Fix duplicate metrics registration when selecting non-default engine

Signed-off-by: Michael Hoffmann <[email protected]>
  • Loading branch information
Michael Hoffmann committed Jan 9, 2025
1 parent 0d42636 commit 5895510
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions pkg/api/query/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,36 +133,29 @@ 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
}

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,
}
}

Expand Down

0 comments on commit 5895510

Please sign in to comment.