Skip to content

Commit

Permalink
truncateExtLabels support Unicode cut (thanos-io#6267)
Browse files Browse the repository at this point in the history
* truncateExtLabels support Unicode cut

Signed-off-by: mickeyzzc <mickey_zzc@163.com>

* update TestTruncateExtLabels and pass test

Signed-off-by: mickeyzzc <mickey_zzc@163.com>

---------

Signed-off-by: mickeyzzc <mickey_zzc@163.com>
  • Loading branch information
mickeyzzc authored and HC Zhu committed Jun 27, 2023
1 parent 1cccffd commit 74e9898
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
9 changes: 8 additions & 1 deletion pkg/query/endpointset.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sort"
"sync"
"time"
"unicode/utf8"

"github.com/thanos-io/thanos/pkg/api/query/querypb"

Expand Down Expand Up @@ -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
}
Expand Down
22 changes: 13 additions & 9 deletions pkg/query/endpointset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 74e9898

Please sign in to comment.