gauge.go 715 B

123456789101112131415161718192021222324252627282930313233
  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.
  13. type StandardGauge struct {
  14. value int64
  15. }
  16. // Create a new gauge.
  17. func NewGauge() *StandardGauge {
  18. return &StandardGauge{0}
  19. }
  20. // Update the gauge's value.
  21. func (g *StandardGauge) Update(v int64) {
  22. atomic.StoreInt64(&g.value, v)
  23. }
  24. // Return the gauge's current value.
  25. func (g *StandardGauge) Value() int64 {
  26. return atomic.LoadInt64(&g.value)
  27. }