@@ -54,7 +54,7 @@ func (s splitByInterval) Do(ctx context.Context, r tripperware.Request) (tripper
54
54
// to line up the boundaries with step.
55
55
interval , err := s .interval (ctx , r )
56
56
if err != nil {
57
- return nil , httpgrpc .Errorf (http .StatusInternalServerError , err .Error ())
57
+ return nil , httpgrpc .Errorf (http .StatusBadRequest , err .Error ())
58
58
}
59
59
reqs , err := splitQuery (r , interval )
60
60
if err != nil {
@@ -153,11 +153,38 @@ func nextIntervalBoundary(t, step int64, interval time.Duration) int64 {
153
153
154
154
// Returns a fixed split interval
155
155
func staticIntervalFn (cfg Config ) func (ctx context.Context , r tripperware.Request ) (time.Duration , error ) {
156
- return func (_ context.Context , _ tripperware.Request ) (time.Duration , error ) {
156
+ return func (_ context.Context , r tripperware.Request ) (time.Duration , error ) {
157
+ totalRange , err := getTotalRangeFromRequest (r )
158
+ if err != nil {
159
+ return cfg .SplitQueriesByInterval , err
160
+ }
161
+ // Hardcode 7 day here, assuming SplitQueriesByInterval is configured as 1 day.
162
+ if totalRange > 7 * cfg .SplitQueriesByInterval {
163
+ return 7 * cfg .SplitQueriesByInterval , nil
164
+ }
157
165
return cfg .SplitQueriesByInterval , nil
158
166
}
159
167
}
160
168
169
+ // getTotalRangeFromRequest gets MatrixSelector and SubqueryExpr range sum from the query.
170
+ func getTotalRangeFromRequest (r tripperware.Request ) (time.Duration , error ) {
171
+ expr , err := parser .ParseExpr (r .GetQuery ())
172
+ if err != nil {
173
+ return 0 , err
174
+ }
175
+ totalRange := time .Duration (0 )
176
+ parser .Inspect (expr , func (node parser.Node , path []parser.Node ) error {
177
+ switch n := node .(type ) {
178
+ case * parser.MatrixSelector :
179
+ totalRange += n .Range
180
+ case * parser.SubqueryExpr :
181
+ totalRange += n .Range
182
+ }
183
+ return nil
184
+ })
185
+ return totalRange , nil
186
+ }
187
+
161
188
// Returns a dynamic multiple of base interval adjusted depending on configured 'max_shards_per_query' and 'max_fetched_data_duration_per_query'
162
189
func dynamicIntervalFn (cfg Config , limits tripperware.Limits , queryAnalyzer querysharding.Analyzer , lookbackDelta time.Duration ) func (ctx context.Context , r tripperware.Request ) (time.Duration , error ) {
163
190
return func (ctx context.Context , r tripperware.Request ) (time.Duration , error ) {
0 commit comments