You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.7 KiB

  1. package stats
  2. import (
  3. "time"
  4. )
  5. type ServerStats struct {
  6. Requests *DurationCounter
  7. Connections *DurationCounter
  8. AssignRequests *DurationCounter
  9. ReadRequests *DurationCounter
  10. WriteRequests *DurationCounter
  11. DeleteRequests *DurationCounter
  12. BytesIn *DurationCounter
  13. BytesOut *DurationCounter
  14. }
  15. type Channels struct {
  16. Connections chan *TimedValue
  17. Requests chan *TimedValue
  18. AssignRequests chan *TimedValue
  19. ReadRequests chan *TimedValue
  20. WriteRequests chan *TimedValue
  21. DeleteRequests chan *TimedValue
  22. BytesIn chan *TimedValue
  23. BytesOut chan *TimedValue
  24. }
  25. var (
  26. Chan *Channels
  27. )
  28. func init() {
  29. Chan = &Channels{
  30. Connections: make(chan *TimedValue, 100),
  31. Requests: make(chan *TimedValue, 100),
  32. AssignRequests: make(chan *TimedValue, 100),
  33. ReadRequests: make(chan *TimedValue, 100),
  34. WriteRequests: make(chan *TimedValue, 100),
  35. DeleteRequests: make(chan *TimedValue, 100),
  36. BytesIn: make(chan *TimedValue, 100),
  37. BytesOut: make(chan *TimedValue, 100),
  38. }
  39. }
  40. func NewServerStats() *ServerStats {
  41. return &ServerStats{
  42. Requests: NewDurationCounter(),
  43. Connections: NewDurationCounter(),
  44. AssignRequests: NewDurationCounter(),
  45. ReadRequests: NewDurationCounter(),
  46. WriteRequests: NewDurationCounter(),
  47. DeleteRequests: NewDurationCounter(),
  48. BytesIn: NewDurationCounter(),
  49. BytesOut: NewDurationCounter(),
  50. }
  51. }
  52. func ConnectionOpen() {
  53. Chan.Connections <- NewTimedValue(time.Now(), 1)
  54. }
  55. func ConnectionClose() {
  56. Chan.Connections <- NewTimedValue(time.Now(), -1)
  57. }
  58. func RequestOpen() {
  59. Chan.Requests <- NewTimedValue(time.Now(), 1)
  60. }
  61. func RequestClose() {
  62. Chan.Requests <- NewTimedValue(time.Now(), -1)
  63. }
  64. func AssignRequest() {
  65. Chan.AssignRequests <- NewTimedValue(time.Now(), 1)
  66. }
  67. func ReadRequest() {
  68. Chan.ReadRequests <- NewTimedValue(time.Now(), 1)
  69. }
  70. func WriteRequest() {
  71. Chan.WriteRequests <- NewTimedValue(time.Now(), 1)
  72. }
  73. func DeleteRequest() {
  74. Chan.DeleteRequests <- NewTimedValue(time.Now(), 1)
  75. }
  76. func BytesIn(val int64) {
  77. Chan.BytesIn <- NewTimedValue(time.Now(), val)
  78. }
  79. func BytesOut(val int64) {
  80. Chan.BytesOut <- NewTimedValue(time.Now(), val)
  81. }
  82. func (ss *ServerStats) Start() {
  83. for {
  84. select {
  85. case tv := <-Chan.Connections:
  86. ss.Connections.Add(tv)
  87. case tv := <-Chan.Requests:
  88. ss.Requests.Add(tv)
  89. case tv := <-Chan.AssignRequests:
  90. ss.AssignRequests.Add(tv)
  91. case tv := <-Chan.ReadRequests:
  92. ss.ReadRequests.Add(tv)
  93. case tv := <-Chan.WriteRequests:
  94. ss.WriteRequests.Add(tv)
  95. case tv := <-Chan.DeleteRequests:
  96. ss.DeleteRequests.Add(tv)
  97. case tv := <-Chan.BytesIn:
  98. ss.BytesIn.Add(tv)
  99. case tv := <-Chan.BytesOut:
  100. ss.BytesOut.Add(tv)
  101. }
  102. }
  103. }