From 74e989817b20730f48a435a31dd4292740d3e5bd Mon Sep 17 00:00:00 2001 From: MickeyZZC Date: Wed, 12 Apr 2023 10:52:14 +0800 Subject: [PATCH] truncateExtLabels support Unicode cut (#6267) * truncateExtLabels support Unicode cut Signed-off-by: mickeyzzc * update TestTruncateExtLabels and pass test Signed-off-by: mickeyzzc --------- Signed-off-by: mickeyzzc --- pkg/query/endpointset.go | 9 ++++++++- pkg/query/endpointset_test.go | 22 +++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/pkg/query/endpointset.go b/pkg/query/endpointset.go index 36f6d389fea..dfd3ec83906 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 } diff --git a/pkg/query/endpointset_test.go b/pkg/query/endpointset_test.go index 2edfe01718f..70b95c8ab5e 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) {