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
25 changes: 19 additions & 6 deletions model/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"strings"
)

var separator = []byte{0}

// A Metric is similar to a LabelSet, but the key difference is that a Metric is
// a singleton and refers to one and only one stream of samples.
type Metric map[LabelName]LabelValue
Expand Down Expand Up @@ -64,23 +66,34 @@ func (m Metric) String() string {

// Fingerprint returns a Metric's Fingerprint.
func (m Metric) Fingerprint() Fingerprint {
labelLength := len(m)
labelNames := make([]string, 0, labelLength)
labelNames := make([]string, 0, len(m))
maxLength := 0

for labelName := range m {
for labelName, labelValue := range m {
labelNames = append(labelNames, string(labelName))
if len(labelName) > maxLength {
maxLength = len(labelName)
}
if len(labelValue) > maxLength {
maxLength = len(labelValue)
}
}

sort.Strings(labelNames)

summer := fnv.New64a()
buf := make([]byte, maxLength)

for _, labelName := range labelNames {
labelValue := m[LabelName(labelName)]

summer.Write([]byte(labelName))
summer.Write([]byte{0})
summer.Write([]byte(labelValue))
copy(buf, labelName)
summer.Write(buf[:len(labelName)])

summer.Write(separator)

copy(buf, labelValue)
summer.Write(buf[:len(labelValue)])
}

return Fingerprint(binary.LittleEndian.Uint64(summer.Sum(nil)))
Expand Down
18 changes: 18 additions & 0 deletions model/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ func testMetric(t testing.TB) {
},
fingerprint: 1470933794305433534,
},
// The following two demonstrate a bug in fingerprinting. They
// should not have the same fingerprint with a sane
// fingerprinting function. See
// https://github.com/prometheus/client_golang/issues/74 .
{
input: Metric{
"a": "bb",
"b": "c",
},
fingerprint: 3734646176939799877,
},
{
input: Metric{
"a": "b",
"bb": "c",
},
fingerprint: 3734646176939799877,
},
}

for i, scenario := range scenarios {
Expand Down