Skip to content

Commit

Permalink
Only applies entry limits to non-SampleExprs. (#2850)
Browse files Browse the repository at this point in the history
* only applies entry limits to non-SampleExprs. updates docs

* fixes some test case formatting
  • Loading branch information
owen-d authored Nov 4, 2020
1 parent 6bacfa1 commit 1eb86d4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/sources/configuration/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,7 @@ logs in Loki.
# CLI flag: -distributor.max-line-size
[max_line_size: <string> | 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: <int> | default = 5000 ]
Expand Down
6 changes: 5 additions & 1 deletion pkg/logentry/stages/drop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion pkg/logentry/stages/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
18 changes: 14 additions & 4 deletions pkg/querier/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 1eb86d4

Please sign in to comment.