gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/metrics/gauge.go (about)

     1  // Copyright 2018 The aquachain Authors
     2  // This file is part of the aquachain library.
     3  //
     4  // The aquachain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The aquachain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the aquachain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package metrics
    18  
    19  import "sync/atomic"
    20  
    21  // Gauges hold an int64 value that can be set arbitrarily.
    22  type Gauge interface {
    23  	Snapshot() Gauge
    24  	Update(int64)
    25  	Value() int64
    26  }
    27  
    28  // GetOrRegisterGauge returns an existing Gauge or constructs and registers a
    29  // new StandardGauge.
    30  func GetOrRegisterGauge(name string, r Registry) Gauge {
    31  	if nil == r {
    32  		r = DefaultRegistry
    33  	}
    34  	return r.GetOrRegister(name, NewGauge).(Gauge)
    35  }
    36  
    37  // NewGauge constructs a new StandardGauge.
    38  func NewGauge() Gauge {
    39  	if !Enabled {
    40  		return NilGauge{}
    41  	}
    42  	return &StandardGauge{0}
    43  }
    44  
    45  // NewRegisteredGauge constructs and registers a new StandardGauge.
    46  func NewRegisteredGauge(name string, r Registry) Gauge {
    47  	c := NewGauge()
    48  	if nil == r {
    49  		r = DefaultRegistry
    50  	}
    51  	r.Register(name, c)
    52  	return c
    53  }
    54  
    55  // NewFunctionalGauge constructs a new FunctionalGauge.
    56  func NewFunctionalGauge(f func() int64) Gauge {
    57  	if !Enabled {
    58  		return NilGauge{}
    59  	}
    60  	return &FunctionalGauge{value: f}
    61  }
    62  
    63  // NewRegisteredFunctionalGauge constructs and registers a new StandardGauge.
    64  func NewRegisteredFunctionalGauge(name string, r Registry, f func() int64) Gauge {
    65  	c := NewFunctionalGauge(f)
    66  	if nil == r {
    67  		r = DefaultRegistry
    68  	}
    69  	r.Register(name, c)
    70  	return c
    71  }
    72  
    73  // GaugeSnapshot is a read-only copy of another Gauge.
    74  type GaugeSnapshot int64
    75  
    76  // Snapshot returns the snapshot.
    77  func (g GaugeSnapshot) Snapshot() Gauge { return g }
    78  
    79  // Update panics.
    80  func (GaugeSnapshot) Update(int64) {
    81  	panic("Update called on a GaugeSnapshot")
    82  }
    83  
    84  // Value returns the value at the time the snapshot was taken.
    85  func (g GaugeSnapshot) Value() int64 { return int64(g) }
    86  
    87  // NilGauge is a no-op Gauge.
    88  type NilGauge struct{}
    89  
    90  // Snapshot is a no-op.
    91  func (NilGauge) Snapshot() Gauge { return NilGauge{} }
    92  
    93  // Update is a no-op.
    94  func (NilGauge) Update(v int64) {}
    95  
    96  // Value is a no-op.
    97  func (NilGauge) Value() int64 { return 0 }
    98  
    99  // StandardGauge is the standard implementation of a Gauge and uses the
   100  // sync/atomic package to manage a single int64 value.
   101  type StandardGauge struct {
   102  	value int64
   103  }
   104  
   105  // Snapshot returns a read-only copy of the gauge.
   106  func (g *StandardGauge) Snapshot() Gauge {
   107  	return GaugeSnapshot(g.Value())
   108  }
   109  
   110  // Update updates the gauge's value.
   111  func (g *StandardGauge) Update(v int64) {
   112  	atomic.StoreInt64(&g.value, v)
   113  }
   114  
   115  // Value returns the gauge's current value.
   116  func (g *StandardGauge) Value() int64 {
   117  	return atomic.LoadInt64(&g.value)
   118  }
   119  
   120  // FunctionalGauge returns value from given function
   121  type FunctionalGauge struct {
   122  	value func() int64
   123  }
   124  
   125  // Value returns the gauge's current value.
   126  func (g FunctionalGauge) Value() int64 {
   127  	return g.value()
   128  }
   129  
   130  // Snapshot returns the snapshot.
   131  func (g FunctionalGauge) Snapshot() Gauge { return GaugeSnapshot(g.Value()) }
   132  
   133  // Update panics.
   134  func (FunctionalGauge) Update(int64) {
   135  	panic("Update called on a FunctionalGauge")
   136  }