@@ -25,6 +25,7 @@ import (
25
25
"sync"
26
26
"unicode/utf8"
27
27
28
+ "github.com/cespare/xxhash/v2"
28
29
"github.com/golang/protobuf/proto"
29
30
"github.com/prometheus/common/expfmt"
30
31
@@ -266,7 +267,7 @@ func (r *Registry) Register(c Collector) error {
266
267
descChan = make (chan * Desc , capDescChan )
267
268
newDescIDs = map [uint64 ]struct {}{}
268
269
newDimHashesByName = map [string ]uint64 {}
269
- collectorID uint64 // Just a sum of all desc IDs.
270
+ collectorID uint64 // All desc IDs XOR'd together .
270
271
duplicateDescErr error
271
272
)
272
273
go func () {
@@ -293,12 +294,12 @@ func (r *Registry) Register(c Collector) error {
293
294
if _ , exists := r .descIDs [desc .id ]; exists {
294
295
duplicateDescErr = fmt .Errorf ("descriptor %s already exists with the same fully-qualified name and const label values" , desc )
295
296
}
296
- // If it is not a duplicate desc in this collector, add it to
297
+ // If it is not a duplicate desc in this collector, XOR it to
297
298
// the collectorID. (We allow duplicate descs within the same
298
299
// collector, but their existence must be a no-op.)
299
300
if _ , exists := newDescIDs [desc .id ]; ! exists {
300
301
newDescIDs [desc .id ] = struct {}{}
301
- collectorID + = desc .id
302
+ collectorID ^ = desc .id
302
303
}
303
304
304
305
// Are all the label names and the help string consistent with
@@ -875,9 +876,9 @@ func checkMetricConsistency(
875
876
}
876
877
877
878
// Is the metric unique (i.e. no other metric with the same name and the same labels)?
878
- h := hashNew ()
879
- h = hashAdd ( h , name )
880
- h = hashAddByte ( h , separatorByte )
879
+ h := xxhash . New ()
880
+ h . WriteString ( name )
881
+ h . Write ( separatorByteSlice )
881
882
// Make sure label pairs are sorted. We depend on it for the consistency
882
883
// check.
883
884
if ! sort .IsSorted (labelPairSorter (dtoMetric .Label )) {
@@ -888,18 +889,19 @@ func checkMetricConsistency(
888
889
dtoMetric .Label = copiedLabels
889
890
}
890
891
for _ , lp := range dtoMetric .Label {
891
- h = hashAdd ( h , lp .GetName ())
892
- h = hashAddByte ( h , separatorByte )
893
- h = hashAdd ( h , lp .GetValue ())
894
- h = hashAddByte ( h , separatorByte )
892
+ h . WriteString ( lp .GetName ())
893
+ h . Write ( separatorByteSlice )
894
+ h . WriteString ( lp .GetValue ())
895
+ h . Write ( separatorByteSlice )
895
896
}
896
- if _ , exists := metricHashes [h ]; exists {
897
+ hSum := h .Sum64 ()
898
+ if _ , exists := metricHashes [hSum ]; exists {
897
899
return fmt .Errorf (
898
900
"collected metric %q { %s} was collected before with the same name and label values" ,
899
901
name , dtoMetric ,
900
902
)
901
903
}
902
- metricHashes [h ] = struct {}{}
904
+ metricHashes [hSum ] = struct {}{}
903
905
return nil
904
906
}
905
907
0 commit comments