meter.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. }
  13. // GetOrRegisterMeter returns an existing Meter or constructs and registers a
  14. // new StandardMeter.
  15. func GetOrRegisterMeter(name string, r Registry) Meter {
  16. if nil == r {
  17. r = DefaultRegistry
  18. }
  19. return r.GetOrRegister(name, NewMeter()).(Meter)
  20. }
  21. // NewMeter constructs a new StandardMeter and launches a goroutine.
  22. func NewMeter() Meter {
  23. if UseNilMetrics {
  24. return NilMeter{}
  25. }
  26. m := &StandardMeter{
  27. make(chan int64),
  28. make(chan *MeterSnapshot),
  29. time.NewTicker(5e9),
  30. }
  31. go m.arbiter()
  32. return m
  33. }
  34. // NewMeter constructs and registers a new StandardMeter and launches a
  35. // goroutine.
  36. func NewRegisteredMeter(name string, r Registry) Meter {
  37. c := NewMeter()
  38. if nil == r {
  39. r = DefaultRegistry
  40. }
  41. r.Register(name, c)
  42. return c
  43. }
  44. // No-op Meter.
  45. type NilMeter struct{}
  46. // Count is a no-op.
  47. func (NilMeter) Count() int64 { return 0 }
  48. // Mark is a no-op.
  49. func (NilMeter) Mark(n int64) {}
  50. // Rate1 is a no-op.
  51. func (NilMeter) Rate1() float64 { return 0.0 }
  52. // Rate5 is a no-op.
  53. func (NilMeter) Rate5() float64 { return 0.0 }
  54. // Rate15is a no-op.
  55. func (NilMeter) Rate15() float64 { return 0.0 }
  56. // RateMean is a no-op.
  57. func (NilMeter) RateMean() float64 { return 0.0 }
  58. // The standard implementation of a Meter uses a goroutine to synchronize
  59. // its calculations and another goroutine (via time.Ticker) to produce
  60. // clock ticks.
  61. type StandardMeter struct {
  62. in chan int64
  63. out chan meterV
  64. ticker *time.Ticker
  65. }
  66. // Count returns the number of events recorded.
  67. func (m *StandardMeter) Count() int64 {
  68. return (<-m.out).count
  69. }
  70. // Mark records the occurance of n events.
  71. func (m *StandardMeter) Mark(n int64) {
  72. m.in <- n
  73. }
  74. // Rate1 returns the one-minute moving average rate of events per second.
  75. func (m *StandardMeter) Rate1() float64 {
  76. return (<-m.out).rate1
  77. }
  78. // Rate5 returns the five-minute moving average rate of events per second.
  79. func (m *StandardMeter) Rate5() float64 {
  80. return (<-m.out).rate5
  81. }
  82. // Rate15 returns the fifteen-minute moving average rate of events per second.
  83. func (m *StandardMeter) Rate15() float64 {
  84. return (<-m.out).rate15
  85. }
  86. // RateMean returns the meter's mean rate of events per second.
  87. func (m *StandardMeter) RateMean() float64 {
  88. return (<-m.out).rateMean
  89. }
  90. // Receive inputs and send outputs. Count each input and update the various
  91. // moving averages and the mean rate of events. Send a copy of the meterV
  92. // as output.
  93. func (m *StandardMeter) arbiter() {
  94. var mv meterV
  95. a1 := NewEWMA1()
  96. a5 := NewEWMA5()
  97. a15 := NewEWMA15()
  98. t := time.Now()
  99. for {
  100. select {
  101. case n := <-m.in:
  102. mv.count += n
  103. a1.Update(n)
  104. a5.Update(n)
  105. a15.Update(n)
  106. mv.rate1 = a1.Rate()
  107. mv.rate5 = a5.Rate()
  108. mv.rate15 = a15.Rate()
  109. mv.rateMean = float64(1e9*mv.count) / float64(time.Since(t))
  110. case m.out <- mv:
  111. case <-m.ticker.C:
  112. a1.Tick()
  113. a5.Tick()
  114. a15.Tick()
  115. mv.rate1 = a1.Rate()
  116. mv.rate5 = a5.Rate()
  117. mv.rate15 = a15.Rate()
  118. mv.rateMean = float64(1e9*mv.count) / float64(time.Since(t))
  119. }
  120. }
  121. }
  122. // A meterV contains all the values that would need to be passed back
  123. // from the synchronizing goroutine.
  124. type meterV struct {
  125. count int64
  126. rate1, rate5, rate15, rateMean float64
  127. }