gauge.go 795 B

1234567891011121314151617181920212223242526272829303132333435
  1. package metrics
  2. import "sync/atomic"
  3. // Gauges hold an int64 value that can be set arbitrarily.
  4. //
  5. // This is an interface so as to encourage other structs to implement
  6. // the Gauge API as appropriate.
  7. type Gauge interface {
  8. Update(int64)
  9. Value() int64
  10. }
  11. // The standard implementation of a Gauge uses the sync/atomic package
  12. // to manage a single int64 value. When the latest weeklies land in a
  13. // release, atomic.LoadInt64 will be available and this code will become
  14. // safe on 32-bit architectures.
  15. type gauge struct {
  16. value int64
  17. }
  18. // Create a new gauge.
  19. func NewGauge() Gauge {
  20. return &gauge{0}
  21. }
  22. // Update the gauge's value.
  23. func (g *gauge) Update(v int64) {
  24. atomic.AddInt64(&g.value, v)
  25. }
  26. // Return the gauge's current value.
  27. func (g *gauge) Value() int64 {
  28. return g.value
  29. }