Fix HashID conflicts in pathological cases

Use "\n" as delimiter as it cannot occur in the series name.

(cherry picked from commit e4009234e9)
This commit is contained in:
Daniel Nelson 2018-04-12 18:09:31 -07:00
parent 19873298b8
commit 8596a5c573
No known key found for this signature in database
GPG Key ID: CAAD59C9444F6155
2 changed files with 29 additions and 0 deletions

View File

@ -232,9 +232,12 @@ func (m *metric) IsAggregate() bool {
func (m *metric) HashID() uint64 { func (m *metric) HashID() uint64 {
h := fnv.New64a() h := fnv.New64a()
h.Write([]byte(m.name)) h.Write([]byte(m.name))
h.Write([]byte("\n"))
for _, tag := range m.tags { for _, tag := range m.tags {
h.Write([]byte(tag.Key)) h.Write([]byte(tag.Key))
h.Write([]byte("\n"))
h.Write([]byte(tag.Value)) h.Write([]byte(tag.Value))
h.Write([]byte("\n"))
} }
return h.Sum64() return h.Sum64()
} }

View File

@ -267,6 +267,32 @@ func TestHashID_Consistency(t *testing.T) {
assert.Equal(t, m2.HashID(), m3.HashID()) 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) { func TestSetName(t *testing.T) {
m := baseMetric() m := baseMetric()
m.SetName("foo") m.SetName("foo")