From 702b274ebfdb2952fdf2e0ef4c70c02817766a02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=93=9D=E5=AE=9D=E7=9F=B3=E7=9A=84=E5=82=BB=E8=AF=9D?= Date: Mon, 10 Apr 2023 16:03:36 +0800 Subject: [PATCH 1/2] truncateExtLabels support Unicode cut Signed-off-by: mickeyzzc --- pkg/query/endpointset.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/query/endpointset.go b/pkg/query/endpointset.go index 36f6d389fe..dfd3ec8390 100644 --- a/pkg/query/endpointset.go +++ b/pkg/query/endpointset.go @@ -11,6 +11,7 @@ import ( "sort" "sync" "time" + "unicode/utf8" "github.com/thanos-io/thanos/pkg/api/query/querypb" @@ -218,7 +219,13 @@ func newEndpointSetNodeCollector(labels ...string) *endpointSetNodeCollector { // truncateExtLabels truncates the stringify external labels with the format of {labels..}. func truncateExtLabels(s string, threshold int) string { if len(s) > threshold { - return fmt.Sprintf("%s}", s[:threshold-1]) + for cut := 1; cut < 4; cut++ { + for cap := 1; cap < 4; cap++ { + if utf8.ValidString(s[threshold-cut-cap : threshold-cut]) { + return fmt.Sprintf("%s}", s[:threshold-cut]) + } + } + } } return s } From 307b8e442d481b7fc3033dc5f1aecece4b2ac921 Mon Sep 17 00:00:00 2001 From: mickeyzzc Date: Tue, 11 Apr 2023 12:47:08 +0800 Subject: [PATCH 2/2] update TestTruncateExtLabels and pass test Signed-off-by: mickeyzzc --- pkg/query/endpointset_test.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/pkg/query/endpointset_test.go b/pkg/query/endpointset_test.go index 2edfe01718..70b95c8ab5 100644 --- a/pkg/query/endpointset_test.go +++ b/pkg/query/endpointset_test.go @@ -272,7 +272,7 @@ func (e *testEndpoints) CloseOne(addr string) { } func TestTruncateExtLabels(t *testing.T) { - const testLength = 5 + const testLength = 10 for _, tc := range []struct { labelToTruncate string @@ -283,20 +283,24 @@ func TestTruncateExtLabels(t *testing.T) { expectedOutput: "{abc}", }, { - labelToTruncate: "{abcd}", - expectedOutput: "{abc}", + labelToTruncate: "{abcdefgh}", + expectedOutput: "{abcdefgh}", }, { - labelToTruncate: "{abcde}", - expectedOutput: "{abc}", + labelToTruncate: "{abcdefghij}", + expectedOutput: "{abcdefgh}", }, { - labelToTruncate: "{abcdef}", - expectedOutput: "{abc}", + labelToTruncate: "{abcde花}", + expectedOutput: "{abcde花}", }, { - labelToTruncate: "{abcdefghij}", - expectedOutput: "{abc}", + labelToTruncate: "{abcde花朵}", + expectedOutput: "{abcde花}", + }, + { + labelToTruncate: "{abcde花fghij}", + expectedOutput: "{abcde花}", }, } { t.Run(tc.labelToTruncate, func(t *testing.T) {