github.com/yandex/pandora@v0.5.32/lib/monitoring/counter.go (about)

     1  package monitoring
     2  
     3  import (
     4  	"expvar"
     5  	"strconv"
     6  
     7  	"go.uber.org/atomic"
     8  )
     9  
    10  // TODO: use one rcrowley/go-metrics instead.
    11  
    12  type Counter struct {
    13  	i atomic.Int64
    14  }
    15  
    16  var _ expvar.Var = (*Counter)(nil)
    17  
    18  func (c *Counter) String() string {
    19  	return strconv.FormatInt(c.i.Load(), 10)
    20  }
    21  
    22  // Add adds given delta to a counter value
    23  func (c *Counter) Add(delta int64) {
    24  	c.i.Add(delta)
    25  }
    26  
    27  // Set sets given value as counter value
    28  func (c *Counter) Set(value int64) {
    29  	c.i.Store(value)
    30  }
    31  
    32  // Get returns counter value
    33  func (c *Counter) Get() int64 {
    34  	return c.i.Load()
    35  }
    36  
    37  func NewCounter(name string) *Counter {
    38  	v := &Counter{}
    39  	expvar.Publish(name, v)
    40  	return v
    41  }