gauge.go 815 B

123456789101112131415161718192021222324252627282930313233343536
  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. // Force the compiler to check that StandardGauge implements Gauge.
  17. var _ Gauge = &StandardGauge{}
  18. // Create a new gauge.
  19. func NewGauge() *StandardGauge {
  20. return &StandardGauge{0}
  21. }
  22. // Update the gauge's value.
  23. func (g *StandardGauge) Update(v int64) {
  24. atomic.StoreInt64(&g.value, v)
  25. }
  26. // Return the gauge's current value.
  27. func (g *StandardGauge) Value() int64 {
  28. return atomic.LoadInt64(&g.value)
  29. }