Skip to content

Commit c2e3855

Browse files
committed
Minimal “fix” for hash collisions
This makes the collisions a bit less likely by XOR'ing descIDs rather than adding them up for the collectorID. Signed-off-by: beorn7 <[email protected]>
1 parent bf9ff71 commit c2e3855

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

prometheus/registry.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (r *Registry) Register(c Collector) error {
266266
descChan = make(chan *Desc, capDescChan)
267267
newDescIDs = map[uint64]struct{}{}
268268
newDimHashesByName = map[string]uint64{}
269-
collectorID uint64 // Just a sum of all desc IDs.
269+
collectorID uint64 // All desc IDs XOR'd together.
270270
duplicateDescErr error
271271
)
272272
go func() {
@@ -293,12 +293,12 @@ func (r *Registry) Register(c Collector) error {
293293
if _, exists := r.descIDs[desc.id]; exists {
294294
duplicateDescErr = fmt.Errorf("descriptor %s already exists with the same fully-qualified name and const label values", desc)
295295
}
296-
// If it is not a duplicate desc in this collector, add it to
296+
// If it is not a duplicate desc in this collector, XOR it to
297297
// the collectorID. (We allow duplicate descs within the same
298298
// collector, but their existence must be a no-op.)
299299
if _, exists := newDescIDs[desc.id]; !exists {
300300
newDescIDs[desc.id] = struct{}{}
301-
collectorID += desc.id
301+
collectorID ^= desc.id
302302
}
303303

304304
// Are all the label names and the help string consistent with

0 commit comments

Comments
 (0)