@@ -26,7 +26,7 @@ type GRPCAPI struct {
26
26
now func () time.Time
27
27
replicaLabels []string
28
28
queryableCreate query.QueryableCreator
29
- engineFactory * QueryEngineFactory
29
+ queryFactory * QueryFactory
30
30
defaultEngine querypb.EngineType
31
31
lookbackDeltaCreate func (int64 ) time.Duration
32
32
defaultMaxResolutionSeconds time.Duration
@@ -36,7 +36,7 @@ func NewGRPCAPI(
36
36
now func () time.Time ,
37
37
replicaLabels []string ,
38
38
creator query.QueryableCreator ,
39
- engineFactory * QueryEngineFactory ,
39
+ queryFactory * QueryFactory ,
40
40
defaultEngine querypb.EngineType ,
41
41
lookbackDeltaCreate func (int64 ) time.Duration ,
42
42
defaultMaxResolutionSeconds time.Duration ,
@@ -45,7 +45,7 @@ func NewGRPCAPI(
45
45
now : now ,
46
46
replicaLabels : replicaLabels ,
47
47
queryableCreate : creator ,
48
- engineFactory : engineFactory ,
48
+ queryFactory : queryFactory ,
49
49
defaultEngine : defaultEngine ,
50
50
lookbackDeltaCreate : lookbackDeltaCreate ,
51
51
defaultMaxResolutionSeconds : defaultMaxResolutionSeconds ,
@@ -97,7 +97,7 @@ func (g *GRPCAPI) Query(request *querypb.QueryRequest, server querypb.Query_Quer
97
97
var qry promql.Query
98
98
if err := tracing .DoInSpanWithErr (ctx , "instant_query_create" , func (ctx context.Context ) error {
99
99
var err error
100
- qry , err = g .getQueryForEngine (ctx , request , queryable , maxResolution )
100
+ qry , err = g .getInstantQueryForEngine (ctx , request , queryable , maxResolution )
101
101
return err
102
102
}); err != nil {
103
103
return err
@@ -146,42 +146,6 @@ func (g *GRPCAPI) Query(request *querypb.QueryRequest, server querypb.Query_Quer
146
146
return nil
147
147
}
148
148
149
- func (g * GRPCAPI ) getQueryForEngine (ctx context.Context , request * querypb.QueryRequest , queryable storage.Queryable , maxResolution int64 ) (promql.Query , error ) {
150
- lookbackDelta := g .lookbackDeltaCreate (maxResolution * 1000 )
151
- if request .LookbackDeltaSeconds > 0 {
152
- lookbackDelta = time .Duration (request .LookbackDeltaSeconds ) * time .Second
153
- }
154
- engineParam := request .Engine
155
- if engineParam == querypb .EngineType_default {
156
- engineParam = g .defaultEngine
157
- }
158
-
159
- var ts time.Time
160
- if request .TimeSeconds == 0 {
161
- ts = g .now ()
162
- } else {
163
- ts = time .Unix (request .TimeSeconds , 0 )
164
- }
165
- opts := & engine.QueryOpts {
166
- LookbackDeltaParam : lookbackDelta ,
167
- EnablePartialResponses : request .EnablePartialResponse ,
168
- }
169
- switch engineParam {
170
- case querypb .EngineType_prometheus :
171
- queryEngine := g .engineFactory .GetPrometheusEngine ()
172
- return queryEngine .MakeInstantQuery (ctx , queryable , opts , request .Query , ts )
173
- case querypb .EngineType_thanos :
174
- queryEngine := g .engineFactory .GetThanosEngine ()
175
- plan , err := logicalplan .Unmarshal (request .QueryPlan .GetJson ())
176
- if err != nil {
177
- return queryEngine .MakeInstantQuery (ctx , queryable , opts , request .Query , ts )
178
- }
179
- return queryEngine .MakeInstantQueryFromPlan (ctx , queryable , opts , plan , ts )
180
- default :
181
- return nil , status .Error (codes .InvalidArgument , "invalid engine parameter" )
182
- }
183
- }
184
-
185
149
func (g * GRPCAPI ) QueryRange (request * querypb.QueryRangeRequest , srv querypb.Query_QueryRangeServer ) error {
186
150
ctx := srv .Context ()
187
151
if request .TimeoutSeconds != 0 {
@@ -295,14 +259,54 @@ func extractQueryStats(qry promql.Query) *querypb.QueryStats {
295
259
return stats
296
260
}
297
261
262
+ func (g * GRPCAPI ) getInstantQueryForEngine (ctx context.Context , request * querypb.QueryRequest , queryable storage.Queryable , maxResolution int64 ) (promql.Query , error ) {
263
+ lookbackDelta := g .lookbackDeltaCreate (maxResolution * 1000 )
264
+ if request .LookbackDeltaSeconds > 0 {
265
+ lookbackDelta = time .Duration (request .LookbackDeltaSeconds ) * time .Second
266
+ }
267
+ engineParam := request .Engine
268
+ if engineParam == querypb .EngineType_default {
269
+ engineParam = g .defaultEngine
270
+ }
271
+
272
+ var ts time.Time
273
+ if request .TimeSeconds == 0 {
274
+ ts = g .now ()
275
+ } else {
276
+ ts = time .Unix (request .TimeSeconds , 0 )
277
+ }
278
+ opts := & engine.QueryOpts {
279
+ LookbackDeltaParam : lookbackDelta ,
280
+ EnablePartialResponses : request .EnablePartialResponse ,
281
+ }
282
+
283
+ var engineType PromqlEngineType
284
+ switch engineParam {
285
+ case querypb .EngineType_prometheus :
286
+ engineType = PromqlEnginePrometheus
287
+ case querypb .EngineType_thanos :
288
+ engineType = PromqlEngineThanos
289
+ default :
290
+ return nil , status .Error (codes .InvalidArgument , "invalid engine parameter" )
291
+ }
292
+
293
+ var qry planOrQuery
294
+ if plan , err := logicalplan .Unmarshal (request .QueryPlan .GetJson ()); err != nil {
295
+ qry = planOrQuery {plan : plan }
296
+ } else {
297
+ qry = planOrQuery {query : request .Query }
298
+ }
299
+ return g .queryFactory .makeInstantQuery (ctx , engineType , queryable , qry , opts , ts )
300
+ }
301
+
298
302
func (g * GRPCAPI ) getRangeQueryForEngine (
299
303
ctx context.Context ,
300
304
request * querypb.QueryRangeRequest ,
301
305
queryable storage.Queryable ,
302
306
) (promql.Query , error ) {
303
- startTime := time .Unix (request .StartTimeSeconds , 0 )
304
- endTime := time .Unix (request .EndTimeSeconds , 0 )
305
- interval := time .Duration (request .IntervalSeconds ) * time .Second
307
+ start := time .Unix (request .StartTimeSeconds , 0 )
308
+ end := time .Unix (request .EndTimeSeconds , 0 )
309
+ step := time .Duration (request .IntervalSeconds ) * time .Second
306
310
307
311
engineParam := request .Engine
308
312
if engineParam == querypb .EngineType_default {
@@ -322,18 +326,21 @@ func (g *GRPCAPI) getRangeQueryForEngine(
322
326
EnablePartialResponses : request .EnablePartialResponse ,
323
327
}
324
328
329
+ var engineType PromqlEngineType
325
330
switch engineParam {
326
331
case querypb .EngineType_prometheus :
327
- queryEngine := g .engineFactory .GetPrometheusEngine ()
328
- return queryEngine .MakeRangeQuery (ctx , queryable , opts , request .Query , startTime , endTime , interval )
332
+ engineType = PromqlEnginePrometheus
329
333
case querypb .EngineType_thanos :
330
- thanosEngine := g .engineFactory .GetThanosEngine ()
331
- plan , err := logicalplan .Unmarshal (request .QueryPlan .GetJson ())
332
- if err != nil {
333
- return thanosEngine .MakeRangeQuery (ctx , queryable , opts , request .Query , startTime , endTime , interval )
334
- }
335
- return thanosEngine .MakeRangeQueryFromPlan (ctx , queryable , opts , plan , startTime , endTime , interval )
334
+ engineType = PromqlEngineThanos
336
335
default :
337
336
return nil , status .Error (codes .InvalidArgument , "invalid engine parameter" )
338
337
}
338
+
339
+ var qry planOrQuery
340
+ if plan , err := logicalplan .Unmarshal (request .QueryPlan .GetJson ()); err != nil {
341
+ qry = planOrQuery {plan : plan }
342
+ } else {
343
+ qry = planOrQuery {query : request .Query }
344
+ }
345
+ return g .queryFactory .makeRangeQuery (ctx , engineType , queryable , qry , opts , start , end , step )
339
346
}
0 commit comments