meter.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package metrics
  2. import "time"
  3. // Meters count events to produce exponentially-weighted moving average rates
  4. // at one-, five-, and fifteen-minutes and a mean rate.
  5. type Meter interface {
  6. Count() int64
  7. Mark(int64)
  8. Rate1() float64
  9. Rate5() float64
  10. Rate15() float64
  11. RateMean() float64
  12. Snapshot() Meter
  13. }
  14. // GetOrRegisterMeter returns an existing Meter or constructs and registers a
  15. // new StandardMeter.
  16. func GetOrRegisterMeter(name string, r Registry) Meter {
  17. if nil == r {
  18. r = DefaultRegistry
  19. }
  20. return r.GetOrRegister(name, NewMeter).(Meter)
  21. }
  22. // NewMeter constructs a new StandardMeter and launches a goroutine.
  23. func NewMeter() Meter {
  24. if UseNilMetrics {
  25. return NilMeter{}
  26. }
  27. m := &StandardMeter{
  28. make(chan int64),
  29. make(chan *MeterSnapshot),
  30. time.NewTicker(5e9),
  31. }
  32. go m.arbiter()
  33. return m
  34. }
  35. // NewMeter constructs and registers a new StandardMeter and launches a
  36. // goroutine.
  37. func NewRegisteredMeter(name string, r Registry) Meter {
  38. c := NewMeter()
  39. if nil == r {
  40. r = DefaultRegistry
  41. }
  42. r.Register(name, c)
  43. return c
  44. }
  45. // MeterSnapshot is a read-only copy of another Meter.
  46. type MeterSnapshot struct {
  47. count int64
  48. rate1, rate5, rate15, rateMean float64
  49. }
  50. // Count returns the count of events at the time the snapshot was taken.
  51. func (m *MeterSnapshot) Count() int64 { return m.count }
  52. // Mark panics.
  53. func (*MeterSnapshot) Mark(n int64) {
  54. panic("Mark called on a MeterSnapshot")
  55. }
  56. // Rate1 returns the one-minute moving average rate of events per second at the
  57. // time the snapshot was taken.
  58. func (m *MeterSnapshot) Rate1() float64 { return m.rate1 }
  59. // Rate5 returns the five-minute moving average rate of events per second at
  60. // the time the snapshot was taken.
  61. func (m *MeterSnapshot) Rate5() float64 { return m.rate5 }
  62. // Rate15 returns the fifteen-minute moving average rate of events per second
  63. // at the time the snapshot was taken.
  64. func (m *MeterSnapshot) Rate15() float64 { return m.rate15 }
  65. // RateMean returns the meter's mean rate of events per second at the time the
  66. // snapshot was taken.
  67. func (m *MeterSnapshot) RateMean() float64 { return m.rateMean }
  68. // Snapshot returns the snapshot.
  69. func (m *MeterSnapshot) Snapshot() Meter { return m }
  70. // NilMeter is a no-op Meter.
  71. type NilMeter struct{}
  72. // Count is a no-op.
  73. func (NilMeter) Count() int64 { return 0 }
  74. // Mark is a no-op.
  75. func (NilMeter) Mark(n int64) {}
  76. // Rate1 is a no-op.
  77. func (NilMeter) Rate1() float64 { return 0.0 }
  78. // Rate5 is a no-op.
  79. func (NilMeter) Rate5() float64 { return 0.0 }
  80. // Rate15is a no-op.
  81. func (NilMeter) Rate15() float64 { return 0.0 }
  82. // RateMean is a no-op.
  83. func (NilMeter) RateMean() float64 { return 0.0 }
  84. // Snapshot is a no-op.
  85. func (NilMeter) Snapshot() Meter { return NilMeter{} }
  86. // StandardMeter is the standard implementation of a Meter and uses a
  87. // goroutine to synchronize its calculations and a time.Ticker to pass time.
  88. type StandardMeter struct {
  89. in chan int64
  90. out chan *MeterSnapshot
  91. ticker *time.Ticker
  92. }
  93. // Count returns the number of events recorded.
  94. func (m *StandardMeter) Count() int64 {
  95. return (<-m.out).count
  96. }
  97. // Mark records the occurance of n events.
  98. func (m *StandardMeter) Mark(n int64) {
  99. m.in <- n
  100. }
  101. // Rate1 returns the one-minute moving average rate of events per second.
  102. func (m *StandardMeter) Rate1() float64 {
  103. return (<-m.out).rate1
  104. }
  105. // Rate5 returns the five-minute moving average rate of events per second.
  106. func (m *StandardMeter) Rate5() float64 {
  107. return (<-m.out).rate5
  108. }
  109. // Rate15 returns the fifteen-minute moving average rate of events per second.
  110. func (m *StandardMeter) Rate15() float64 {
  111. return (<-m.out).rate15
  112. }
  113. // RateMean returns the meter's mean rate of events per second.
  114. func (m *StandardMeter) RateMean() float64 {
  115. return (<-m.out).rateMean
  116. }
  117. // Snapshot returns a read-only copy of the meter.
  118. func (m *StandardMeter) Snapshot() Meter {
  119. snapshot := *<-m.out
  120. return &snapshot
  121. }
  122. // arbiter receives inputs and sends outputs. It counts each input and updates
  123. // the various moving averages and the mean rate of events. It sends a copy of
  124. // the meterV as output.
  125. func (m *StandardMeter) arbiter() {
  126. snapshot := &MeterSnapshot{}
  127. a1 := NewEWMA1()
  128. a5 := NewEWMA5()
  129. a15 := NewEWMA15()
  130. t := time.Now()
  131. for {
  132. select {
  133. case n := <-m.in:
  134. snapshot.count += n
  135. a1.Update(n)
  136. a5.Update(n)
  137. a15.Update(n)
  138. snapshot.rate1 = a1.Rate()
  139. snapshot.rate5 = a5.Rate()
  140. snapshot.rate15 = a15.Rate()
  141. snapshot.rateMean = float64(1e9*snapshot.count) / float64(time.Since(t))
  142. case m.out <- snapshot:
  143. case <-m.ticker.C:
  144. a1.Tick()
  145. a5.Tick()
  146. a15.Tick()
  147. snapshot.rate1 = a1.Rate()
  148. snapshot.rate5 = a5.Rate()
  149. snapshot.rate15 = a15.Rate()
  150. snapshot.rateMean = float64(1e9*snapshot.count) / float64(time.Since(t))
  151. }
  152. }
  153. }