Skip to content

Commit

Permalink
Fix HashID conflicts in pathological cases
Browse files Browse the repository at this point in the history
Use "\n" as delimiter as it cannot occur in the series name.
  • Loading branch information
danielnelson committed Apr 13, 2018
1 parent 8d516d2 commit e400923
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,12 @@ func (m *metric) IsAggregate() bool {
func (m *metric) HashID() uint64 {
h := fnv.New64a()
h.Write([]byte(m.name))
h.Write([]byte("\n"))
for _, tag := range m.tags {
h.Write([]byte(tag.Key))
h.Write([]byte("\n"))
h.Write([]byte(tag.Value))
h.Write([]byte("\n"))
}
return h.Sum64()
}
Expand Down
26 changes: 26 additions & 0 deletions metric/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,32 @@ func TestHashID_Consistency(t *testing.T) {
assert.Equal(t, m2.HashID(), m3.HashID())
}

func TestHashID_Delimiting(t *testing.T) {
m1, _ := New(
"cpu",
map[string]string{
"a": "x",
"b": "y",
"c": "z",
},
map[string]interface{}{
"value": float64(1),
},
time.Now(),
)
m2, _ := New(
"cpu",
map[string]string{
"a": "xbycz",
},
map[string]interface{}{
"value": float64(1),
},
time.Now(),
)
assert.NotEqual(t, m1.HashID(), m2.HashID())
}

func TestSetName(t *testing.T) {
m := baseMetric()
m.SetName("foo")
Expand Down

0 comments on commit e400923

Please sign in to comment.