Skip to content
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

truncateExtLabels support Unicode cut #6267

Merged
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
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