@@ -326,6 +326,12 @@ func (e *Exporter) NewGaugeFloat64(name string, help string) *stats.GaugeFloat64
326
326
// NewCounterFunc creates a name-spaced equivalent for stats.NewCounterFunc.
327
327
func (e * Exporter ) NewCounterFunc (name string , help string , f func () int64 ) * stats.CounterFunc {
328
328
if e .name == "" || name == "" {
329
+ // Before exporting a new unnamed variable, make sure it doesn't already exist,
330
+ // and reuse it if it does.
331
+ if v := unnamedExports [name ]; v != nil {
332
+ return v .(* stats.CounterFunc )
333
+ }
334
+
329
335
v := stats .NewCounterFunc (name , help , f )
330
336
addUnnamedExport (name , v )
331
337
return v
@@ -338,6 +344,12 @@ func (e *Exporter) NewCounterFunc(name string, help string, f func() int64) *sta
338
344
// NewGaugeFunc creates a name-spaced equivalent for stats.NewGaugeFunc.
339
345
func (e * Exporter ) NewGaugeFunc (name string , help string , f func () int64 ) * stats.GaugeFunc {
340
346
if e .name == "" || name == "" {
347
+ // Before exporting a new unnamed variable, make sure it doesn't already exist,
348
+ // and reuse it if it does.
349
+ if v := unnamedExports [name ]; v != nil {
350
+ return v .(* stats.GaugeFunc )
351
+ }
352
+
341
353
v := stats .NewGaugeFunc (name , help , f )
342
354
addUnnamedExport (name , v )
343
355
return v
@@ -350,6 +362,12 @@ func (e *Exporter) NewGaugeFunc(name string, help string, f func() int64) *stats
350
362
// NewCounterDurationFunc creates a name-spaced equivalent for stats.NewCounterDurationFunc.
351
363
func (e * Exporter ) NewCounterDurationFunc (name string , help string , f func () time.Duration ) * stats.CounterDurationFunc {
352
364
if e .name == "" || name == "" {
365
+ // Before exporting a new unnamed variable, make sure it doesn't already exist,
366
+ // and reuse it if it does.
367
+ if v := unnamedExports [name ]; v != nil {
368
+ return v .(* stats.CounterDurationFunc )
369
+ }
370
+
353
371
v := stats .NewCounterDurationFunc (name , help , f )
354
372
addUnnamedExport (name , v )
355
373
return v
@@ -362,6 +380,12 @@ func (e *Exporter) NewCounterDurationFunc(name string, help string, f func() tim
362
380
// NewGaugeDurationFunc creates a name-spaced equivalent for stats.NewGaugeDurationFunc.
363
381
func (e * Exporter ) NewGaugeDurationFunc (name string , help string , f func () time.Duration ) * stats.GaugeDurationFunc {
364
382
if e .name == "" || name == "" {
383
+ // Before exporting a new unnamed variable, make sure it doesn't already exist,
384
+ // and reuse it if it does.
385
+ if v := unnamedExports [name ]; v != nil {
386
+ return v .(* stats.GaugeDurationFunc )
387
+ }
388
+
365
389
v := stats .NewGaugeDurationFunc (name , help , f )
366
390
addUnnamedExport (name , v )
367
391
return v
@@ -436,6 +460,16 @@ func (e *Exporter) NewGaugesWithMultiLabels(name, help string, labels []string)
436
460
// The function currently just returns an unexported variable.
437
461
func (e * Exporter ) NewTimings (name string , help string , label string ) * TimingsWrapper {
438
462
if e .name == "" || name == "" {
463
+ // Before exporting a new unnamed variable, make sure it doesn't already exist,
464
+ // and reuse it if it does.
465
+ if v := unnamedExports [name ]; v != nil {
466
+ tw := & TimingsWrapper {
467
+ timings : v .(* stats.MultiTimings ),
468
+ }
469
+
470
+ return tw
471
+ }
472
+
439
473
v := & TimingsWrapper {
440
474
timings : stats .NewMultiTimings (name , help , []string {label }),
441
475
}
@@ -731,3 +765,16 @@ func addUnnamedExport(name string, v expvar.Var) {
731
765
func combineLabels (label string , labels []string ) []string {
732
766
return append (append (make ([]string , 0 , len (labels )+ 1 ), label ), labels ... )
733
767
}
768
+
769
+ func timing (name string ) * TimingsWrapper {
770
+ exporterMu .Lock ()
771
+ defer exporterMu .Unlock ()
772
+
773
+ if v , ok := unnamedExports [name ]; ok {
774
+ return & TimingsWrapper {
775
+ timings : v .(* stats.MultiTimings ),
776
+ }
777
+ }
778
+
779
+ return nil
780
+ }
0 commit comments