tsdb: apply LabelNames limit from LabelHints in blockBaseQuerier

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
This commit is contained in:
Julien Pivotto 2026-04-23 10:58:29 +02:00
parent be5db19c73
commit a5b5a3329c
2 changed files with 45 additions and 2 deletions

View File

@ -81,9 +81,17 @@ func (q *blockBaseQuerier) LabelValues(ctx context.Context, name string, hints *
return res, nil, err
}
func (q *blockBaseQuerier) LabelNames(ctx context.Context, _ *storage.LabelHints, matchers ...*labels.Matcher) ([]string, annotations.Annotations, error) {
func (q *blockBaseQuerier) LabelNames(ctx context.Context, hints *storage.LabelHints, matchers ...*labels.Matcher) ([]string, annotations.Annotations, error) {
res, err := q.index.LabelNames(ctx, matchers...)
return res, nil, err
if err != nil {
return nil, nil, err
}
if hints != nil && hints.Limit > 0 && len(res) > hints.Limit {
res = res[:hints.Limit]
}
return res, nil, nil
}
func (q *blockBaseQuerier) Close() error {

View File

@ -418,6 +418,41 @@ func TestBlockQuerier(t *testing.T) {
}
}
func TestBlockBaseQuerier_LabelNamesLimit(t *testing.T) {
ix := newMockIndex()
ls := labels.FromStrings("aaa", "1", "bbb", "2", "ccc", "3")
require.NoError(t, ix.AddSeries(1, ls))
ls.Range(func(lbl labels.Label) {
require.NoError(t, ix.WritePostings(lbl.Name, lbl.Value, index.NewListPostings([]storage.SeriesRef{1})))
})
q := &blockBaseQuerier{index: ix, chunks: mockChunkReader(nil), tombstones: tombstones.NewMemTombstones()}
t.Run("no limit", func(t *testing.T) {
names, _, err := q.LabelNames(context.Background(), nil)
require.NoError(t, err)
require.Equal(t, []string{"aaa", "bbb", "ccc"}, names)
})
t.Run("limit applied", func(t *testing.T) {
names, _, err := q.LabelNames(context.Background(), &storage.LabelHints{Limit: 2})
require.NoError(t, err)
require.Equal(t, []string{"aaa", "bbb"}, names)
})
t.Run("limit larger than result", func(t *testing.T) {
names, _, err := q.LabelNames(context.Background(), &storage.LabelHints{Limit: 10})
require.NoError(t, err)
require.Equal(t, []string{"aaa", "bbb", "ccc"}, names)
})
t.Run("zero limit means no limit", func(t *testing.T) {
names, _, err := q.LabelNames(context.Background(), &storage.LabelHints{Limit: 0})
require.NoError(t, err)
require.Equal(t, []string{"aaa", "bbb", "ccc"}, names)
})
}
func TestBlockQuerier_AgainstHeadWithOpenChunks(t *testing.T) {
for _, c := range []blockQuerierTestCase{
{