meter.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. //
  6. // This is an interface so as to encourage other structs to implement
  7. // the Meter API as appropriate.
  8. type Meter interface {
  9. Count() int64
  10. Mark(int64)
  11. Rate1() float64
  12. Rate5() float64
  13. Rate15() float64
  14. RateMean() float64
  15. }
  16. // Create a new Meter. Create the communication channels and start the
  17. // synchronizing goroutine.
  18. func NewMeter() Meter {
  19. if UseNilMetrics {
  20. return NilMeter{}
  21. }
  22. m := &StandardMeter{
  23. make(chan int64),
  24. make(chan meterV),
  25. time.NewTicker(5e9),
  26. }
  27. go m.arbiter()
  28. return m
  29. }
  30. // No-op Meter.
  31. type NilMeter struct{}
  32. // No-op.
  33. func (m NilMeter) Count() int64 { return 0 }
  34. // No-op.
  35. func (m NilMeter) Mark(n int64) {}
  36. // No-op.
  37. func (m NilMeter) Rate1() float64 { return 0.0 }
  38. // No-op.
  39. func (m NilMeter) Rate5() float64 { return 0.0 }
  40. // No-op.
  41. func (m NilMeter) Rate15() float64 { return 0.0 }
  42. // No-op.
  43. func (m NilMeter) RateMean() float64 { return 0.0 }
  44. // The standard implementation of a Meter uses a goroutine to synchronize
  45. // its calculations and another goroutine (via time.Ticker) to produce
  46. // clock ticks.
  47. type StandardMeter struct {
  48. in chan int64
  49. out chan meterV
  50. ticker *time.Ticker
  51. }
  52. // Return the count of events seen.
  53. func (m *StandardMeter) Count() int64 {
  54. return (<-m.out).count
  55. }
  56. // Mark the occurance of n events.
  57. func (m *StandardMeter) Mark(n int64) {
  58. m.in <- n
  59. }
  60. // Return the meter's one-minute moving average rate of events.
  61. func (m *StandardMeter) Rate1() float64 {
  62. return (<-m.out).rate1
  63. }
  64. // Return the meter's five-minute moving average rate of events.
  65. func (m *StandardMeter) Rate5() float64 {
  66. return (<-m.out).rate5
  67. }
  68. // Return the meter's fifteen-minute moving average rate of events.
  69. func (m *StandardMeter) Rate15() float64 {
  70. return (<-m.out).rate15
  71. }
  72. // Return the meter's mean rate of events.
  73. func (m *StandardMeter) RateMean() float64 {
  74. return (<-m.out).rateMean
  75. }
  76. // Receive inputs and send outputs. Count each input and update the various
  77. // moving averages and the mean rate of events. Send a copy of the meterV
  78. // as output.
  79. func (m *StandardMeter) arbiter() {
  80. var mv meterV
  81. a1 := NewEWMA1()
  82. a5 := NewEWMA5()
  83. a15 := NewEWMA15()
  84. t := time.Now()
  85. for {
  86. select {
  87. case n := <-m.in:
  88. mv.count += n
  89. a1.Update(n)
  90. a5.Update(n)
  91. a15.Update(n)
  92. mv.rate1 = a1.Rate()
  93. mv.rate5 = a5.Rate()
  94. mv.rate15 = a15.Rate()
  95. mv.rateMean = float64(1e9*mv.count) / float64(time.Since(t))
  96. case m.out <- mv:
  97. case <-m.ticker.C:
  98. a1.Tick()
  99. a5.Tick()
  100. a15.Tick()
  101. mv.rate1 = a1.Rate()
  102. mv.rate5 = a5.Rate()
  103. mv.rate15 = a15.Rate()
  104. mv.rateMean = float64(1e9*mv.count) / float64(time.Since(t))
  105. }
  106. }
  107. }
  108. // A meterV contains all the values that would need to be passed back
  109. // from the synchronizing goroutine.
  110. type meterV struct {
  111. count int64
  112. rate1, rate5, rate15, rateMean float64
  113. }