|
| 1 | +package instantquery |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/cortexproject/cortex/pkg/querier/tripperware" |
| 6 | + "github.com/prometheus/prometheus/promql/parser" |
| 7 | + "github.com/thanos-io/promql-engine/logicalplan" |
| 8 | + "github.com/thanos-io/promql-engine/query" |
| 9 | + "github.com/weaveworks/common/httpgrpc" |
| 10 | + "net/http" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + stepBatch = 10 |
| 16 | +) |
| 17 | + |
| 18 | +func InstantLogicalPlanGenMiddleware(lookbackDelta time.Duration, enablePerStepStats bool, disableDuplicateLabelChecks bool) tripperware.Middleware { |
| 19 | + return tripperware.MiddlewareFunc(func(next tripperware.Handler) tripperware.Handler { |
| 20 | + return instantLogicalPlanGen{ |
| 21 | + lookbackDelta: lookbackDelta, |
| 22 | + enabledPerStepStats: enablePerStepStats, |
| 23 | + disableDuplicateLabelChecks: disableDuplicateLabelChecks, |
| 24 | + next: next, |
| 25 | + } |
| 26 | + }) |
| 27 | +} |
| 28 | + |
| 29 | +type instantLogicalPlanGen struct { |
| 30 | + next tripperware.Handler |
| 31 | + lookbackDelta time.Duration |
| 32 | + enabledPerStepStats bool |
| 33 | + disableDuplicateLabelChecks bool |
| 34 | +} |
| 35 | + |
| 36 | +func (l instantLogicalPlanGen) NewInstantLogicalPlan(qs string, ts time.Time) ([]byte, error) { |
| 37 | + |
| 38 | + qOpts := query.Options{ |
| 39 | + Start: ts, |
| 40 | + End: ts, |
| 41 | + Step: 0, |
| 42 | + StepsBatch: stepBatch, |
| 43 | + LookbackDelta: l.lookbackDelta, |
| 44 | + EnablePerStepStats: l.disableDuplicateLabelChecks, |
| 45 | + } |
| 46 | + |
| 47 | + expr, err := parser.NewParser(qs, parser.WithFunctions(parser.Functions)).ParseExpr() |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + |
| 52 | + planOpts := logicalplan.PlanOptions{ |
| 53 | + DisableDuplicateLabelCheck: l.disableDuplicateLabelChecks, |
| 54 | + } |
| 55 | + |
| 56 | + logicalPlan := logicalplan.NewFromAST(expr, &qOpts, planOpts) |
| 57 | + optimizedPlan, _ := logicalPlan.Optimize(logicalplan.DefaultOptimizers) |
| 58 | + |
| 59 | + // TODO: Add distributed optimizer for remote node insertion |
| 60 | + |
| 61 | + byteLP, err := logicalplan.Marshal(optimizedPlan.Root()) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + return byteLP, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (l instantLogicalPlanGen) Do(ctx context.Context, r tripperware.Request) (tripperware.Response, error) { |
| 70 | + promReq, ok := r.(*tripperware.PrometheusRequest) |
| 71 | + if !ok { |
| 72 | + return nil, httpgrpc.Errorf(http.StatusBadRequest, "invalid request format") |
| 73 | + } |
| 74 | + |
| 75 | + instantTime := time.Unix(0, promReq.Start*int64(time.Millisecond)) |
| 76 | + |
| 77 | + byteLP, err := l.NewInstantLogicalPlan(promReq.Query, instantTime) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + promReq.LogicalPlan = byteLP |
| 82 | + |
| 83 | + return l.next.Do(ctx, r) |
| 84 | +} |
0 commit comments