From 1eb86d4b5f23f4ea6df666fe2ae92c226a25db2b Mon Sep 17 00:00:00 2001 From: Owen Diehl Date: Wed, 4 Nov 2020 07:47:01 -0500 Subject: [PATCH] Only applies entry limits to non-SampleExprs. (#2850) * only applies entry limits to non-SampleExprs. updates docs * fixes some test case formatting --- docs/sources/configuration/_index.md | 2 +- pkg/logentry/stages/drop_test.go | 6 +++++- pkg/logentry/stages/metrics_test.go | 2 +- pkg/querier/http.go | 18 ++++++++++++++---- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/docs/sources/configuration/_index.md b/docs/sources/configuration/_index.md index 10c175d79e782..2d297b626f2ae 100644 --- a/docs/sources/configuration/_index.md +++ b/docs/sources/configuration/_index.md @@ -1598,7 +1598,7 @@ logs in Loki. # CLI flag: -distributor.max-line-size [max_line_size: | default = none ] -# Maximum number of log entries that will be returned for a query. 0 to disable. +# Maximum number of log entries that will be returned for a query. # CLI flag: -validation.max-entries-limit [max_entries_limit_per_query: | default = 5000 ] diff --git a/pkg/logentry/stages/drop_test.go b/pkg/logentry/stages/drop_test.go index 3d8cabf65f6b0..36c048a94d42a 100644 --- a/pkg/logentry/stages/drop_test.go +++ b/pkg/logentry/stages/drop_test.go @@ -342,7 +342,11 @@ func Test_validateDropConfig(t *testing.T) { config: &DropConfig{ OlderThan: &dropInvalidDur, }, - wantErr: fmt.Errorf(ErrDropStageInvalidDuration, dropInvalidDur, "time: unknown unit \"y\" in duration \"10y\""), + wantErr: fmt.Errorf( + ErrDropStageInvalidDuration, + dropInvalidDur, + "time: unknown unit y in duration 10y", + ), }, { name: "Invalid Config", diff --git a/pkg/logentry/stages/metrics_test.go b/pkg/logentry/stages/metrics_test.go index bf98b917616f8..33319e37272c1 100644 --- a/pkg/logentry/stages/metrics_test.go +++ b/pkg/logentry/stages/metrics_test.go @@ -222,7 +222,7 @@ func Test(t *testing.T) { IdleDuration: &metricTestInvalidIdle, }, }, - errors.Errorf(ErrInvalidIdleDur, "time: unknown unit \"f\" in duration \"10f\""), + errors.Errorf(ErrInvalidIdleDur, "time: unknown unit f in duration 10f"), }, "valid": { MetricsConfig{ diff --git a/pkg/querier/http.go b/pkg/querier/http.go index d9da166c26086..99e417c8ab62d 100644 --- a/pkg/querier/http.go +++ b/pkg/querier/http.go @@ -42,7 +42,7 @@ func (q *Querier) RangeQueryHandler(w http.ResponseWriter, r *http.Request) { return } - if err := q.validateEntriesLimits(ctx, request.Limit); err != nil { + if err := q.validateEntriesLimits(ctx, request.Query, request.Limit); err != nil { serverutil.WriteError(err, w) return } @@ -82,7 +82,7 @@ func (q *Querier) InstantQueryHandler(w http.ResponseWriter, r *http.Request) { return } - if err := q.validateEntriesLimits(ctx, request.Limit); err != nil { + if err := q.validateEntriesLimits(ctx, request.Query, request.Limit); err != nil { serverutil.WriteError(err, w) return } @@ -139,7 +139,7 @@ func (q *Querier) LogQueryHandler(w http.ResponseWriter, r *http.Request) { return } - if err := q.validateEntriesLimits(ctx, request.Limit); err != nil { + if err := q.validateEntriesLimits(ctx, request.Query, request.Limit); err != nil { serverutil.WriteError(err, w) return } @@ -345,12 +345,22 @@ func parseRegexQuery(httpRequest *http.Request) (string, error) { return query, nil } -func (q *Querier) validateEntriesLimits(ctx context.Context, limit uint32) error { +func (q *Querier) validateEntriesLimits(ctx context.Context, query string, limit uint32) error { userID, err := user.ExtractOrgID(ctx) if err != nil { return httpgrpc.Errorf(http.StatusBadRequest, err.Error()) } + expr, err := logql.ParseExpr(query) + if err != nil { + return err + } + + // entry limit does not apply to metric queries. + if _, ok := expr.(logql.SampleExpr); ok { + return nil + } + maxEntriesLimit := q.limits.MaxEntriesLimitPerQuery(userID) if int(limit) > maxEntriesLimit && maxEntriesLimit != 0 { return httpgrpc.Errorf(http.StatusBadRequest,