Skip to content
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
17 changes: 11 additions & 6 deletions go/stats/counters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"math/rand"
"reflect"
"sort"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -97,17 +98,21 @@ func TestMultiCountersDot(t *testing.T) {
c.Add([]string{"c1.a", "c1b"}, 1)
c.Add([]string{"c2a", "c2.b"}, 1)
c.Add([]string{"c2a", "c2.b"}, 1)
want1 := `{"c1_a.c1b": 1, "c2a.c2_b": 2}`
want2 := `{"c2a.c2_b": 2, "c1_a.c1b": 1}`
c1a := safeLabel("c1.a")
c1aJSON := strings.Replace(c1a, "\\", "\\\\", -1)
c2b := safeLabel("c2.b")
c2bJSON := strings.Replace(c2b, "\\", "\\\\", -1)
want1 := `{"` + c1aJSON + `.c1b": 1, "c2a.` + c2bJSON + `": 2}`
want2 := `{"c2a.` + c2bJSON + `": 2, "` + c1aJSON + `.c1b": 1}`
if s := c.String(); s != want1 && s != want2 {
t.Errorf("want %s or %s, got %s", want1, want2, s)
}
counts := c.Counts()
if counts["c1_a.c1b"] != 1 {
t.Errorf("want 1, got %d", counts["c1_a.c1b"])
if counts[c1a+".c1b"] != 1 {
t.Errorf("want 1, got %d", counts[c1a+".c1b"])
}
if counts["c2a.c2_b"] != 2 {
t.Errorf("want 2, got %d", counts["c2a.c2_b"])
if counts["c2a."+c2b] != 2 {
t.Errorf("want 2, got %d", counts["c2a."+c2b])
}
}

Expand Down
27 changes: 27 additions & 0 deletions go/stats/safe_label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2018 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package stats

import (
"strings"
)

// safeLabel turns a label into a safe label for stats export.
// It is in its own file so it can be customized.
func safeLabel(label string) string {
return strings.Replace(label, ".", "_", -1)
}
7 changes: 4 additions & 3 deletions go/stats/timings.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,12 @@ func (mt *MultiTimings) Labels() []string {
}

// safeJoinLabels joins the label values with ".", but first replaces any existing
// "." characters in the labels with "_" to avoid issues parsing them apart later.
// "." characters in the labels with the proper replacement, to avoid issues parsing
// them apart later.
func safeJoinLabels(labels []string) string {
sanitizedLabels := make([]string, len(labels), len(labels))
sanitizedLabels := make([]string, len(labels))
for idx, label := range labels {
sanitizedLabels[idx] = strings.Replace(label, ".", "_", -1)
sanitizedLabels[idx] = safeLabel(label)
}
return strings.Join(sanitizedLabels, ".")
}
Expand Down
5 changes: 4 additions & 1 deletion go/stats/timings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package stats

import (
"expvar"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -50,7 +51,9 @@ func TestMultiTimingsDot(t *testing.T) {
clear()
mtm := NewMultiTimings("maptimings2", "help", []string{"label"})
mtm.Add([]string{"value.dot"}, 500*time.Microsecond)
want := `{"TotalCount":1,"TotalTime":500000,"Histograms":{"value_dot":{"500000":1,"1000000":1,"5000000":1,"10000000":1,"50000000":1,"100000000":1,"500000000":1,"1000000000":1,"5000000000":1,"10000000000":1,"inf":1,"Count":1,"Time":500000}}}`
safe := safeLabel("value.dot")
safeJSON := strings.Replace(safe, "\\", "\\\\", -1)
want := `{"TotalCount":1,"TotalTime":500000,"Histograms":{"` + safeJSON + `":{"500000":1,"1000000":1,"5000000":1,"10000000":1,"50000000":1,"100000000":1,"500000000":1,"1000000000":1,"5000000000":1,"10000000000":1,"inf":1,"Count":1,"Time":500000}}}`
if got := mtm.String(); got != want {
t.Errorf("got %s, want %s", got, want)
}
Expand Down