gauge.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package metrics
  2. import "sync/atomic"
  3. // Gauges hold an int64 value that can be set arbitrarily.
  4. type Gauge interface {
  5. Snapshot() Gauge
  6. Update(int64)
  7. Value() int64
  8. }
  9. // GetOrRegisterGauge returns an existing Gauge or constructs and registers a
  10. // new StandardGauge.
  11. func GetOrRegisterGauge(name string, r Registry) Gauge {
  12. if nil == r {
  13. r = DefaultRegistry
  14. }
  15. return r.GetOrRegister(name, NewGauge).(Gauge)
  16. }
  17. // NewGauge constructs a new StandardGauge.
  18. func NewGauge() Gauge {
  19. if UseNilMetrics {
  20. return NilGauge{}
  21. }
  22. return &StandardGauge{0}
  23. }
  24. // NewRegisteredGauge constructs and registers a new StandardGauge.
  25. func NewRegisteredGauge(name string, r Registry) Gauge {
  26. c := NewGauge()
  27. if nil == r {
  28. r = DefaultRegistry
  29. }
  30. r.Register(name, c)
  31. return c
  32. }
  33. // NewFunctionalGauge constructs a new FunctionalGauge.
  34. func NewFunctionalGauge(f func() int64) Gauge {
  35. if UseNilMetrics {
  36. return NilGauge{}
  37. }
  38. return &FunctionalGauge{value: f}
  39. }
  40. // NewRegisteredFunctionalGauge constructs and registers a new StandardGauge.
  41. func NewRegisteredFunctionalGauge(name string, r Registry, f func() int64) Gauge {
  42. c := NewFunctionalGauge(f)
  43. if nil == r {
  44. r = DefaultRegistry
  45. }
  46. r.Register(name, c)
  47. return c
  48. }
  49. // GaugeSnapshot is a read-only copy of another Gauge.
  50. type GaugeSnapshot int64
  51. // Snapshot returns the snapshot.
  52. func (g GaugeSnapshot) Snapshot() Gauge { return g }
  53. // Update panics.
  54. func (GaugeSnapshot) Update(int64) {
  55. panic("Update called on a GaugeSnapshot")
  56. }
  57. // Value returns the value at the time the snapshot was taken.
  58. func (g GaugeSnapshot) Value() int64 { return int64(g) }
  59. // NilGauge is a no-op Gauge.
  60. type NilGauge struct{}
  61. // Snapshot is a no-op.
  62. func (NilGauge) Snapshot() Gauge { return NilGauge{} }
  63. // Update is a no-op.
  64. func (NilGauge) Update(v int64) {}
  65. // Value is a no-op.
  66. func (NilGauge) Value() int64 { return 0 }
  67. // StandardGauge is the standard implementation of a Gauge and uses the
  68. // sync/atomic package to manage a single int64 value.
  69. type StandardGauge struct {
  70. value int64
  71. }
  72. // Snapshot returns a read-only copy of the gauge.
  73. func (g *StandardGauge) Snapshot() Gauge {
  74. return GaugeSnapshot(g.Value())
  75. }
  76. // Update updates the gauge's value.
  77. func (g *StandardGauge) Update(v int64) {
  78. atomic.StoreInt64(&g.value, v)
  79. }
  80. // Value returns the gauge's current value.
  81. func (g *StandardGauge) Value() int64 {
  82. return atomic.LoadInt64(&g.value)
  83. }
  84. // FunctionalGauge returns value from given function
  85. type FunctionalGauge struct {
  86. value func() int64
  87. }
  88. // Value returns the gauge's current value.
  89. func (g FunctionalGauge) Value() int64 {
  90. return g.value()
  91. }
  92. // Snapshot returns the snapshot.
  93. func (g FunctionalGauge) Snapshot() Gauge { return GaugeSnapshot(g.Value()) }
  94. // Update panics.
  95. func (FunctionalGauge) Update(int64) {
  96. panic("Update called on a FunctionalGauge")
  97. }