Add context argument to LabelQuerier.LabelNames (#12666)

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Arve Knudsen 2023-09-14 10:39:51 +02:00 committed by GitHub
parent 8f52477e20
commit a964349e97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 76 additions and 62 deletions

View file

@ -433,7 +433,7 @@ func analyzeBlock(ctx context.Context, path, blockID string, limit int, runExten
} }
defer ir.Close() defer ir.Close()
allLabelNames, err := ir.LabelNames() allLabelNames, err := ir.LabelNames(ctx)
if err != nil { if err != nil {
return err return err
} }

View file

@ -202,7 +202,7 @@ func (*errQuerier) LabelValues(string, ...*labels.Matcher) ([]string, storage.Wa
return nil, nil, nil return nil, nil, nil
} }
func (*errQuerier) LabelNames(...*labels.Matcher) ([]string, storage.Warnings, error) { func (*errQuerier) LabelNames(context.Context, ...*labels.Matcher) ([]string, storage.Warnings, error) {
return nil, nil, nil return nil, nil, nil
} }
func (*errQuerier) Close() error { return nil } func (*errQuerier) Close() error { return nil }

View file

@ -237,7 +237,7 @@ func (errQuerier) LabelValues(string, ...*labels.Matcher) ([]string, storage.War
return nil, nil, errors.New("label values error") return nil, nil, errors.New("label values error")
} }
func (errQuerier) LabelNames(...*labels.Matcher) ([]string, storage.Warnings, error) { func (errQuerier) LabelNames(context.Context, ...*labels.Matcher) ([]string, storage.Warnings, error) {
return nil, nil, errors.New("label names error") return nil, nil, errors.New("label names error")
} }

View file

@ -122,7 +122,7 @@ func (q *MockQuerier) LabelValues(string, ...*labels.Matcher) ([]string, Warning
return nil, nil, nil return nil, nil, nil
} }
func (q *MockQuerier) LabelNames(...*labels.Matcher) ([]string, Warnings, error) { func (q *MockQuerier) LabelNames(context.Context, ...*labels.Matcher) ([]string, Warnings, error) {
return nil, nil, nil return nil, nil, nil
} }
@ -162,7 +162,7 @@ type LabelQuerier interface {
// LabelNames returns all the unique label names present in the block in sorted order. // LabelNames returns all the unique label names present in the block in sorted order.
// If matchers are specified the returned result set is reduced // If matchers are specified the returned result set is reduced
// to label names of metrics matching the matchers. // to label names of metrics matching the matchers.
LabelNames(matchers ...*labels.Matcher) ([]string, Warnings, error) LabelNames(ctx context.Context, matchers ...*labels.Matcher) ([]string, Warnings, error)
// Close releases the resources of the Querier. // Close releases the resources of the Querier.
Close() error Close() error

View file

@ -218,13 +218,13 @@ func mergeStrings(a, b []string) []string {
} }
// LabelNames returns all the unique label names present in all queriers in sorted order. // LabelNames returns all the unique label names present in all queriers in sorted order.
func (q *mergeGenericQuerier) LabelNames(matchers ...*labels.Matcher) ([]string, Warnings, error) { func (q *mergeGenericQuerier) LabelNames(ctx context.Context, matchers ...*labels.Matcher) ([]string, Warnings, error) {
var ( var (
labelNamesMap = make(map[string]struct{}) labelNamesMap = make(map[string]struct{})
warnings Warnings warnings Warnings
) )
for _, querier := range q.queriers { for _, querier := range q.queriers {
names, wrn, err := querier.LabelNames(matchers...) names, wrn, err := querier.LabelNames(ctx, matchers...)
if wrn != nil { if wrn != nil {
// TODO(bwplotka): We could potentially wrap warnings. // TODO(bwplotka): We could potentially wrap warnings.
warnings = append(warnings, wrn...) warnings = append(warnings, wrn...)

View file

@ -1000,7 +1000,7 @@ func (m *mockGenericQuerier) LabelValues(name string, matchers ...*labels.Matche
return m.resp, m.warnings, m.err return m.resp, m.warnings, m.err
} }
func (m *mockGenericQuerier) LabelNames(...*labels.Matcher) ([]string, Warnings, error) { func (m *mockGenericQuerier) LabelNames(context.Context, ...*labels.Matcher) ([]string, Warnings, error) {
m.mtx.Lock() m.mtx.Lock()
m.labelNamesCalls++ m.labelNamesCalls++
m.mtx.Unlock() m.mtx.Unlock()
@ -1059,6 +1059,7 @@ func TestMergeGenericQuerierWithSecondaries_ErrorHandling(t *testing.T) {
var ( var (
errStorage = errors.New("storage error") errStorage = errors.New("storage error")
warnStorage = errors.New("storage warning") warnStorage = errors.New("storage warning")
ctx = context.Background()
) )
for _, tcase := range []struct { for _, tcase := range []struct {
name string name string
@ -1199,7 +1200,7 @@ func TestMergeGenericQuerierWithSecondaries_ErrorHandling(t *testing.T) {
} }
}) })
t.Run("LabelNames", func(t *testing.T) { t.Run("LabelNames", func(t *testing.T) {
res, w, err := q.LabelNames() res, w, err := q.LabelNames(ctx)
require.Equal(t, tcase.expectedWarnings[1], w) require.Equal(t, tcase.expectedWarnings[1], w)
require.True(t, errors.Is(err, tcase.expectedErrs[1]), "expected error doesn't match") require.True(t, errors.Is(err, tcase.expectedErrs[1]), "expected error doesn't match")
require.Equal(t, tcase.expectedLabels, res) require.Equal(t, tcase.expectedLabels, res)

View file

@ -34,7 +34,7 @@ func (noopQuerier) LabelValues(string, ...*labels.Matcher) ([]string, Warnings,
return nil, nil, nil return nil, nil, nil
} }
func (noopQuerier) LabelNames(...*labels.Matcher) ([]string, Warnings, error) { func (noopQuerier) LabelNames(context.Context, ...*labels.Matcher) ([]string, Warnings, error) {
return nil, nil, nil return nil, nil, nil
} }
@ -57,7 +57,7 @@ func (noopChunkQuerier) LabelValues(string, ...*labels.Matcher) ([]string, Warni
return nil, nil, nil return nil, nil, nil
} }
func (noopChunkQuerier) LabelNames(...*labels.Matcher) ([]string, Warnings, error) { func (noopChunkQuerier) LabelNames(context.Context, ...*labels.Matcher) ([]string, Warnings, error) {
return nil, nil, nil return nil, nil, nil
} }

View file

@ -215,7 +215,7 @@ func (q *querier) LabelValues(string, ...*labels.Matcher) ([]string, storage.War
} }
// LabelNames implements storage.Querier and is a noop. // LabelNames implements storage.Querier and is a noop.
func (q *querier) LabelNames(...*labels.Matcher) ([]string, storage.Warnings, error) { func (q *querier) LabelNames(context.Context, ...*labels.Matcher) ([]string, storage.Warnings, error) {
// TODO: Implement: https://github.com/prometheus/prometheus/issues/3351 // TODO: Implement: https://github.com/prometheus/prometheus/issues/3351
return nil, nil, errors.New("not implemented") return nil, nil, errors.New("not implemented")
} }

View file

@ -56,8 +56,8 @@ func (s *secondaryQuerier) LabelValues(name string, matchers ...*labels.Matcher)
return vals, w, nil return vals, w, nil
} }
func (s *secondaryQuerier) LabelNames(matchers ...*labels.Matcher) ([]string, Warnings, error) { func (s *secondaryQuerier) LabelNames(ctx context.Context, matchers ...*labels.Matcher) ([]string, Warnings, error) {
names, w, err := s.genericQuerier.LabelNames(matchers...) names, w, err := s.genericQuerier.LabelNames(ctx, matchers...)
if err != nil { if err != nil {
return nil, append([]error{err}, w...), nil return nil, append([]error{err}, w...), nil
} }

View file

@ -86,7 +86,7 @@ type IndexReader interface {
Series(ref storage.SeriesRef, builder *labels.ScratchBuilder, chks *[]chunks.Meta) error Series(ref storage.SeriesRef, builder *labels.ScratchBuilder, chks *[]chunks.Meta) error
// LabelNames returns all the unique label names present in the index in sorted order. // LabelNames returns all the unique label names present in the index in sorted order.
LabelNames(matchers ...*labels.Matcher) ([]string, error) LabelNames(ctx context.Context, matchers ...*labels.Matcher) ([]string, error)
// LabelValueFor returns label value for the given label name in the series referred to by ID. // LabelValueFor returns label value for the given label name in the series referred to by ID.
// If the series couldn't be found or the series doesn't have the requested label a // If the series couldn't be found or the series doesn't have the requested label a
@ -95,7 +95,7 @@ type IndexReader interface {
// LabelNamesFor returns all the label names for the series referred to by IDs. // LabelNamesFor returns all the label names for the series referred to by IDs.
// The names returned are sorted. // The names returned are sorted.
LabelNamesFor(ids ...storage.SeriesRef) ([]string, error) LabelNamesFor(ctx context.Context, ids ...storage.SeriesRef) ([]string, error)
// Close releases the underlying resources of the reader. // Close releases the underlying resources of the reader.
Close() error Close() error
@ -480,12 +480,12 @@ func (r blockIndexReader) LabelValues(name string, matchers ...*labels.Matcher)
return labelValuesWithMatchers(r.ir, name, matchers...) return labelValuesWithMatchers(r.ir, name, matchers...)
} }
func (r blockIndexReader) LabelNames(matchers ...*labels.Matcher) ([]string, error) { func (r blockIndexReader) LabelNames(ctx context.Context, matchers ...*labels.Matcher) ([]string, error) {
if len(matchers) == 0 { if len(matchers) == 0 {
return r.b.LabelNames() return r.b.LabelNames(ctx)
} }
return labelNamesWithMatchers(r.ir, matchers...) return labelNamesWithMatchers(ctx, r.ir, matchers...)
} }
func (r blockIndexReader) Postings(ctx context.Context, name string, values ...string) (index.Postings, error) { func (r blockIndexReader) Postings(ctx context.Context, name string, values ...string) (index.Postings, error) {
@ -519,8 +519,8 @@ func (r blockIndexReader) LabelValueFor(id storage.SeriesRef, label string) (str
// LabelNamesFor returns all the label names for the series referred to by IDs. // LabelNamesFor returns all the label names for the series referred to by IDs.
// The names returned are sorted. // The names returned are sorted.
func (r blockIndexReader) LabelNamesFor(ids ...storage.SeriesRef) ([]string, error) { func (r blockIndexReader) LabelNamesFor(ctx context.Context, ids ...storage.SeriesRef) ([]string, error) {
return r.ir.LabelNamesFor(ids...) return r.ir.LabelNamesFor(ctx, ids...)
} }
type blockTombstoneReader struct { type blockTombstoneReader struct {
@ -686,8 +686,8 @@ func (pb *Block) OverlapsClosedInterval(mint, maxt int64) bool {
} }
// LabelNames returns all the unique label names present in the Block in sorted order. // LabelNames returns all the unique label names present in the Block in sorted order.
func (pb *Block) LabelNames() ([]string, error) { func (pb *Block) LabelNames(ctx context.Context) ([]string, error) {
return pb.indexr.LabelNames() return pb.indexr.LabelNames(ctx)
} }
func clampInterval(a, b, mint, maxt int64) (int64, int64) { func clampInterval(a, b, mint, maxt int64) (int64, int64) {

View file

@ -406,6 +406,7 @@ func BenchmarkLabelValuesWithMatchers(b *testing.B) {
func TestLabelNamesWithMatchers(t *testing.T) { func TestLabelNamesWithMatchers(t *testing.T) {
tmpdir := t.TempDir() tmpdir := t.TempDir()
ctx := context.Background()
var seriesEntries []storage.Series var seriesEntries []storage.Series
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
@ -471,7 +472,7 @@ func TestLabelNamesWithMatchers(t *testing.T) {
for _, tt := range testCases { for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
actualNames, err := indexReader.LabelNames(tt.matchers...) actualNames, err := indexReader.LabelNames(ctx, tt.matchers...)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tt.expectedNames, actualNames) require.Equal(t, tt.expectedNames, actualNames)
}) })

View file

@ -2142,6 +2142,7 @@ func TestNoEmptyBlocks(t *testing.T) {
} }
func TestDB_LabelNames(t *testing.T) { func TestDB_LabelNames(t *testing.T) {
ctx := context.Background()
tests := []struct { tests := []struct {
// Add 'sampleLabels1' -> Test Head -> Compact -> Test Disk -> // Add 'sampleLabels1' -> Test Head -> Compact -> Test Disk ->
// -> Add 'sampleLabels2' -> Test Head+Disk // -> Add 'sampleLabels2' -> Test Head+Disk
@ -2185,7 +2186,6 @@ func TestDB_LabelNames(t *testing.T) {
// Appends samples into the database. // Appends samples into the database.
appendSamples := func(db *DB, mint, maxt int64, sampleLabels [][2]string) { appendSamples := func(db *DB, mint, maxt int64, sampleLabels [][2]string) {
t.Helper() t.Helper()
ctx := context.Background()
app := db.Appender(ctx) app := db.Appender(ctx)
for i := mint; i <= maxt; i++ { for i := mint; i <= maxt; i++ {
for _, tuple := range sampleLabels { for _, tuple := range sampleLabels {
@ -2209,7 +2209,7 @@ func TestDB_LabelNames(t *testing.T) {
// Testing head. // Testing head.
headIndexr, err := db.head.Index() headIndexr, err := db.head.Index()
require.NoError(t, err) require.NoError(t, err)
labelNames, err := headIndexr.LabelNames() labelNames, err := headIndexr.LabelNames(ctx)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tst.exp1, labelNames) require.Equal(t, tst.exp1, labelNames)
require.NoError(t, headIndexr.Close()) require.NoError(t, headIndexr.Close())
@ -2222,21 +2222,21 @@ func TestDB_LabelNames(t *testing.T) {
for _, b := range db.Blocks() { for _, b := range db.Blocks() {
blockIndexr, err := b.Index() blockIndexr, err := b.Index()
require.NoError(t, err) require.NoError(t, err)
labelNames, err = blockIndexr.LabelNames() labelNames, err = blockIndexr.LabelNames(ctx)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tst.exp1, labelNames) require.Equal(t, tst.exp1, labelNames)
require.NoError(t, blockIndexr.Close()) require.NoError(t, blockIndexr.Close())
} }
// Adding more samples to head with new label names // Adding more samples to head with new label names
// so that we can test (head+disk).LabelNames() (the union). // so that we can test (head+disk).LabelNames(ctx) (the union).
appendSamples(db, 5, 9, tst.sampleLabels2) appendSamples(db, 5, 9, tst.sampleLabels2)
// Testing DB (union). // Testing DB (union).
q, err := db.Querier(math.MinInt64, math.MaxInt64) q, err := db.Querier(math.MinInt64, math.MaxInt64)
require.NoError(t, err) require.NoError(t, err)
var ws storage.Warnings var ws storage.Warnings
labelNames, ws, err = q.LabelNames() labelNames, ws, err = q.LabelNames(ctx)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 0, len(ws)) require.Equal(t, 0, len(ws))
require.NoError(t, q.Close()) require.NoError(t, q.Close())

View file

@ -88,7 +88,7 @@ func (h *headIndexReader) LabelValues(name string, matchers ...*labels.Matcher)
// LabelNames returns all the unique label names present in the head // LabelNames returns all the unique label names present in the head
// that are within the time range mint to maxt. // that are within the time range mint to maxt.
func (h *headIndexReader) LabelNames(matchers ...*labels.Matcher) ([]string, error) { func (h *headIndexReader) LabelNames(ctx context.Context, matchers ...*labels.Matcher) ([]string, error) {
if h.maxt < h.head.MinTime() || h.mint > h.head.MaxTime() { if h.maxt < h.head.MinTime() || h.mint > h.head.MaxTime() {
return []string{}, nil return []string{}, nil
} }
@ -99,7 +99,7 @@ func (h *headIndexReader) LabelNames(matchers ...*labels.Matcher) ([]string, err
return labelNames, nil return labelNames, nil
} }
return labelNamesWithMatchers(h, matchers...) return labelNamesWithMatchers(ctx, h, matchers...)
} }
// Postings returns the postings list iterator for the label pairs. // Postings returns the postings list iterator for the label pairs.
@ -232,9 +232,12 @@ func (h *headIndexReader) LabelValueFor(id storage.SeriesRef, label string) (str
// LabelNamesFor returns all the label names for the series referred to by IDs. // LabelNamesFor returns all the label names for the series referred to by IDs.
// The names returned are sorted. // The names returned are sorted.
func (h *headIndexReader) LabelNamesFor(ids ...storage.SeriesRef) ([]string, error) { func (h *headIndexReader) LabelNamesFor(ctx context.Context, ids ...storage.SeriesRef) ([]string, error) {
namesMap := make(map[string]struct{}) namesMap := make(map[string]struct{})
for _, id := range ids { for _, id := range ids {
if ctx.Err() != nil {
return nil, ctx.Err()
}
memSeries := h.head.series.getByID(chunks.HeadSeriesRef(id)) memSeries := h.head.series.getByID(chunks.HeadSeriesRef(id))
if memSeries == nil { if memSeries == nil {
return nil, storage.ErrNotFound return nil, storage.ErrNotFound

View file

@ -2637,6 +2637,7 @@ func TestHeadLabelNamesValuesWithMinMaxRange(t *testing.T) {
} }
expectedLabelNames = []string{"a", "b", "c"} expectedLabelNames = []string{"a", "b", "c"}
expectedLabelValues = []string{"d", "e", "f"} expectedLabelValues = []string{"d", "e", "f"}
ctx = context.Background()
) )
app := head.Appender(context.Background()) app := head.Appender(context.Background())
@ -2664,7 +2665,7 @@ func TestHeadLabelNamesValuesWithMinMaxRange(t *testing.T) {
for _, tt := range testCases { for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
headIdxReader := head.indexRange(tt.mint, tt.maxt) headIdxReader := head.indexRange(tt.mint, tt.maxt)
actualLabelNames, err := headIdxReader.LabelNames() actualLabelNames, err := headIdxReader.LabelNames(ctx)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tt.expectedNames, actualLabelNames) require.Equal(t, tt.expectedNames, actualLabelNames)
if len(tt.expectedValues) > 0 { if len(tt.expectedValues) > 0 {
@ -2798,7 +2799,7 @@ func TestHeadLabelNamesWithMatchers(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
headIdxReader := head.indexRange(0, 200) headIdxReader := head.indexRange(0, 200)
actualNames, err := headIdxReader.LabelNames(tt.matchers...) actualNames, err := headIdxReader.LabelNames(context.Background(), tt.matchers...)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tt.expectedNames, actualNames) require.Equal(t, tt.expectedNames, actualNames)
}) })

View file

@ -923,7 +923,7 @@ func (w *Writer) writePostingsToTmpFiles() error {
// Symbol numbers are in order, so the strings will also be in order. // Symbol numbers are in order, so the strings will also be in order.
slices.Sort(values) slices.Sort(values)
for _, v := range values { for _, v := range values {
value, err := w.symbols.Lookup(v) value, err := w.symbols.Lookup(w.ctx, v)
if err != nil { if err != nil {
return err return err
} }
@ -1295,7 +1295,7 @@ func NewSymbols(bs ByteSlice, version, off int) (*Symbols, error) {
return s, nil return s, nil
} }
func (s Symbols) Lookup(o uint32) (string, error) { func (s Symbols) Lookup(ctx context.Context, o uint32) (string, error) {
d := encoding.Decbuf{ d := encoding.Decbuf{
B: s.bs.Range(0, s.bs.Len()), B: s.bs.Range(0, s.bs.Len()),
} }
@ -1307,6 +1307,9 @@ func (s Symbols) Lookup(o uint32) (string, error) {
d.Skip(s.offsets[int(o/symbolFactor)]) d.Skip(s.offsets[int(o/symbolFactor)])
// Walk until we find the one we want. // Walk until we find the one we want.
for i := o - (o / symbolFactor * symbolFactor); i > 0; i-- { for i := o - (o / symbolFactor * symbolFactor); i > 0; i-- {
if ctx.Err() != nil {
return "", ctx.Err()
}
d.UvarintBytes() d.UvarintBytes()
} }
} else { } else {
@ -1434,11 +1437,11 @@ func (r *Reader) Close() error {
return r.c.Close() return r.c.Close()
} }
func (r *Reader) lookupSymbol(o uint32) (string, error) { func (r *Reader) lookupSymbol(ctx context.Context, o uint32) (string, error) {
if s, ok := r.nameSymbols[o]; ok { if s, ok := r.nameSymbols[o]; ok {
return s, nil return s, nil
} }
return r.symbols.Lookup(o) return r.symbols.Lookup(ctx, o)
} }
// Symbols returns an iterator over the symbols that exist within the index. // Symbols returns an iterator over the symbols that exist within the index.
@ -1523,10 +1526,14 @@ func (r *Reader) LabelValues(name string, matchers ...*labels.Matcher) ([]string
// LabelNamesFor returns all the label names for the series referred to by IDs. // LabelNamesFor returns all the label names for the series referred to by IDs.
// The names returned are sorted. // The names returned are sorted.
func (r *Reader) LabelNamesFor(ids ...storage.SeriesRef) ([]string, error) { func (r *Reader) LabelNamesFor(ctx context.Context, ids ...storage.SeriesRef) ([]string, error) {
// Gather offsetsMap the name offsetsMap in the symbol table first // Gather offsetsMap the name offsetsMap in the symbol table first
offsetsMap := make(map[uint32]struct{}) offsetsMap := make(map[uint32]struct{})
for _, id := range ids { for _, id := range ids {
if ctx.Err() != nil {
return nil, ctx.Err()
}
offset := id offset := id
// In version 2 series IDs are no longer exact references but series are 16-byte padded // In version 2 series IDs are no longer exact references but series are 16-byte padded
// and the ID is the multiple of 16 of the actual position. // and the ID is the multiple of 16 of the actual position.
@ -1552,7 +1559,7 @@ func (r *Reader) LabelNamesFor(ids ...storage.SeriesRef) ([]string, error) {
// Lookup the unique symbols. // Lookup the unique symbols.
names := make([]string, 0, len(offsetsMap)) names := make([]string, 0, len(offsetsMap))
for off := range offsetsMap { for off := range offsetsMap {
name, err := r.lookupSymbol(off) name, err := r.lookupSymbol(ctx, off)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "lookup symbol in LabelNamesFor") return nil, errors.Wrap(err, "lookup symbol in LabelNamesFor")
} }
@ -1722,7 +1729,7 @@ func (r *Reader) Size() int64 {
// LabelNames returns all the unique label names present in the index. // LabelNames returns all the unique label names present in the index.
// TODO(twilkie) implement support for matchers // TODO(twilkie) implement support for matchers
func (r *Reader) LabelNames(matchers ...*labels.Matcher) ([]string, error) { func (r *Reader) LabelNames(_ context.Context, matchers ...*labels.Matcher) ([]string, error) {
if len(matchers) > 0 { if len(matchers) > 0 {
return nil, errors.Errorf("matchers parameter is not implemented: %+v", matchers) return nil, errors.Errorf("matchers parameter is not implemented: %+v", matchers)
} }
@ -1766,7 +1773,7 @@ func (s stringListIter) Err() error { return nil }
// It currently does not contain decoding methods for all entry types but can be extended // It currently does not contain decoding methods for all entry types but can be extended
// by them if there's demand. // by them if there's demand.
type Decoder struct { type Decoder struct {
LookupSymbol func(uint32) (string, error) LookupSymbol func(context.Context, uint32) (string, error)
} }
// Postings returns a postings list for b and its number of elements. // Postings returns a postings list for b and its number of elements.
@ -1815,13 +1822,13 @@ func (dec *Decoder) LabelValueFor(b []byte, label string) (string, error) {
return "", errors.Wrap(d.Err(), "read series label offsets") return "", errors.Wrap(d.Err(), "read series label offsets")
} }
ln, err := dec.LookupSymbol(lno) ln, err := dec.LookupSymbol(context.TODO(), lno)
if err != nil { if err != nil {
return "", errors.Wrap(err, "lookup label name") return "", errors.Wrap(err, "lookup label name")
} }
if ln == label { if ln == label {
lv, err := dec.LookupSymbol(lvo) lv, err := dec.LookupSymbol(context.TODO(), lvo)
if err != nil { if err != nil {
return "", errors.Wrap(err, "lookup label value") return "", errors.Wrap(err, "lookup label value")
} }
@ -1851,11 +1858,11 @@ func (dec *Decoder) Series(b []byte, builder *labels.ScratchBuilder, chks *[]chu
return errors.Wrap(d.Err(), "read series label offsets") return errors.Wrap(d.Err(), "read series label offsets")
} }
ln, err := dec.LookupSymbol(lno) ln, err := dec.LookupSymbol(context.TODO(), lno)
if err != nil { if err != nil {
return errors.Wrap(err, "lookup label name") return errors.Wrap(err, "lookup label name")
} }
lv, err := dec.LookupSymbol(lvo) lv, err := dec.LookupSymbol(context.TODO(), lvo)
if err != nil { if err != nil {
return errors.Wrap(err, "lookup label value") return errors.Wrap(err, "lookup label value")
} }

View file

@ -229,7 +229,7 @@ func TestIndexRW_Postings(t *testing.T) {
d := encoding.NewDecbufAt(ir.b, int(off), castagnoliTable) d := encoding.NewDecbufAt(ir.b, int(off), castagnoliTable)
require.Equal(t, 1, d.Be32int(), "Unexpected number of label indices table names") require.Equal(t, 1, d.Be32int(), "Unexpected number of label indices table names")
for i := d.Be32(); i > 0 && d.Err() == nil; i-- { for i := d.Be32(); i > 0 && d.Err() == nil; i-- {
v, err := ir.lookupSymbol(d.Be32()) v, err := ir.lookupSymbol(ctx, d.Be32())
require.NoError(t, err) require.NoError(t, err)
labelIndices[lbl] = append(labelIndices[lbl], v) labelIndices[lbl] = append(labelIndices[lbl], v)
} }
@ -519,6 +519,7 @@ func TestNewFileReaderErrorNoOpenFiles(t *testing.T) {
} }
func TestSymbols(t *testing.T) { func TestSymbols(t *testing.T) {
ctx := context.Background()
buf := encoding.Encbuf{} buf := encoding.Encbuf{}
// Add prefix to the buffer to simulate symbols as part of larger buffer. // Add prefix to the buffer to simulate symbols as part of larger buffer.
@ -541,11 +542,11 @@ func TestSymbols(t *testing.T) {
require.Equal(t, 32, s.Size()) require.Equal(t, 32, s.Size())
for i := 99; i >= 0; i-- { for i := 99; i >= 0; i-- {
s, err := s.Lookup(uint32(i)) s, err := s.Lookup(ctx, uint32(i))
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, string(rune(i)), s) require.Equal(t, string(rune(i)), s)
} }
_, err = s.Lookup(100) _, err = s.Lookup(ctx, 100)
require.Error(t, err) require.Error(t, err)
for i := 99; i >= 0; i-- { for i := 99; i >= 0; i-- {

View file

@ -426,7 +426,7 @@ func (ir *OOOCompactionHeadIndexReader) PostingsForMatchers(concurrent bool, ms
return nil, errors.New("not implemented") return nil, errors.New("not implemented")
} }
func (ir *OOOCompactionHeadIndexReader) LabelNames(matchers ...*labels.Matcher) ([]string, error) { func (ir *OOOCompactionHeadIndexReader) LabelNames(context.Context, ...*labels.Matcher) ([]string, error) {
return nil, errors.New("not implemented") return nil, errors.New("not implemented")
} }
@ -434,7 +434,7 @@ func (ir *OOOCompactionHeadIndexReader) LabelValueFor(id storage.SeriesRef, labe
return "", errors.New("not implemented") return "", errors.New("not implemented")
} }
func (ir *OOOCompactionHeadIndexReader) LabelNamesFor(ids ...storage.SeriesRef) ([]string, error) { func (ir *OOOCompactionHeadIndexReader) LabelNamesFor(ctx context.Context, ids ...storage.SeriesRef) ([]string, error) {
return nil, errors.New("not implemented") return nil, errors.New("not implemented")
} }

View file

@ -94,8 +94,8 @@ func (q *blockBaseQuerier) LabelValues(name string, matchers ...*labels.Matcher)
return res, nil, err return res, nil, err
} }
func (q *blockBaseQuerier) LabelNames(matchers ...*labels.Matcher) ([]string, storage.Warnings, error) { func (q *blockBaseQuerier) LabelNames(ctx context.Context, matchers ...*labels.Matcher) ([]string, storage.Warnings, error) {
res, err := q.index.LabelNames(matchers...) res, err := q.index.LabelNames(ctx, matchers...)
return res, nil, err return res, nil, err
} }
@ -481,7 +481,7 @@ func labelValuesWithMatchers(r IndexReader, name string, matchers ...*labels.Mat
return values, nil return values, nil
} }
func labelNamesWithMatchers(r IndexReader, matchers ...*labels.Matcher) ([]string, error) { func labelNamesWithMatchers(ctx context.Context, r IndexReader, matchers ...*labels.Matcher) ([]string, error) {
p, err := PostingsForMatchers(r, matchers...) p, err := PostingsForMatchers(r, matchers...)
if err != nil { if err != nil {
return nil, err return nil, err
@ -495,7 +495,7 @@ func labelNamesWithMatchers(r IndexReader, matchers ...*labels.Matcher) ([]strin
return nil, errors.Wrapf(p.Err(), "postings for label names with matchers") return nil, errors.Wrapf(p.Err(), "postings for label names with matchers")
} }
return r.LabelNamesFor(postings...) return r.LabelNamesFor(ctx, postings...)
} }
// seriesData, used inside other iterators, are updated when we move from one series to another. // seriesData, used inside other iterators, are updated when we move from one series to another.

View file

@ -1487,7 +1487,7 @@ func (m mockIndex) LabelValueFor(id storage.SeriesRef, label string) (string, er
return m.series[id].l.Get(label), nil return m.series[id].l.Get(label), nil
} }
func (m mockIndex) LabelNamesFor(ids ...storage.SeriesRef) ([]string, error) { func (m mockIndex) LabelNamesFor(ctx context.Context, ids ...storage.SeriesRef) ([]string, error) {
namesMap := make(map[string]bool) namesMap := make(map[string]bool)
for _, id := range ids { for _, id := range ids {
m.series[id].l.Range(func(lbl labels.Label) { m.series[id].l.Range(func(lbl labels.Label) {
@ -1533,7 +1533,7 @@ func (m mockIndex) Series(ref storage.SeriesRef, builder *labels.ScratchBuilder,
return nil return nil
} }
func (m mockIndex) LabelNames(matchers ...*labels.Matcher) ([]string, error) { func (m mockIndex) LabelNames(_ context.Context, matchers ...*labels.Matcher) ([]string, error) {
names := map[string]struct{}{} names := map[string]struct{}{}
if len(matchers) == 0 { if len(matchers) == 0 {
for l := range m.postings { for l := range m.postings {
@ -2449,7 +2449,7 @@ func (m mockMatcherIndex) LabelValueFor(id storage.SeriesRef, label string) (str
return "", errors.New("label value for called") return "", errors.New("label value for called")
} }
func (m mockMatcherIndex) LabelNamesFor(ids ...storage.SeriesRef) ([]string, error) { func (m mockMatcherIndex) LabelNamesFor(ctx context.Context, ids ...storage.SeriesRef) ([]string, error) {
return nil, errors.New("label names for for called") return nil, errors.New("label names for for called")
} }
@ -2465,7 +2465,7 @@ func (m mockMatcherIndex) Series(ref storage.SeriesRef, builder *labels.ScratchB
return nil return nil
} }
func (m mockMatcherIndex) LabelNames(...*labels.Matcher) ([]string, error) { func (m mockMatcherIndex) LabelNames(context.Context, ...*labels.Matcher) ([]string, error) {
return []string{}, nil return []string{}, nil
} }

View file

@ -673,7 +673,7 @@ func (api *API) labelNames(r *http.Request) apiFuncResult {
labelNamesSet := make(map[string]struct{}) labelNamesSet := make(map[string]struct{})
for _, matchers := range matcherSets { for _, matchers := range matcherSets {
vals, callWarnings, err := q.LabelNames(matchers...) vals, callWarnings, err := q.LabelNames(r.Context(), matchers...)
if err != nil { if err != nil {
return apiFuncResult{nil, returnAPIError(err), warnings, nil} return apiFuncResult{nil, returnAPIError(err), warnings, nil}
} }
@ -691,7 +691,7 @@ func (api *API) labelNames(r *http.Request) apiFuncResult {
} }
slices.Sort(names) slices.Sort(names)
} else { } else {
names, warnings, err = q.LabelNames() names, warnings, err = q.LabelNames(r.Context())
if err != nil { if err != nil {
return apiFuncResult{nil, &apiError{errorExec, err}, warnings, nil} return apiFuncResult{nil, &apiError{errorExec, err}, warnings, nil}
} }

View file

@ -174,7 +174,7 @@ func (t errorTestQuerier) LabelValues(name string, matchers ...*labels.Matcher)
return nil, nil, t.err return nil, nil, t.err
} }
func (t errorTestQuerier) LabelNames(matchers ...*labels.Matcher) ([]string, storage.Warnings, error) { func (t errorTestQuerier) LabelNames(context.Context, ...*labels.Matcher) ([]string, storage.Warnings, error) {
return nil, nil, t.err return nil, nil, t.err
} }