Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Querier] Add day range limit for LabelNames and LabelValues #6233

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions pkg/querier/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,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 @@ -442,6 +442,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 @@ -481,7 +489,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 @@ -492,6 +500,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
73 changes: 73 additions & 0 deletions pkg/querier/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,79 @@ func TestQuerier_ValidateQueryTimeRange_MaxQueryLength_Series(t *testing.T) {
require.True(t, strings.Contains(ss.Err().Error(), "the query time range exceeds the limit (query length: 721h0m0s, limit: 720h0m0s)"))
}

func TestQuerier_ValidateQueryTimeRange_MaxQueryLength_Labels(t *testing.T) {
t.Parallel()
const maxQueryLength = 30 * 24 * time.Hour
tests := map[string]struct {
startTime time.Time
endTime time.Time
expected error
ignoreMaxQueryLength bool
}{
"time range shorter than maxQueryLength": {
startTime: time.Now().Add(-maxQueryLength).Add(time.Hour),
endTime: time.Now(),
expected: nil,
ignoreMaxQueryLength: false,
},
"time range longer than maxQueryLength": {
startTime: time.Now().Add(-maxQueryLength).Add(-time.Hour),
endTime: time.Now(),
expected: validation.LimitError("expanding series: the query time range exceeds the limit (query length: 721h0m0s, limit: 720h0m0s)"),
ignoreMaxQueryLength: false,
},
"time range longer than maxQueryLength and ignoreMaxQueryLength is true": {
startTime: time.Now().Add(-maxQueryLength).Add(-time.Hour),
endTime: time.Now(),
expected: validation.LimitError("expanding series: the query time range exceeds the limit (query length: 721h0m0s, limit: 720h0m0s)"),
ignoreMaxQueryLength: true,
},
}

for testName, testData := range tests {
t.Run(testName, func(t *testing.T) {
var cfg Config
flagext.DefaultValues(&cfg)
cfg.ActiveQueryTrackerDir = ""
cfg.IgnoreMaxQueryLength = testData.ignoreMaxQueryLength

limits := DefaultLimitsConfig()
limits.MaxQueryLength = model.Duration(maxQueryLength)
overrides, err := validation.NewOverrides(limits, nil)
require.NoError(t, err)

chunkStore := &emptyChunkStore{}
distributor := &emptyDistributor{}

queryables := []QueryableWithFilter{UseAlwaysQueryable(NewMockStoreQueryable(chunkStore))}
queryable, _, _ := New(cfg, overrides, distributor, queryables, nil, log.NewNopLogger())

ctx := user.InjectOrgID(context.Background(), "test")

q, err := queryable.Querier(util.TimeToMillis(testData.startTime), util.TimeToMillis(testData.endTime))
require.NoError(t, err)

_, _, err = q.LabelNames(ctx, &storage.LabelHints{Limit: 0})

if testData.expected != nil {
require.NotNil(t, err)
assert.True(t, strings.Contains(testData.expected.Error(), err.Error()))
} else {
assert.Nil(t, err)
}

_, _, err = q.LabelValues(ctx, labels.MetricName, &storage.LabelHints{Limit: 0})

if testData.expected != nil {
require.NotNil(t, err)
assert.True(t, strings.Contains(testData.expected.Error(), err.Error()))
} else {
assert.Nil(t, err)
}
})
}
}

func TestQuerier_ValidateQueryTimeRange_MaxQueryLookback(t *testing.T) {
t.Parallel()
const (
Expand Down
Loading