Skip to content

Commit 9877499

Browse files
committed
Refactor exporter functions to reuse unnamed variables if they already exist
Signed-off-by: Mohamed Hamza <[email protected]>
1 parent 643eec4 commit 9877499

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

go/stats/export.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ func (vg *varGroup) publish(name string, v expvar.Var) {
101101
vg.Lock()
102102
defer vg.Unlock()
103103

104-
// Ignore if the variable is already registered
105-
if v := expvar.Get(name); v != nil {
106-
return
107-
}
104+
// // Ignore if the variable is already registered
105+
// if v := expvar.Get(name); v != nil {
106+
// return
107+
// }
108108

109109
expvar.Publish(name, v)
110110
if vg.newVarHook != nil {

go/vt/servenv/exporter.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,12 @@ func (e *Exporter) NewGaugeFloat64(name string, help string) *stats.GaugeFloat64
326326
// NewCounterFunc creates a name-spaced equivalent for stats.NewCounterFunc.
327327
func (e *Exporter) NewCounterFunc(name string, help string, f func() int64) *stats.CounterFunc {
328328
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+
329335
v := stats.NewCounterFunc(name, help, f)
330336
addUnnamedExport(name, v)
331337
return v
@@ -338,6 +344,12 @@ func (e *Exporter) NewCounterFunc(name string, help string, f func() int64) *sta
338344
// NewGaugeFunc creates a name-spaced equivalent for stats.NewGaugeFunc.
339345
func (e *Exporter) NewGaugeFunc(name string, help string, f func() int64) *stats.GaugeFunc {
340346
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+
341353
v := stats.NewGaugeFunc(name, help, f)
342354
addUnnamedExport(name, v)
343355
return v
@@ -350,6 +362,12 @@ func (e *Exporter) NewGaugeFunc(name string, help string, f func() int64) *stats
350362
// NewCounterDurationFunc creates a name-spaced equivalent for stats.NewCounterDurationFunc.
351363
func (e *Exporter) NewCounterDurationFunc(name string, help string, f func() time.Duration) *stats.CounterDurationFunc {
352364
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+
353371
v := stats.NewCounterDurationFunc(name, help, f)
354372
addUnnamedExport(name, v)
355373
return v
@@ -362,6 +380,12 @@ func (e *Exporter) NewCounterDurationFunc(name string, help string, f func() tim
362380
// NewGaugeDurationFunc creates a name-spaced equivalent for stats.NewGaugeDurationFunc.
363381
func (e *Exporter) NewGaugeDurationFunc(name string, help string, f func() time.Duration) *stats.GaugeDurationFunc {
364382
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+
365389
v := stats.NewGaugeDurationFunc(name, help, f)
366390
addUnnamedExport(name, v)
367391
return v
@@ -436,6 +460,16 @@ func (e *Exporter) NewGaugesWithMultiLabels(name, help string, labels []string)
436460
// The function currently just returns an unexported variable.
437461
func (e *Exporter) NewTimings(name string, help string, label string) *TimingsWrapper {
438462
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+
439473
v := &TimingsWrapper{
440474
timings: stats.NewMultiTimings(name, help, []string{label}),
441475
}
@@ -731,3 +765,16 @@ func addUnnamedExport(name string, v expvar.Var) {
731765
func combineLabels(label string, labels []string) []string {
732766
return append(append(make([]string, 0, len(labels)+1), label), labels...)
733767
}
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

Comments
 (0)