Skip to content
Closed
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
2 changes: 1 addition & 1 deletion go/stats/counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func NewMultiCountersFunc(name string, labels []string, f CountersFunc) *MultiCo
return t
}

var escaper = strings.NewReplacer(".", "\\.", "\\", "\\\\")
var escaper = strings.NewReplacer(".", "\\\\.", "\\", "\\\\\\")

func mapKey(ss []string) string {
esc := make([]string, len(ss))
Expand Down
32 changes: 26 additions & 6 deletions go/stats/counters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package stats

import (
"encoding/json"
"expvar"
"math/rand"
"reflect"
Expand Down Expand Up @@ -106,17 +107,36 @@ 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}`
want1 := `{"c1\\.a.c1b": 1, "c2a.c2\\.b": 2}`
want2 := `{"c2a.c2\\.b": 2, "c1\\.a.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["c1\\\\.a.c1b"] != 1 {
t.Errorf("want 1, got %d", counts["c1\\\\.a.c1b"])
}
if counts["c2a.c2\\.b"] != 2 {
t.Errorf("want 2, got %d", counts["c2a.c2\\.b"])
if counts["c2a.c2\\\\.b"] != 2 {
t.Errorf("want 2, got %d", counts["c2a.c2\\\\.b"])
}

m := make(map[string]interface{})
if err := json.Unmarshal([]byte(c.String()), &m); err != nil {
t.Errorf("failed to parse as json: %v", err)
}

c = NewMultiCounters("mapCounter3", []string{"aaa", "bbb"})
c.Add([]string{"c2a", `c2\b`}, 1)
if s, want := c.String(), `{"c2a.c2\\\b": 1}`; s != want {
t.Errorf("want %v, got %v", want, s)
}
counts = c.Counts()
if got := counts[`c2a.c2\\\b`]; got != 1 {
t.Errorf("want 1, got %d: %v", got, counts)
}

if err := json.Unmarshal([]byte(c.String()), &m); err != nil {
t.Errorf("failed to parse as json: %v", err)
}
}

Expand Down