From 903670051b0238f0ade23ee159b49a7729ccf86c Mon Sep 17 00:00:00 2001 From: ydrozhdzhal Date: Fri, 3 Dec 2021 19:55:20 +0200 Subject: [PATCH 1/2] Google Cloud Spanner Receiver: Added parsing only distinct items for sample lock request label. --- .../internal/metadata/labelvalue.go | 20 ++++++++++++++++--- .../internal/metadata/labelvalue_test.go | 2 ++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/receiver/googlecloudspannerreceiver/internal/metadata/labelvalue.go b/receiver/googlecloudspannerreceiver/internal/metadata/labelvalue.go index de597ee018f9..30bb71b26f6b 100644 --- a/receiver/googlecloudspannerreceiver/internal/metadata/labelvalue.go +++ b/receiver/googlecloudspannerreceiver/internal/metadata/labelvalue.go @@ -215,12 +215,26 @@ type lockRequest struct { func newLockRequestSliceLabelValue(metadata LabelValueMetadata, valueHolder interface{}) LabelValue { value := *valueHolder.(*[]*lockRequest) - convertedValue := make([]string, len(value)) + // During the specifics of this label we need to take into account only distinct values + uniqueValueItems := make(map[string]struct{}) + var convertedValue []string + + for _, valueItem := range value { + var valueItemString string + if valueItem.TransactionTag == "" { + valueItemString = fmt.Sprintf("{%v,%v}", valueItem.LockMode, valueItem.Column) + } else { + valueItemString = fmt.Sprintf("{%v,%v,%v}", valueItem.LockMode, valueItem.Column, valueItem.TransactionTag) + } - for i, valueItem := range value { - convertedValue[i] = fmt.Sprintf("{%v,%v,%v}", valueItem.LockMode, valueItem.Column, valueItem.TransactionTag) + if _, contains := uniqueValueItems[valueItemString]; !contains { + uniqueValueItems[valueItemString] = struct{}{} + convertedValue = append(convertedValue, valueItemString) + } } + sort.Strings(convertedValue) + constructedValue := strings.Join(convertedValue, ",") return lockRequestSliceLabelValue{ diff --git a/receiver/googlecloudspannerreceiver/internal/metadata/labelvalue_test.go b/receiver/googlecloudspannerreceiver/internal/metadata/labelvalue_test.go index 0cafd010d8c2..f09d8661aea2 100644 --- a/receiver/googlecloudspannerreceiver/internal/metadata/labelvalue_test.go +++ b/receiver/googlecloudspannerreceiver/internal/metadata/labelvalue_test.go @@ -282,6 +282,8 @@ func TestNewLockRequestSliceLabelValue(t *testing.T) { value := []*lockRequest{ {LockMode: "lockMode1", Column: "column1", TransactionTag: "tag1"}, {LockMode: "lockMode2", Column: "column2", TransactionTag: "tag2"}, + {LockMode: "lockMode1", Column: "column1", TransactionTag: "tag1"}, + {LockMode: "lockMode2", Column: "column2", TransactionTag: "tag2"}, } expectedValue := "{lockMode1,column1,tag1},{lockMode2,column2,tag2}" valueHolder := &value From 25ce886522c5769576a3d63848b3d2c5a1a6c7f9 Mon Sep 17 00:00:00 2001 From: ydrozhdzhal Date: Mon, 6 Dec 2021 12:37:55 +0200 Subject: [PATCH 2/2] Updated test data to cover missing if branch. --- .../internal/metadata/labelvalue_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/receiver/googlecloudspannerreceiver/internal/metadata/labelvalue_test.go b/receiver/googlecloudspannerreceiver/internal/metadata/labelvalue_test.go index f09d8661aea2..c5469942d4c1 100644 --- a/receiver/googlecloudspannerreceiver/internal/metadata/labelvalue_test.go +++ b/receiver/googlecloudspannerreceiver/internal/metadata/labelvalue_test.go @@ -284,8 +284,9 @@ func TestNewLockRequestSliceLabelValue(t *testing.T) { {LockMode: "lockMode2", Column: "column2", TransactionTag: "tag2"}, {LockMode: "lockMode1", Column: "column1", TransactionTag: "tag1"}, {LockMode: "lockMode2", Column: "column2", TransactionTag: "tag2"}, + {LockMode: "lockMode3", Column: "column3"}, } - expectedValue := "{lockMode1,column1,tag1},{lockMode2,column2,tag2}" + expectedValue := "{lockMode1,column1,tag1},{lockMode2,column2,tag2},{lockMode3,column3}" valueHolder := &value labelValue := newLockRequestSliceLabelValue(metadata, valueHolder)