meter.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // The standard implementation of a Meter uses a goroutine to synchronize
  17. // its calculations and another goroutine (via time.Ticker) to produce
  18. // clock ticks.
  19. type StandardMeter struct {
  20. in chan int64
  21. out chan meterV
  22. ticker *time.Ticker
  23. }
  24. // Force the compiler to check that StandardMeter implements Meter.
  25. var _ Meter = &StandardMeter{}
  26. // A meterV contains all the values that would need to be passed back
  27. // from the synchronizing goroutine.
  28. type meterV struct {
  29. count int64
  30. rate1, rate5, rate15, rateMean float64
  31. }
  32. // Create a new meter. Create the communication channels and start the
  33. // synchronizing goroutine.
  34. func NewMeter() *StandardMeter {
  35. m := &StandardMeter{
  36. make(chan int64),
  37. make(chan meterV),
  38. time.NewTicker(5e9),
  39. }
  40. go m.arbiter()
  41. return m
  42. }
  43. // Return the count of events seen.
  44. func (m *StandardMeter) Count() int64 {
  45. return (<-m.out).count
  46. }
  47. // Mark the occurance of n events.
  48. func (m *StandardMeter) Mark(n int64) {
  49. m.in <- n
  50. }
  51. // Return the meter's one-minute moving average rate of events.
  52. func (m *StandardMeter) Rate1() float64 {
  53. return (<-m.out).rate1
  54. }
  55. // Return the meter's five-minute moving average rate of events.
  56. func (m *StandardMeter) Rate5() float64 {
  57. return (<-m.out).rate5
  58. }
  59. // Return the meter's fifteen-minute moving average rate of events.
  60. func (m *StandardMeter) Rate15() float64 {
  61. return (<-m.out).rate15
  62. }
  63. // Return the meter's mean rate of events.
  64. func (m *StandardMeter) RateMean() float64 {
  65. return (<-m.out).rateMean
  66. }
  67. // Receive inputs and send outputs. Count each input and update the various
  68. // moving averages and the mean rate of events. Send a copy of the meterV
  69. // as output.
  70. func (m *StandardMeter) arbiter() {
  71. var mv meterV
  72. a1 := NewEWMA1()
  73. a5 := NewEWMA5()
  74. a15 := NewEWMA15()
  75. tsStart := time.Now()
  76. for {
  77. select {
  78. case n := <-m.in:
  79. mv.count += n
  80. a1.Update(n)
  81. mv.rate1 = a1.Rate()
  82. a5.Update(n)
  83. mv.rate5 = a5.Rate()
  84. a15.Update(n)
  85. mv.rate15 = a15.Rate()
  86. mv.rateMean = float64(1e9*mv.count) / float64(
  87. time.Now().Sub(tsStart))
  88. case m.out <- mv:
  89. case <-m.ticker.C:
  90. a1.Tick()
  91. a5.Tick()
  92. a15.Tick()
  93. }
  94. }
  95. }