Skip to content

Commit

Permalink
Fix multibyte splitting bug in graph titles Closes #179 (#202)
Browse files Browse the repository at this point in the history
* Fix multibyte splitting bug in graph titles

* add testcase

* add testcase when len(label) < 30 simbols and len(label) > 30 bytes
  • Loading branch information
ifireice authored and borovskyav committed Jan 17, 2019
1 parent 8919bbc commit fa9868b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions plotting/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math"
"strings"
"time"
"unicode/utf8"

"github.com/dustin/go-humanize"
"github.com/go-graphite/carbonapi/expr/types"
Expand All @@ -28,9 +29,9 @@ func (initial sortedByLen) Swap(i int, j int) {

// sanitizeLabelName shortens label names to max length
func sanitizeLabelName(label string, maxLabelLength int) string {
labelLength := len(label)
labelLength := utf8.RuneCountInString(label)
if labelLength > maxLabelLength {
label = label[:maxLabelLength-3]
label = string([]rune(label)[:maxLabelLength-3])
label += "..."
}
return label
Expand Down
6 changes: 6 additions & 0 deletions plotting/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func TestSanitizeLabelName(t *testing.T) {
"HostName.CategoryName.CategoryCounterName.CategoryCounterType.MetricName",
"ServiceName.HostName.CategoryName.CategoryCounterName.CategoryCounterType.MetricName",
"MetricPrefix.ServiceName.HostName.CategoryName.CategoryCounterName.CategoryCounterType.MetricName",
"Рост количества ответов nginx р95",
"ЯдлиннаядлиннаястрокабезпробеловизрусскихбуквчтобыТимуриАркадийневыпендривалисьаЛешенепришлосьприходитьивсеобъяснятьнормально😈",
"Привет, не режь меня!",
}
labelsShortForm := []string{
"MetricName",
Expand All @@ -60,6 +63,9 @@ func TestSanitizeLabelName(t *testing.T) {
"HostName.CategoryName.Categ...",
"ServiceName.HostName.Catego...",
"MetricPrefix.ServiceName.Ho...",
"Рост количества ответов ngi...",
"Ядлиннаядлиннаястрокабезпро...",
"Привет, не режь меня!",
}
Convey("sanitize lables names", t, func() {
maxLabelLength := 30
Expand Down

0 comments on commit fa9868b

Please sign in to comment.