gauge.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. Dec(int64)
  8. Inc(int64)
  9. Value() int64
  10. }
  11. // GetOrRegisterGauge returns an existing Gauge or constructs and registers a
  12. // new StandardGauge.
  13. func GetOrRegisterGauge(name string, r Registry) Gauge {
  14. if nil == r {
  15. r = DefaultRegistry
  16. }
  17. return r.GetOrRegister(name, NewGauge).(Gauge)
  18. }
  19. // NewGauge constructs a new StandardGauge.
  20. func NewGauge() Gauge {
  21. if !Enabled {
  22. return NilGauge{}
  23. }
  24. return &StandardGauge{0}
  25. }
  26. // NewRegisteredGauge constructs and registers a new StandardGauge.
  27. func NewRegisteredGauge(name string, r Registry) Gauge {
  28. c := NewGauge()
  29. if nil == r {
  30. r = DefaultRegistry
  31. }
  32. r.Register(name, c)
  33. return c
  34. }
  35. // NewFunctionalGauge constructs a new FunctionalGauge.
  36. func NewFunctionalGauge(f func() int64) Gauge {
  37. if !Enabled {
  38. return NilGauge{}
  39. }
  40. return &FunctionalGauge{value: f}
  41. }
  42. // NewRegisteredFunctionalGauge constructs and registers a new StandardGauge.
  43. func NewRegisteredFunctionalGauge(name string, r Registry, f func() int64) Gauge {
  44. c := NewFunctionalGauge(f)
  45. if nil == r {
  46. r = DefaultRegistry
  47. }
  48. r.Register(name, c)
  49. return c
  50. }
  51. // GaugeSnapshot is a read-only copy of another Gauge.
  52. type GaugeSnapshot int64
  53. // Snapshot returns the snapshot.
  54. func (g GaugeSnapshot) Snapshot() Gauge { return g }
  55. // Update panics.
  56. func (GaugeSnapshot) Update(int64) {
  57. panic("Update called on a GaugeSnapshot")
  58. }
  59. // Dec panics.
  60. func (GaugeSnapshot) Dec(int64) {
  61. panic("Dec called on a GaugeSnapshot")
  62. }
  63. // Inc panics.
  64. func (GaugeSnapshot) Inc(int64) {
  65. panic("Inc called on a GaugeSnapshot")
  66. }
  67. // Value returns the value at the time the snapshot was taken.
  68. func (g GaugeSnapshot) Value() int64 { return int64(g) }
  69. // NilGauge is a no-op Gauge.
  70. type NilGauge struct{}
  71. // Snapshot is a no-op.
  72. func (NilGauge) Snapshot() Gauge { return NilGauge{} }
  73. // Update is a no-op.
  74. func (NilGauge) Update(v int64) {}
  75. // Dec is a no-op.
  76. func (NilGauge) Dec(i int64) {}
  77. // Inc is a no-op.
  78. func (NilGauge) Inc(i int64) {}
  79. // Value is a no-op.
  80. func (NilGauge) Value() int64 { return 0 }
  81. // StandardGauge is the standard implementation of a Gauge and uses the
  82. // sync/atomic package to manage a single int64 value.
  83. type StandardGauge struct {
  84. value int64
  85. }
  86. // Snapshot returns a read-only copy of the gauge.
  87. func (g *StandardGauge) Snapshot() Gauge {
  88. return GaugeSnapshot(g.Value())
  89. }
  90. // Update updates the gauge's value.
  91. func (g *StandardGauge) Update(v int64) {
  92. atomic.StoreInt64(&g.value, v)
  93. }
  94. // Value returns the gauge's current value.
  95. func (g *StandardGauge) Value() int64 {
  96. return atomic.LoadInt64(&g.value)
  97. }
  98. // Dec decrements the gauge's current value by the given amount.
  99. func (g *StandardGauge) Dec(i int64) {
  100. atomic.AddInt64(&g.value, -i)
  101. }
  102. // Inc increments the gauge's current value by the given amount.
  103. func (g *StandardGauge) Inc(i int64) {
  104. atomic.AddInt64(&g.value, i)
  105. }
  106. // FunctionalGauge returns value from given function
  107. type FunctionalGauge struct {
  108. value func() int64
  109. }
  110. // Value returns the gauge's current value.
  111. func (g FunctionalGauge) Value() int64 {
  112. return g.value()
  113. }
  114. // Snapshot returns the snapshot.
  115. func (g FunctionalGauge) Snapshot() Gauge { return GaugeSnapshot(g.Value()) }
  116. // Update panics.
  117. func (FunctionalGauge) Update(int64) {
  118. panic("Update called on a FunctionalGauge")
  119. }
  120. // Dec panics.
  121. func (FunctionalGauge) Dec(int64) {
  122. panic("Dec called on a FunctionalGauge")
  123. }
  124. // Inc panics.
  125. func (FunctionalGauge) Inc(int64) {
  126. panic("Inc called on a FunctionalGauge")
  127. }