Skip to content

Commit 5d2297a

Browse files
committed
add max query length limit for LabelNames and LabelValues
Signed-off-by: Ahmed Hassan <afayekhassan@gmail.com>
1 parent 409f065 commit 5d2297a

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

pkg/querier/querier.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ func (q querier) Select(ctx context.Context, sortSeries bool, sp *storage.Select
431431

432432
// LabelValues implements storage.Querier.
433433
func (q querier) LabelValues(ctx context.Context, name string, hints *storage.LabelHints, matchers ...*labels.Matcher) ([]string, annotations.Annotations, error) {
434-
ctx, stats, _, _, _, _, queriers, err := q.setupFromCtx(ctx)
434+
ctx, stats, userID, mint, maxt, _, queriers, err := q.setupFromCtx(ctx)
435435
if err == errEmptyTimeRange {
436436
return nil, nil, nil
437437
} else if err != nil {
@@ -442,6 +442,14 @@ func (q querier) LabelValues(ctx context.Context, name string, hints *storage.La
442442
stats.AddQueryStorageWallTime(time.Since(startT))
443443
}()
444444

445+
startTime := model.Time(mint)
446+
endTime := model.Time(maxt)
447+
448+
if maxQueryLength := q.limits.MaxQueryLength(userID); maxQueryLength > 0 && endTime.Sub(startTime) > maxQueryLength {
449+
limitErr := validation.LimitError(fmt.Sprintf(validation.ErrQueryTooLong, endTime.Sub(startTime), maxQueryLength))
450+
return nil, nil, limitErr
451+
}
452+
445453
if len(queriers) == 1 {
446454
return queriers[0].LabelValues(ctx, name, hints, matchers...)
447455
}
@@ -481,7 +489,7 @@ func (q querier) LabelValues(ctx context.Context, name string, hints *storage.La
481489
}
482490

483491
func (q querier) LabelNames(ctx context.Context, hints *storage.LabelHints, matchers ...*labels.Matcher) ([]string, annotations.Annotations, error) {
484-
ctx, stats, _, _, _, _, queriers, err := q.setupFromCtx(ctx)
492+
ctx, stats, userID, mint, maxt, _, queriers, err := q.setupFromCtx(ctx)
485493
if err == errEmptyTimeRange {
486494
return nil, nil, nil
487495
} else if err != nil {
@@ -492,6 +500,14 @@ func (q querier) LabelNames(ctx context.Context, hints *storage.LabelHints, matc
492500
stats.AddQueryStorageWallTime(time.Since(startT))
493501
}()
494502

503+
startTime := model.Time(mint)
504+
endTime := model.Time(maxt)
505+
506+
if maxQueryLength := q.limits.MaxQueryLength(userID); maxQueryLength > 0 && endTime.Sub(startTime) > maxQueryLength {
507+
limitErr := validation.LimitError(fmt.Sprintf(validation.ErrQueryTooLong, endTime.Sub(startTime), maxQueryLength))
508+
return nil, nil, limitErr
509+
}
510+
495511
if len(queriers) == 1 {
496512
return queriers[0].LabelNames(ctx, hints, matchers...)
497513
}

0 commit comments

Comments
 (0)