@@ -66,16 +66,20 @@ func init() {
66
66
// pre-registered.
67
67
func NewRegistry () * Registry {
68
68
return & Registry {
69
- collectorsByID : map [uint64 ]Collector {},
70
- collectorsByCompatID : map [uint64 ]Collector {},
71
- descIDs : map [uint64 ]struct {}{},
72
- compatDescIDs : map [uint64 ]struct {}{},
73
- dimHashesByName : map [string ]uint64 {},
69
+ collectorsByID : map [uint64 ]Collector {},
70
+ collectorsByEscapedID : map [uint64 ]Collector {},
71
+ descIDs : map [uint64 ]struct {}{},
72
+ escapedDescIDs : map [uint64 ]struct {}{},
73
+ dimHashesByName : map [string ]uint64 {},
74
74
}
75
75
}
76
76
77
- func (r * Registry ) AllowCompatCollisions (allow bool ) * Registry {
78
- r .utf8Collision = allow
77
+ // AllowEscapedCollisions determines whether the Registry should reject
78
+ // Collectors that would collide when escaped to underscores for compatibility
79
+ // with older systems. You may set this option to Allow if you know your metrics
80
+ // will never be scraped by an older system.
81
+ func (r * Registry ) AllowEscapedCollisions (allow bool ) * Registry {
82
+ r .disableLegacyCollision = allow
79
83
return r
80
84
}
81
85
@@ -267,27 +271,28 @@ func (errs MultiError) MaybeUnwrap() error {
267
271
type Registry struct {
268
272
mtx sync.RWMutex
269
273
collectorsByID map [uint64 ]Collector // ID is a hash of the descIDs.
270
- // stores colletors by compatid , only if compat id is different (otherwise we
271
- // can just do the lookup in the regular map).
272
- collectorsByCompatID map [uint64 ]Collector
273
- descIDs map [uint64 ]struct {}
274
+ // stores colletors by escapedID , only if escaped id is different (otherwise
275
+ // we can just do the lookup in the regular map).
276
+ collectorsByEscapedID map [uint64 ]Collector
277
+ descIDs map [uint64 ]struct {}
274
278
// desc ids, only if different
275
- compatDescIDs map [uint64 ]struct {}
279
+ escapedDescIDs map [uint64 ]struct {}
276
280
dimHashesByName map [string ]uint64
277
281
uncheckedCollectors []Collector
278
282
pedanticChecksEnabled bool
279
- utf8Collision bool
283
+ // This flag is inverted so that the default can be the false value.
284
+ disableLegacyCollision bool
280
285
}
281
286
282
287
// Register implements Registerer.
283
288
func (r * Registry ) Register (c Collector ) error {
284
289
var (
285
290
descChan = make (chan * Desc , capDescChan )
286
291
newDescIDs = map [uint64 ]struct {}{}
287
- newCompatIDs = map [uint64 ]struct {}{}
292
+ newEscapedIDs = map [uint64 ]struct {}{}
288
293
newDimHashesByName = map [string ]uint64 {}
289
294
collectorID uint64 // All desc IDs XOR'd together.
290
- compatID uint64
295
+ escapedID uint64
291
296
duplicateDescErr error
292
297
)
293
298
go func () {
@@ -324,19 +329,19 @@ func (r *Registry) Register(c Collector) error {
324
329
325
330
// Unless we are in pure UTF-8 mode, also check to see if the descID is
326
331
// unique when all the names are escaped to underscores.
327
- if ! r .utf8Collision {
328
- if _ , exists := r .compatDescIDs [desc .compatID ]; exists {
332
+ if ! r .disableLegacyCollision {
333
+ if _ , exists := r .escapedDescIDs [desc .escapedID ]; exists {
329
334
duplicateDescErr = fmt .Errorf ("descriptor %s will collide with an existing descriptor when escaped for compatibility with non-UTF8 systems" , desc )
330
335
}
331
- if _ , exists := r .descIDs [desc .compatID ]; exists {
336
+ if _ , exists := r .descIDs [desc .escapedID ]; exists {
332
337
duplicateDescErr = fmt .Errorf ("descriptor %s will collide with an existing descriptor when escaped for compatibility with non-UTF8 systems" , desc )
333
338
}
334
339
}
335
- if _ , exists := newCompatIDs [desc .compatID ]; ! exists {
336
- if desc .compatID != desc .id {
337
- newCompatIDs [desc .compatID ] = struct {}{}
340
+ if _ , exists := newEscapedIDs [desc .escapedID ]; ! exists {
341
+ if desc .escapedID != desc .id {
342
+ newEscapedIDs [desc .escapedID ] = struct {}{}
338
343
}
339
- compatID ^= desc .compatID
344
+ escapedID ^= desc .escapedID
340
345
}
341
346
342
347
// Are all the label names and the help string consistent with
@@ -367,10 +372,10 @@ func (r *Registry) Register(c Collector) error {
367
372
existing , collision := r .collectorsByID [collectorID ]
368
373
// Unless we are in pure UTF-8 mode, we also need to check that the
369
374
// underscore-escaped versions of the IDs don't match.
370
- if ! collision && ! r .utf8Collision {
371
- existing , collision = r .collectorsByID [compatID ]
375
+ if ! collision && ! r .disableLegacyCollision {
376
+ existing , collision = r .collectorsByID [escapedID ]
372
377
if ! collision {
373
- existing , collision = r .collectorsByCompatID [ compatID ]
378
+ existing , collision = r .collectorsByEscapedID [ escapedID ]
374
379
}
375
380
}
376
381
@@ -396,30 +401,30 @@ func (r *Registry) Register(c Collector) error {
396
401
397
402
// Only after all tests have passed, actually register.
398
403
r .collectorsByID [collectorID ] = c
399
- // We only need to store the compatID if it doesn't match the unescaped one.
400
- if compatID != collectorID {
401
- r .collectorsByCompatID [ compatID ] = c
404
+ // We only need to store the escapedID if it doesn't match the unescaped one.
405
+ if escapedID != collectorID {
406
+ r .collectorsByEscapedID [ escapedID ] = c
402
407
}
403
408
for hash := range newDescIDs {
404
409
r .descIDs [hash ] = struct {}{}
405
410
}
406
411
for name , dimHash := range newDimHashesByName {
407
412
r .dimHashesByName [name ] = dimHash
408
413
}
409
- for hash := range newCompatIDs {
410
- r .compatDescIDs [hash ] = struct {}{}
414
+ for hash := range newEscapedIDs {
415
+ r .escapedDescIDs [hash ] = struct {}{}
411
416
}
412
417
return nil
413
418
}
414
419
415
420
// Unregister implements Registerer.
416
421
func (r * Registry ) Unregister (c Collector ) bool {
417
422
var (
418
- descChan = make (chan * Desc , capDescChan )
419
- descIDs = map [uint64 ]struct {}{}
420
- compatDescIDs = map [uint64 ]struct {}{}
421
- collectorID uint64 // All desc IDs XOR'd together.
422
- compatID uint64
423
+ descChan = make (chan * Desc , capDescChan )
424
+ descIDs = map [uint64 ]struct {}{}
425
+ escpaedDescIDs = map [uint64 ]struct {}{}
426
+ collectorID uint64 // All desc IDs XOR'd together.
427
+ collectorEscapedID uint64
423
428
)
424
429
go func () {
425
430
c .Describe (descChan )
@@ -429,8 +434,8 @@ func (r *Registry) Unregister(c Collector) bool {
429
434
if _ , exists := descIDs [desc .id ]; ! exists {
430
435
collectorID ^= desc .id
431
436
descIDs [desc .id ] = struct {}{}
432
- compatID ^= desc .compatID
433
- compatDescIDs [desc .compatID ] = struct {}{}
437
+ collectorEscapedID ^= desc .escapedID
438
+ escpaedDescIDs [desc .escapedID ] = struct {}{}
434
439
}
435
440
}
436
441
@@ -445,12 +450,12 @@ func (r *Registry) Unregister(c Collector) bool {
445
450
defer r .mtx .Unlock ()
446
451
447
452
delete (r .collectorsByID , collectorID )
448
- delete (r .collectorsByCompatID , compatID )
453
+ delete (r .collectorsByEscapedID , collectorEscapedID )
449
454
for id := range descIDs {
450
455
delete (r .descIDs , id )
451
456
}
452
- for id := range compatDescIDs {
453
- delete (r .compatDescIDs , id )
457
+ for id := range escpaedDescIDs {
458
+ delete (r .escapedDescIDs , id )
454
459
}
455
460
// dimHashesByName is left untouched as those must be consistent
456
461
// throughout the lifetime of a program.
0 commit comments