@@ -22,6 +22,8 @@ import (
22
22
"strings"
23
23
)
24
24
25
+ var separator = []byte {0 }
26
+
25
27
// A Metric is similar to a LabelSet, but the key difference is that a Metric is
26
28
// a singleton and refers to one and only one stream of samples.
27
29
type Metric map [LabelName ]LabelValue
@@ -64,23 +66,34 @@ func (m Metric) String() string {
64
66
65
67
// Fingerprint returns a Metric's Fingerprint.
66
68
func (m Metric ) Fingerprint () Fingerprint {
67
- labelLength := len (m )
68
- labelNames := make ([] string , 0 , labelLength )
69
+ labelNames := make ([] string , 0 , len (m ) )
70
+ maxLength := 0
69
71
70
- for labelName := range m {
72
+ for labelName , labelValue := range m {
71
73
labelNames = append (labelNames , string (labelName ))
74
+ if len (labelName ) > maxLength {
75
+ maxLength = len (labelName )
76
+ }
77
+ if len (labelValue ) > maxLength {
78
+ maxLength = len (labelValue )
79
+ }
72
80
}
73
81
74
82
sort .Strings (labelNames )
75
83
76
84
summer := fnv .New64a ()
85
+ buf := make ([]byte , maxLength )
77
86
78
87
for _ , labelName := range labelNames {
79
88
labelValue := m [LabelName (labelName )]
80
89
81
- summer .Write ([]byte (labelName ))
82
- summer .Write ([]byte {0 })
83
- summer .Write ([]byte (labelValue ))
90
+ copy (buf , labelName )
91
+ summer .Write (buf [:len (labelName )])
92
+
93
+ summer .Write (separator )
94
+
95
+ copy (buf , labelValue )
96
+ summer .Write (buf [:len (labelValue )])
84
97
}
85
98
86
99
return Fingerprint (binary .LittleEndian .Uint64 (summer .Sum (nil )))
0 commit comments