Skip to content

Commit 82ce871

Browse files
authored
Merge pull request #663 from costela/master
Fix collectorID calculation for Unregister()
2 parents b292466 + 4be0ab4 commit 82ce871

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

prometheus/registry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,15 @@ func (r *Registry) Unregister(c Collector) bool {
361361
var (
362362
descChan = make(chan *Desc, capDescChan)
363363
descIDs = map[uint64]struct{}{}
364-
collectorID uint64 // Just a sum of the desc IDs.
364+
collectorID uint64 // All desc IDs XOR'd together.
365365
)
366366
go func() {
367367
c.Describe(descChan)
368368
close(descChan)
369369
}()
370370
for desc := range descChan {
371371
if _, exists := descIDs[desc.id]; !exists {
372-
collectorID += desc.id
372+
collectorID ^= desc.id
373373
descIDs[desc.id] = struct{}{}
374374
}
375375
}

prometheus/registry_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,23 @@ func TestAlreadyRegistered(t *testing.T) {
863863
}
864864
}
865865

866+
// TestRegisterUnregisterCollector ensures registering and unregistering a
867+
// collector doesn't leave any dangling metrics.
868+
// We use NewGoCollector as a nice concrete example of a collector with
869+
// multiple metrics.
870+
func TestRegisterUnregisterCollector(t *testing.T) {
871+
col := prometheus.NewGoCollector()
872+
873+
reg := prometheus.NewRegistry()
874+
reg.MustRegister(col)
875+
reg.Unregister(col)
876+
if metrics, err := reg.Gather(); err != nil {
877+
t.Error("error gathering sample metric")
878+
} else if len(metrics) != 0 {
879+
t.Error("should have unregistered metric")
880+
}
881+
}
882+
866883
// TestHistogramVecRegisterGatherConcurrency is an end-to-end test that
867884
// concurrently calls Observe on random elements of a HistogramVec while the
868885
// same HistogramVec is registered concurrently and the Gather method of the

0 commit comments

Comments
 (0)