github.com/vccomnet/occchain@v0.0.0-20181129092339-c57d4bab23fb/metrics/gauge.go (about)

     1  package metrics
     2  
     3  import "sync/atomic"
     4  
     5  // Gauges hold an int64 value that can be set arbitrarily.
     6  type Gauge interface {
     7  	Snapshot() Gauge
     8  	Update(int64)
     9  	Value() int64
    10  }
    11  
    12  // GetOrRegisterGauge returns an existing Gauge or constructs and registers a
    13  // new StandardGauge.
    14  func GetOrRegisterGauge(name string, r Registry) Gauge {
    15  	if nil == r {
    16  		r = DefaultRegistry
    17  	}
    18  	return r.GetOrRegister(name, NewGauge).(Gauge)
    19  }
    20  
    21  // NewGauge constructs a new StandardGauge.
    22  func NewGauge() Gauge {
    23  	if !Enabled {
    24  		return NilGauge{}
    25  	}
    26  	return &StandardGauge{0}
    27  }
    28  
    29  // NewRegisteredGauge constructs and registers a new StandardGauge.
    30  func NewRegisteredGauge(name string, r Registry) Gauge {
    31  	c := NewGauge()
    32  	if nil == r {
    33  		r = DefaultRegistry
    34  	}
    35  	r.Register(name, c)
    36  	return c
    37  }
    38  
    39  // NewFunctionalGauge constructs a new FunctionalGauge.
    40  func NewFunctionalGauge(f func() int64) Gauge {
    41  	if !Enabled {
    42  		return NilGauge{}
    43  	}
    44  	return &FunctionalGauge{value: f}
    45  }
    46  
    47  // NewRegisteredFunctionalGauge constructs and registers a new StandardGauge.
    48  func NewRegisteredFunctionalGauge(name string, r Registry, f func() int64) Gauge {
    49  	c := NewFunctionalGauge(f)
    50  	if nil == r {
    51  		r = DefaultRegistry
    52  	}
    53  	r.Register(name, c)
    54  	return c
    55  }
    56  
    57  // GaugeSnapshot is a read-only copy of another Gauge.
    58  type GaugeSnapshot int64
    59  
    60  // Snapshot returns the snapshot.
    61  func (g GaugeSnapshot) Snapshot() Gauge { return g }
    62  
    63  // Update panics.
    64  func (GaugeSnapshot) Update(int64) {
    65  	panic("Update called on a GaugeSnapshot")
    66  }
    67  
    68  // Value returns the value at the time the snapshot was taken.
    69  func (g GaugeSnapshot) Value() int64 { return int64(g) }
    70  
    71  // NilGauge is a no-op Gauge.
    72  type NilGauge struct{}
    73  
    74  // Snapshot is a no-op.
    75  func (NilGauge) Snapshot() Gauge { return NilGauge{} }
    76  
    77  // Update is a no-op.
    78  func (NilGauge) Update(v int64) {}
    79  
    80  // Value is a no-op.
    81  func (NilGauge) Value() int64 { return 0 }
    82  
    83  // StandardGauge is the standard implementation of a Gauge and uses the
    84  // sync/atomic package to manage a single int64 value.
    85  type StandardGauge struct {
    86  	value int64
    87  }
    88  
    89  // Snapshot returns a read-only copy of the gauge.
    90  func (g *StandardGauge) Snapshot() Gauge {
    91  	return GaugeSnapshot(g.Value())
    92  }
    93  
    94  // Update updates the gauge's value.
    95  func (g *StandardGauge) Update(v int64) {
    96  	atomic.StoreInt64(&g.value, v)
    97  }
    98  
    99  // Value returns the gauge's current value.
   100  func (g *StandardGauge) Value() int64 {
   101  	return atomic.LoadInt64(&g.value)
   102  }
   103  
   104  // FunctionalGauge returns value from given function
   105  type FunctionalGauge struct {
   106  	value func() int64
   107  }
   108  
   109  // Value returns the gauge's current value.
   110  func (g FunctionalGauge) Value() int64 {
   111  	return g.value()
   112  }
   113  
   114  // Snapshot returns the snapshot.
   115  func (g FunctionalGauge) Snapshot() Gauge { return GaugeSnapshot(g.Value()) }
   116  
   117  // Update panics.
   118  func (FunctionalGauge) Update(int64) {
   119  	panic("Update called on a FunctionalGauge")
   120  }