meter.go 3.3 KB

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