Skip to content

Commit

Permalink
add max query length limit for LabelNames and LabelValues
Browse files Browse the repository at this point in the history
Signed-off-by: Ahmed Hassan <afayekhassan@gmail.com>
  • Loading branch information
afhassan committed Sep 25, 2024
1 parent 820c3bf commit 2d65a02
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions pkg/querier/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ func (q querier) Select(ctx context.Context, sortSeries bool, sp *storage.Select

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

startTime := model.Time(mint)
endTime := model.Time(maxt)

if maxQueryLength := q.limits.MaxQueryLength(userID); maxQueryLength > 0 && endTime.Sub(startTime) > maxQueryLength {
limitErr := validation.LimitError(fmt.Sprintf(validation.ErrQueryTooLong, endTime.Sub(startTime), maxQueryLength))
return nil, nil, limitErr
}

if len(queriers) == 1 {
return queriers[0].LabelValues(ctx, name, hints, matchers...)
}
Expand Down Expand Up @@ -472,7 +480,7 @@ func (q querier) LabelValues(ctx context.Context, name string, hints *storage.La
}

func (q querier) LabelNames(ctx context.Context, hints *storage.LabelHints, matchers ...*labels.Matcher) ([]string, annotations.Annotations, error) {
ctx, stats, _, _, _, _, queriers, err := q.setupFromCtx(ctx)
ctx, stats, userID, mint, maxt, _, queriers, err := q.setupFromCtx(ctx)
if err == errEmptyTimeRange {
return nil, nil, nil
} else if err != nil {
Expand All @@ -483,6 +491,14 @@ func (q querier) LabelNames(ctx context.Context, hints *storage.LabelHints, matc
stats.AddQueryStorageWallTime(time.Since(startT))
}()

startTime := model.Time(mint)
endTime := model.Time(maxt)

if maxQueryLength := q.limits.MaxQueryLength(userID); maxQueryLength > 0 && endTime.Sub(startTime) > maxQueryLength {
limitErr := validation.LimitError(fmt.Sprintf(validation.ErrQueryTooLong, endTime.Sub(startTime), maxQueryLength))
return nil, nil, limitErr
}

if len(queriers) == 1 {
return queriers[0].LabelNames(ctx, hints, matchers...)
}
Expand Down

0 comments on commit 2d65a02

Please sign in to comment.