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 {
361
361
var (
362
362
descChan = make (chan * Desc , capDescChan )
363
363
descIDs = map [uint64 ]struct {}{}
364
- collectorID uint64 // Just a sum of the desc IDs.
364
+ collectorID uint64 // All desc IDs XOR'd together .
365
365
)
366
366
go func () {
367
367
c .Describe (descChan )
368
368
close (descChan )
369
369
}()
370
370
for desc := range descChan {
371
371
if _ , exists := descIDs [desc .id ]; ! exists {
372
- collectorID + = desc .id
372
+ collectorID ^ = desc .id
373
373
descIDs [desc .id ] = struct {}{}
374
374
}
375
375
}
Original file line number Diff line number Diff line change @@ -863,6 +863,23 @@ func TestAlreadyRegistered(t *testing.T) {
863
863
}
864
864
}
865
865
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
+
866
883
// TestHistogramVecRegisterGatherConcurrency is an end-to-end test that
867
884
// concurrently calls Observe on random elements of a HistogramVec while the
868
885
// same HistogramVec is registered concurrently and the Gather method of the
You can’t perform that action at this time.
0 commit comments