Skip to content

Use labels Bytes() rather than String() for map key #5095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/distributor/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,11 @@ func (d *Distributor) queryIngestersExemplars(ctx context.Context, replicationSe
func mergeExemplarQueryResponses(results []interface{}) *ingester_client.ExemplarQueryResponse {
var keys []string
exemplarResults := make(map[string]cortexpb.TimeSeries)
buf := make([]byte, 0, 1024)
for _, result := range results {
r := result.(*ingester_client.ExemplarQueryResponse)
for _, ts := range r.Timeseries {
lbls := cortexpb.FromLabelAdaptersToLabels(ts.Labels).String()
lbls := string(cortexpb.FromLabelAdaptersToLabels(ts.Labels).Bytes(buf))
e, ok := exemplarResults[lbls]
if !ok {
exemplarResults[lbls] = ts
Expand Down
3 changes: 2 additions & 1 deletion pkg/querier/tripperware/instantquery/instant_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ func (instantQueryCodec) MergeResponse(ctx context.Context, responses ...tripper

func vectorMerge(resps []*PrometheusInstantQueryResponse) *Vector {
output := map[string]*Sample{}
buf := make([]byte, 0, 1024)
for _, resp := range resps {
if resp == nil {
continue
Expand All @@ -313,7 +314,7 @@ func vectorMerge(resps []*PrometheusInstantQueryResponse) *Vector {
if s == nil {
continue
}
metric := cortexpb.FromLabelAdaptersToLabels(sample.Labels).String()
metric := string(cortexpb.FromLabelAdaptersToLabels(sample.Labels).Bytes(buf))
if existingSample, ok := output[metric]; !ok {
output[metric] = s
} else if existingSample.GetSample().TimestampMs < s.GetSample().TimestampMs {
Expand Down
3 changes: 2 additions & 1 deletion pkg/querier/tripperware/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (

// MergeSampleStreams deduplicates sample streams using a map.
func MergeSampleStreams(output map[string]SampleStream, sampleStreams []SampleStream) {
buf := make([]byte, 0, 1024)
for _, stream := range sampleStreams {
metric := cortexpb.FromLabelAdaptersToLabels(stream.Labels).String()
metric := string(cortexpb.FromLabelAdaptersToLabels(stream.Labels).Bytes(buf))
existing, ok := output[metric]
if !ok {
existing = SampleStream{
Expand Down
9 changes: 5 additions & 4 deletions pkg/querier/tripperware/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/cortexproject/cortex/pkg/cortexpb"
ingester_client "github.com/cortexproject/cortex/pkg/ingester/client"
)

func TestMergeSampleStreams(t *testing.T) {
Expand Down Expand Up @@ -34,7 +35,7 @@ func TestMergeSampleStreams(t *testing.T) {
},
},
expectedOutput: map[string]SampleStream{
lbls.String(): {
ingester_client.LabelsToKeyString(lbls): {
Labels: cortexpb.FromLabelsToLabelAdapters(lbls),
Samples: []cortexpb.Sample{
{Value: 0, TimestampMs: 0},
Expand Down Expand Up @@ -63,7 +64,7 @@ func TestMergeSampleStreams(t *testing.T) {
},
},
expectedOutput: map[string]SampleStream{
lbls.String(): {
ingester_client.LabelsToKeyString(lbls): {
Labels: cortexpb.FromLabelsToLabelAdapters(lbls),
Samples: []cortexpb.Sample{
{Value: 0, TimestampMs: 0},
Expand Down Expand Up @@ -109,7 +110,7 @@ func TestMergeSampleStreams(t *testing.T) {
},
},
expectedOutput: map[string]SampleStream{
lbls.String(): {
ingester_client.LabelsToKeyString(lbls): {
Labels: cortexpb.FromLabelsToLabelAdapters(lbls),
Samples: []cortexpb.Sample{
{Value: 0, TimestampMs: 0},
Expand All @@ -118,7 +119,7 @@ func TestMergeSampleStreams(t *testing.T) {
{Value: 4, TimestampMs: 4},
},
},
lbls1.String(): {
ingester_client.LabelsToKeyString(lbls1): {
Labels: cortexpb.FromLabelsToLabelAdapters(lbls1),
Samples: []cortexpb.Sample{
{Value: 0, TimestampMs: 0},
Expand Down