github.com/anycable/anycable-go@v1.5.1/metrics/gauge.go (about)

     1  package metrics
     2  
     3  import (
     4  	"sync/atomic"
     5  )
     6  
     7  // Gauge stores an int value
     8  type Gauge struct {
     9  	value uint64
    10  	name  string
    11  	desc  string
    12  }
    13  
    14  // NewGauge initializes Gauge.
    15  func NewGauge(name string, desc string) *Gauge {
    16  	return &Gauge{name: name, desc: desc, value: 0}
    17  }
    18  
    19  // Name returns gauge name
    20  func (g *Gauge) Name() string {
    21  	return g.name
    22  }
    23  
    24  // Desc returns gauge description
    25  func (g *Gauge) Desc() string {
    26  	return g.desc
    27  }
    28  
    29  // Set gauge value
    30  func (g *Gauge) Set(value int) {
    31  	atomic.StoreUint64(&g.value, uint64(value))
    32  }
    33  
    34  // Inc increment the current value by 1
    35  func (g *Gauge) Inc() uint64 {
    36  	return atomic.AddUint64(&g.value, 1)
    37  }
    38  
    39  // Dec decrement the current value by 1
    40  func (g *Gauge) Dec() uint64 {
    41  	return atomic.AddUint64(&g.value, ^uint64(0))
    42  }
    43  
    44  // Set64 sets gauge value as uint64
    45  func (g *Gauge) Set64(value uint64) {
    46  	atomic.StoreUint64(&g.value, value)
    47  }
    48  
    49  // Value returns the current gauge value
    50  func (g *Gauge) Value() uint64 {
    51  	return atomic.LoadUint64(&g.value)
    52  }