(ctx context.Context, r tripperware.Request)
| 38 | } |
| 39 | |
| 40 | func (l limitsMiddleware) Do(ctx context.Context, r tripperware.Request) (tripperware.Response, error) { |
| 41 | log, ctx := spanlogger.New(ctx, "limits") |
| 42 | defer log.Finish() |
| 43 | |
| 44 | tenantIDs, err := users.TenantIDs(ctx) |
| 45 | if err != nil { |
| 46 | return nil, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) |
| 47 | } |
| 48 | |
| 49 | // Clamp the time range based on the max query lookback. |
| 50 | |
| 51 | if maxQueryLookback := validation.SmallestPositiveNonZeroDurationPerTenant(tenantIDs, l.MaxQueryLookback); maxQueryLookback > 0 { |
| 52 | minStartTime := util.TimeToMillis(time.Now().Add(-maxQueryLookback)) |
| 53 | |
| 54 | if r.GetEnd() < minStartTime { |
| 55 | // The request is fully outside the allowed range, so we can return an |
| 56 | // empty response. |
| 57 | level.Debug(log).Log( |
| 58 | "msg", "skipping the execution of the query because its time range is before the 'max query lookback' setting", |
| 59 | "reqStart", util.FormatTimeMillis(r.GetStart()), |
| 60 | "redEnd", util.FormatTimeMillis(r.GetEnd()), |
| 61 | "maxQueryLookback", maxQueryLookback) |
| 62 | |
| 63 | return tripperware.NewEmptyPrometheusResponse(false), nil |
| 64 | } |
| 65 | |
| 66 | if r.GetStart() < minStartTime { |
| 67 | // Replace the start time in the request. |
| 68 | level.Debug(log).Log( |
| 69 | "msg", "the start time of the query has been manipulated because of the 'max query lookback' setting", |
| 70 | "original", util.FormatTimeMillis(r.GetStart()), |
| 71 | "updated", util.FormatTimeMillis(minStartTime)) |
| 72 | |
| 73 | r = r.WithStartEnd(minStartTime, r.GetEnd()) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Enforce the max query length. |
| 78 | maxQueryLength := validation.SmallestPositiveNonZeroDurationPerTenant(tenantIDs, l.MaxQueryLength) |
| 79 | if maxQueryLength > 0 { |
| 80 | queryLen := timestamp.Time(r.GetEnd()).Sub(timestamp.Time(r.GetStart())) |
| 81 | if queryLen > maxQueryLength { |
| 82 | return nil, httpgrpc.Errorf(http.StatusBadRequest, validation.ErrQueryTooLong, queryLen, maxQueryLength) |
| 83 | } |
| 84 | |
| 85 | expr, err := cortexparser.ParseExpr(r.GetQuery()) |
| 86 | if err != nil { |
| 87 | return nil, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) |
| 88 | } |
| 89 | |
| 90 | // Enforce query length across all selectors in the query. |
| 91 | length := promql.FindNonOverlapQueryLength(expr, 0, 0, l.lookbackDelta) |
| 92 | if length > maxQueryLength { |
| 93 | return nil, httpgrpc.Errorf(http.StatusBadRequest, validation.ErrQueryTooLong, length, maxQueryLength) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return l.next.Do(ctx, r) |
nothing calls this directly
no test coverage detected