diff --git a/pkg/store/prometheus.go b/pkg/store/prometheus.go index 6f5bd1b7de..f9e836aca9 100644 --- a/pkg/store/prometheus.go +++ b/pkg/store/prometheus.go @@ -116,12 +116,11 @@ func (p *PrometheusStore) Info(_ context.Context, _ *storepb.InfoRequest) (*stor mint, maxt := p.timestamps() res := &storepb.InfoResponse{ - Labels: make([]labelpb.ZLabel, 0, len(lset)), + Labels: labelpb.ZLabelsFromPromLabels(lset), StoreType: p.component.ToProto(), MinTime: mint, MaxTime: maxt, } - res.Labels = append(res.Labels, labelpb.ZLabelsFromPromLabels(lset)...) // Until we deprecate the single labels in the reply, we just duplicate // them here for migration/compatibility purposes. @@ -191,7 +190,7 @@ func (p *PrometheusStore) Series(r *storepb.SeriesRequest, seriesSrv storepb.Sto return err } for _, lbm := range labelMaps { - lset := make([]labelpb.ZLabel, 0, len(lbm)+len(finalExtLset)) + lset := make([]labelpb.ZLabel, 0, len(lbm)+finalExtLset.Len()) for k, v := range lbm { lset = append(lset, labelpb.ZLabel{Name: k, Value: v}) } @@ -290,19 +289,18 @@ func (p *PrometheusStore) queryPrometheus( } externalLbls := rmLabels(p.externalLabelsFn().Copy(), extLsetToRemove) + b := labels.NewScratchBuilder(16) for _, vector := range matrix { - seriesLbls := labels.Labels(make([]labels.Label, 0, len(vector.Metric))) + b.Reset() // Attach labels from samples. for k, v := range vector.Metric { - seriesLbls = append(seriesLbls, labels.FromStrings(string(k), string(v))...) + b.Add(string(k), string(v)) } - sort.Slice(seriesLbls, func(i, j int) bool { - return seriesLbls.Less(i, j) - }) - // Attach external labels for compatibility with remote read. - finalLbls := labelpb.ExtendSortedLabels(seriesLbls, externalLbls) - finalLbls = append(finalLbls, dedup.PushdownMarker) + b.Add(dedup.PushdownMarker.Name, dedup.PushdownMarker.Value) + b.Sort() + + finalLbls := labelpb.ExtendSortedLabels(b.Labels(), externalLbls) series := &prompb.TimeSeries{ Labels: labelpb.ZLabelsFromPromLabels(finalLbls), @@ -604,7 +602,7 @@ func matchesExternalLabels(ms []storepb.LabelMatcher, externalLabels labels.Labe return false, nil, err } - if len(externalLabels) == 0 { + if externalLabels.IsEmpty() { return true, tms, nil } @@ -682,9 +680,9 @@ func (p *PrometheusStore) LabelNames(ctx context.Context, r *storepb.LabelNamesR } if len(lbls) > 0 { - for _, extLbl := range extLset { - lbls = append(lbls, extLbl.Name) - } + extLset.Range(func(l labels.Label) { + lbls = append(lbls, l.Name) + }) sort.Strings(lbls) } @@ -746,10 +744,7 @@ func (p *PrometheusStore) LabelValues(ctx context.Context, r *storepb.LabelValue } func (p *PrometheusStore) LabelSet() []labelpb.ZLabelSet { - lset := p.externalLabelsFn() - - labels := make([]labelpb.ZLabel, 0, len(lset)) - labels = append(labels, labelpb.ZLabelsFromPromLabels(lset)...) + labels := labelpb.ZLabelsFromPromLabels(p.externalLabelsFn()) labelset := []labelpb.ZLabelSet{} if len(labels) > 0 { diff --git a/pkg/store/prometheus_test.go b/pkg/store/prometheus_test.go index d0597b6e9c..8ed13a9f99 100644 --- a/pkg/store/prometheus_test.go +++ b/pkg/store/prometheus_test.go @@ -169,9 +169,9 @@ func testPrometheusStoreSeriesE2e(t *testing.T, prefix string) { testutil.Equals(t, 1, len(srv.SeriesSet)) testutil.Equals(t, []labelpb.ZLabel{ + {Name: "__thanos_pushed_down", Value: "true"}, {Name: "a", Value: "b"}, {Name: "region", Value: "eu-west"}, - {Name: "__thanos_pushed_down", Value: "true"}, }, srv.SeriesSet[0].Labels) testutil.Equals(t, []string(nil), srv.Warnings) testutil.Equals(t, 1, len(srv.SeriesSet[0].Chunks)) diff --git a/pkg/store/storepb/testutil/series.go b/pkg/store/storepb/testutil/series.go index ffc69d1cc6..a2d43b6c56 100644 --- a/pkg/store/storepb/testutil/series.go +++ b/pkg/store/storepb/testutil/series.go @@ -138,7 +138,6 @@ func ReadSeriesFromBlock(t testing.TB, h tsdb.BlockReader, extLabels labels.Labe defer func() { testutil.Ok(t, ir.Close()) }() var ( - lset labels.Labels chunkMetas []chunks.Meta expected = make([]*storepb.Series, 0) ) @@ -148,8 +147,8 @@ func ReadSeriesFromBlock(t testing.TB, h tsdb.BlockReader, extLabels labels.Labe all := allPostings(context.TODO(), t, ir) for all.Next() { testutil.Ok(t, ir.Series(all.At(), &builder, &chunkMetas)) - lset = builder.Labels() - expected = append(expected, &storepb.Series{Labels: labelpb.ZLabelsFromPromLabels(append(extLabels.Copy(), lset...))}) + lset := labelpb.ExtendSortedLabels(builder.Labels(), extLabels) + expected = append(expected, &storepb.Series{Labels: labelpb.ZLabelsFromPromLabels(lset)}) if skipChunks { continue @@ -189,7 +188,7 @@ func appendFloatSamples(t testing.TB, app storage.Appender, tsLabel int, opts He testutil.Ok(t, err) for is := 1; is < opts.SamplesPerSeries; is++ { - _, err := app.Append(ref, nil, int64(tsLabel+is)*opts.ScrapeInterval.Milliseconds(), opts.Random.Float64()) + _, err := app.Append(ref, labels.EmptyLabels(), int64(tsLabel+is)*opts.ScrapeInterval.Milliseconds(), opts.Random.Float64()) testutil.Ok(t, err) } } @@ -218,7 +217,7 @@ func appendHistogramSamples(t testing.TB, app storage.Appender, tsLabel int, opt testutil.Ok(t, err) for is := 1; is < opts.SamplesPerSeries; is++ { - _, err := app.AppendHistogram(ref, nil, int64(tsLabel+is)*opts.ScrapeInterval.Milliseconds(), sample, nil) + _, err := app.AppendHistogram(ref, labels.EmptyLabels(), int64(tsLabel+is)*opts.ScrapeInterval.Milliseconds(), sample, nil) testutil.Ok(t, err) } } diff --git a/pkg/testutil/e2eutil/prometheus.go b/pkg/testutil/e2eutil/prometheus.go index b0f1fc6576..6ae618a199 100644 --- a/pkg/testutil/e2eutil/prometheus.go +++ b/pkg/testutil/e2eutil/prometheus.go @@ -509,10 +509,6 @@ func createBlock( app := h.Appender(ctx) for _, lset := range batch { - sort.Slice(lset, func(i, j int) bool { - return lset[i].Name < lset[j].Name - }) - var err error if sampleType == chunkenc.ValFloat { randMutex.Lock()