meter.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package metrics
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. // Meters count events to produce exponentially-weighted moving average rates
  7. // at one-, five-, and fifteen-minutes and a mean rate.
  8. type Meter interface {
  9. Count() int64
  10. Mark(int64)
  11. Rate1() float64
  12. Rate5() float64
  13. Rate15() float64
  14. RateMean() float64
  15. Snapshot() Meter
  16. Stop()
  17. }
  18. // GetOrRegisterMeter returns an existing Meter or constructs and registers a
  19. // new StandardMeter.
  20. // Be sure to unregister the meter from the registry once it is of no use to
  21. // allow for garbage collection.
  22. func GetOrRegisterMeter(name string, r Registry) Meter {
  23. if nil == r {
  24. r = DefaultRegistry
  25. }
  26. return r.GetOrRegister(name, NewMeter).(Meter)
  27. }
  28. // GetOrRegisterMeterForced returns an existing Meter or constructs and registers a
  29. // new StandardMeter no matter the global switch is enabled or not.
  30. // Be sure to unregister the meter from the registry once it is of no use to
  31. // allow for garbage collection.
  32. func GetOrRegisterMeterForced(name string, r Registry) Meter {
  33. if nil == r {
  34. r = DefaultRegistry
  35. }
  36. return r.GetOrRegister(name, NewMeterForced).(Meter)
  37. }
  38. // NewMeter constructs a new StandardMeter and launches a goroutine.
  39. // Be sure to call Stop() once the meter is of no use to allow for garbage collection.
  40. func NewMeter() Meter {
  41. if !Enabled {
  42. return NilMeter{}
  43. }
  44. m := newStandardMeter()
  45. arbiter.Lock()
  46. defer arbiter.Unlock()
  47. arbiter.meters[m] = struct{}{}
  48. if !arbiter.started {
  49. arbiter.started = true
  50. go arbiter.tick()
  51. }
  52. return m
  53. }
  54. // NewMeterForced constructs a new StandardMeter and launches a goroutine no matter
  55. // the global switch is enabled or not.
  56. // Be sure to call Stop() once the meter is of no use to allow for garbage collection.
  57. func NewMeterForced() Meter {
  58. m := newStandardMeter()
  59. arbiter.Lock()
  60. defer arbiter.Unlock()
  61. arbiter.meters[m] = struct{}{}
  62. if !arbiter.started {
  63. arbiter.started = true
  64. go arbiter.tick()
  65. }
  66. return m
  67. }
  68. // NewRegisteredMeter constructs and registers a new StandardMeter
  69. // and launches a goroutine.
  70. // Be sure to unregister the meter from the registry once it is of no use to
  71. // allow for garbage collection.
  72. func NewRegisteredMeter(name string, r Registry) Meter {
  73. c := NewMeter()
  74. if nil == r {
  75. r = DefaultRegistry
  76. }
  77. r.Register(name, c)
  78. return c
  79. }
  80. // NewRegisteredMeterForced constructs and registers a new StandardMeter
  81. // and launches a goroutine no matter the global switch is enabled or not.
  82. // Be sure to unregister the meter from the registry once it is of no use to
  83. // allow for garbage collection.
  84. func NewRegisteredMeterForced(name string, r Registry) Meter {
  85. c := NewMeterForced()
  86. if nil == r {
  87. r = DefaultRegistry
  88. }
  89. r.Register(name, c)
  90. return c
  91. }
  92. // MeterSnapshot is a read-only copy of another Meter.
  93. type MeterSnapshot struct {
  94. count int64
  95. rate1, rate5, rate15, rateMean float64
  96. }
  97. // Count returns the count of events at the time the snapshot was taken.
  98. func (m *MeterSnapshot) Count() int64 { return m.count }
  99. // Mark panics.
  100. func (*MeterSnapshot) Mark(n int64) {
  101. panic("Mark called on a MeterSnapshot")
  102. }
  103. // Rate1 returns the one-minute moving average rate of events per second at the
  104. // time the snapshot was taken.
  105. func (m *MeterSnapshot) Rate1() float64 { return m.rate1 }
  106. // Rate5 returns the five-minute moving average rate of events per second at
  107. // the time the snapshot was taken.
  108. func (m *MeterSnapshot) Rate5() float64 { return m.rate5 }
  109. // Rate15 returns the fifteen-minute moving average rate of events per second
  110. // at the time the snapshot was taken.
  111. func (m *MeterSnapshot) Rate15() float64 { return m.rate15 }
  112. // RateMean returns the meter's mean rate of events per second at the time the
  113. // snapshot was taken.
  114. func (m *MeterSnapshot) RateMean() float64 { return m.rateMean }
  115. // Snapshot returns the snapshot.
  116. func (m *MeterSnapshot) Snapshot() Meter { return m }
  117. // Stop is a no-op.
  118. func (m *MeterSnapshot) Stop() {}
  119. // NilMeter is a no-op Meter.
  120. type NilMeter struct{}
  121. // Count is a no-op.
  122. func (NilMeter) Count() int64 { return 0 }
  123. // Mark is a no-op.
  124. func (NilMeter) Mark(n int64) {}
  125. // Rate1 is a no-op.
  126. func (NilMeter) Rate1() float64 { return 0.0 }
  127. // Rate5 is a no-op.
  128. func (NilMeter) Rate5() float64 { return 0.0 }
  129. // Rate15is a no-op.
  130. func (NilMeter) Rate15() float64 { return 0.0 }
  131. // RateMean is a no-op.
  132. func (NilMeter) RateMean() float64 { return 0.0 }
  133. // Snapshot is a no-op.
  134. func (NilMeter) Snapshot() Meter { return NilMeter{} }
  135. // Stop is a no-op.
  136. func (NilMeter) Stop() {}
  137. // StandardMeter is the standard implementation of a Meter.
  138. type StandardMeter struct {
  139. lock sync.RWMutex
  140. snapshot *MeterSnapshot
  141. a1, a5, a15 EWMA
  142. startTime time.Time
  143. stopped bool
  144. }
  145. func newStandardMeter() *StandardMeter {
  146. return &StandardMeter{
  147. snapshot: &MeterSnapshot{},
  148. a1: NewEWMA1(),
  149. a5: NewEWMA5(),
  150. a15: NewEWMA15(),
  151. startTime: time.Now(),
  152. }
  153. }
  154. // Stop stops the meter, Mark() will be a no-op if you use it after being stopped.
  155. func (m *StandardMeter) Stop() {
  156. m.lock.Lock()
  157. stopped := m.stopped
  158. m.stopped = true
  159. m.lock.Unlock()
  160. if !stopped {
  161. arbiter.Lock()
  162. delete(arbiter.meters, m)
  163. arbiter.Unlock()
  164. }
  165. }
  166. // Count returns the number of events recorded.
  167. func (m *StandardMeter) Count() int64 {
  168. m.lock.RLock()
  169. count := m.snapshot.count
  170. m.lock.RUnlock()
  171. return count
  172. }
  173. // Mark records the occurrence of n events.
  174. func (m *StandardMeter) Mark(n int64) {
  175. m.lock.Lock()
  176. defer m.lock.Unlock()
  177. if m.stopped {
  178. return
  179. }
  180. m.snapshot.count += n
  181. m.a1.Update(n)
  182. m.a5.Update(n)
  183. m.a15.Update(n)
  184. m.updateSnapshot()
  185. }
  186. // Rate1 returns the one-minute moving average rate of events per second.
  187. func (m *StandardMeter) Rate1() float64 {
  188. m.lock.RLock()
  189. rate1 := m.snapshot.rate1
  190. m.lock.RUnlock()
  191. return rate1
  192. }
  193. // Rate5 returns the five-minute moving average rate of events per second.
  194. func (m *StandardMeter) Rate5() float64 {
  195. m.lock.RLock()
  196. rate5 := m.snapshot.rate5
  197. m.lock.RUnlock()
  198. return rate5
  199. }
  200. // Rate15 returns the fifteen-minute moving average rate of events per second.
  201. func (m *StandardMeter) Rate15() float64 {
  202. m.lock.RLock()
  203. rate15 := m.snapshot.rate15
  204. m.lock.RUnlock()
  205. return rate15
  206. }
  207. // RateMean returns the meter's mean rate of events per second.
  208. func (m *StandardMeter) RateMean() float64 {
  209. m.lock.RLock()
  210. rateMean := m.snapshot.rateMean
  211. m.lock.RUnlock()
  212. return rateMean
  213. }
  214. // Snapshot returns a read-only copy of the meter.
  215. func (m *StandardMeter) Snapshot() Meter {
  216. m.lock.RLock()
  217. snapshot := *m.snapshot
  218. m.lock.RUnlock()
  219. return &snapshot
  220. }
  221. func (m *StandardMeter) updateSnapshot() {
  222. // should run with write lock held on m.lock
  223. snapshot := m.snapshot
  224. snapshot.rate1 = m.a1.Rate()
  225. snapshot.rate5 = m.a5.Rate()
  226. snapshot.rate15 = m.a15.Rate()
  227. snapshot.rateMean = float64(snapshot.count) / time.Since(m.startTime).Seconds()
  228. }
  229. func (m *StandardMeter) tick() {
  230. m.lock.Lock()
  231. defer m.lock.Unlock()
  232. m.a1.Tick()
  233. m.a5.Tick()
  234. m.a15.Tick()
  235. m.updateSnapshot()
  236. }
  237. // meterArbiter ticks meters every 5s from a single goroutine.
  238. // meters are references in a set for future stopping.
  239. type meterArbiter struct {
  240. sync.RWMutex
  241. started bool
  242. meters map[*StandardMeter]struct{}
  243. ticker *time.Ticker
  244. }
  245. var arbiter = meterArbiter{ticker: time.NewTicker(5e9), meters: make(map[*StandardMeter]struct{})}
  246. // Ticks meters on the scheduled interval
  247. func (ma *meterArbiter) tick() {
  248. for range ma.ticker.C {
  249. ma.tickMeters()
  250. }
  251. }
  252. func (ma *meterArbiter) tickMeters() {
  253. ma.RLock()
  254. defer ma.RUnlock()
  255. for meter := range ma.meters {
  256. meter.tick()
  257. }
  258. }