diff --git a/pkg/storage/stores/shipper/indexshipper/tsdb/index/index.go b/pkg/storage/stores/shipper/indexshipper/tsdb/index/index.go index 60a8d5f1d62cf..f3cb7653cbe9f 100644 --- a/pkg/storage/stores/shipper/indexshipper/tsdb/index/index.go +++ b/pkg/storage/stores/shipper/indexshipper/tsdb/index/index.go @@ -2234,7 +2234,7 @@ func (dec *Decoder) prepSeries(b []byte, lbls *labels.Labels, chks *[]ChunkMeta) // prepSeriesBy returns series labels and chunks for a series and only returning selected `by` label names. // If `by` is empty, it returns all labels for the series. func (dec *Decoder) prepSeriesBy(b []byte, lbls *labels.Labels, chks *[]ChunkMeta, by map[string]struct{}) (*encoding.Decbuf, uint64, error) { - if len(by) == 0 { + if by == nil { return dec.prepSeries(b, lbls, chks) } *lbls = (*lbls)[:0] diff --git a/pkg/storage/stores/shipper/indexshipper/tsdb/single_file_index.go b/pkg/storage/stores/shipper/indexshipper/tsdb/single_file_index.go index c523381534fb2..5385d418ea689 100644 --- a/pkg/storage/stores/shipper/indexshipper/tsdb/single_file_index.go +++ b/pkg/storage/stores/shipper/indexshipper/tsdb/single_file_index.go @@ -290,12 +290,15 @@ func (i *TSDBIndex) Stats(ctx context.Context, _ string, from, through model.Tim // TODO(owen-d): use pool var ls labels.Labels var filterer chunk.Filterer + by := make(map[string]struct{}) if i.chunkFilter != nil { filterer = i.chunkFilter.ForRequest(ctx) + for _, k := range i.chunkFilter.ForRequest(ctx).RequiredLabelNames() { + by[k] = struct{}{} + } } - for p.Next() { - fp, stats, err := i.reader.ChunkStats(p.At(), int64(from), int64(through), &ls, nil) + fp, stats, err := i.reader.ChunkStats(p.At(), int64(from), int64(through), &ls, by) if err != nil { return err } @@ -365,11 +368,10 @@ func (i *TSDBIndex) Volume( for k := range labelsToMatch { by[k] = struct{}{} } - if len(labelsToMatch) > 0 { - for k := range labelsToMatch { - by[k] = struct{}{} - } + for _, k := range targetLabels { + by[k] = struct{}{} } + // If we are aggregating by series, we need to include all labels in the series required for filtering chunks. if i.chunkFilter != nil { for _, k := range i.chunkFilter.ForRequest(ctx).RequiredLabelNames() {