File tree Expand file tree Collapse file tree 2 files changed +19
-2
lines changed Expand file tree Collapse file tree 2 files changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -361,15 +361,15 @@ func (r *Registry) Unregister(c Collector) bool {
361361var (
362362descChan = make (chan * Desc , capDescChan )
363363descIDs = map [uint64 ]struct {}{}
364- collectorID uint64 // Just a sum of the desc IDs.
364+ collectorID uint64 // All desc IDs XOR'd together .
365365)
366366go func () {
367367c .Describe (descChan )
368368close (descChan )
369369}()
370370for desc := range descChan {
371371if _ , exists := descIDs [desc .id ]; ! exists {
372- collectorID + = desc .id
372+ collectorID ^ = desc .id
373373descIDs [desc .id ] = struct {}{}
374374}
375375}
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments