From ef8b26a2259621316e5385fd798f5755f352e521 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Tue, 31 Oct 2023 18:45:35 +0100 Subject: [PATCH] store: fix stringlabel build errors for prometheus store api (#6861) Signed-off-by: Michael Hoffmann --- pkg/store/prometheus.go | 33 ++++++++++++---------------- pkg/store/storepb/testutil/series.go | 8 +++---- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/pkg/store/prometheus.go b/pkg/store/prometheus.go index 6f48d8e9e9..3c10c8d016 100644 --- a/pkg/store/prometheus.go +++ b/pkg/store/prometheus.go @@ -121,12 +121,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. @@ -194,7 +193,7 @@ func (p *PrometheusStore) Series(r *storepb.SeriesRequest, s storepb.Store_Serie 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}) } @@ -293,19 +292,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), @@ -606,7 +604,7 @@ func matchesExternalLabels(ms []storepb.LabelMatcher, externalLabels labels.Labe return false, nil, err } - if len(externalLabels) == 0 { + if externalLabels.IsEmpty() { return true, tms, nil } @@ -683,9 +681,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) } @@ -761,10 +759,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/storepb/testutil/series.go b/pkg/store/storepb/testutil/series.go index 32f6297914..aa89c1b0b3 100644 --- a/pkg/store/storepb/testutil/series.go +++ b/pkg/store/storepb/testutil/series.go @@ -136,8 +136,8 @@ func ReadSeriesFromBlock(t testing.TB, h tsdb.BlockReader, extLabels labels.Labe all := allPostings(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 +189,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) } } @@ -211,7 +211,7 @@ func appendHistogramSamples(t testing.TB, app storage.Appender, tsLabel int, opt testutil.Ok(t, err) for i, h := range histograms[1:] { - _, err := app.AppendHistogram(ref, nil, int64(tsLabel+i+1)*opts.ScrapeInterval.Milliseconds(), h, nil) + _, err := app.AppendHistogram(ref, labels.EmptyLabels(), int64(tsLabel+i+1)*opts.ScrapeInterval.Milliseconds(), h, nil) testutil.Ok(t, err) } }