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 5229ca6
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions pkg/api/query/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"sort"
"strconv"
"strings"
"sync"
"time"

"github.com/go-kit/log"
Expand Down Expand Up @@ -122,47 +121,34 @@ func (a *prometheusEngineAdapter) MakeRangeQueryFromPlan(ctx context.Context, q
}

type QueryEngineFactory struct {
engineOpts engine.Opts
remoteEngineEndpoints promqlapi.RemoteEndpoints

createPrometheusEngine sync.Once
prometheus Engine

createThanosEngine sync.Once
thanos Engine
prometheus Engine
thanos Engine
}

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 5229ca6

Please sign in to comment.