sidekiq_metrics.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // Copyright 2018, Sander van Harmelen
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. package gitlab
  17. import "time"
  18. // SidekiqService handles communication with the sidekiq service
  19. //
  20. // GitLab API docs: https://docs.gitlab.com/ce/api/sidekiq_metrics.html
  21. type SidekiqService struct {
  22. client *Client
  23. }
  24. // QueueMetrics represents the GitLab sidekiq queue metrics.
  25. //
  26. // GitLab API docs:
  27. // https://docs.gitlab.com/ce/api/sidekiq_metrics.html#get-the-current-queue-metrics
  28. type QueueMetrics struct {
  29. Queues map[string]struct {
  30. Backlog int `json:"backlog"`
  31. Latency int `json:"latency"`
  32. } `json:"queues"`
  33. }
  34. // GetQueueMetrics lists information about all the registered queues,
  35. // their backlog and their latency.
  36. //
  37. // GitLab API docs:
  38. // https://docs.gitlab.com/ce/api/sidekiq_metrics.html#get-the-current-queue-metrics
  39. func (s *SidekiqService) GetQueueMetrics(options ...OptionFunc) (*QueueMetrics, *Response, error) {
  40. req, err := s.client.NewRequest("GET", "/sidekiq/queue_metrics", nil, options)
  41. if err != nil {
  42. return nil, nil, err
  43. }
  44. q := new(QueueMetrics)
  45. resp, err := s.client.Do(req, q)
  46. if err != nil {
  47. return nil, resp, err
  48. }
  49. return q, resp, err
  50. }
  51. // ProcessMetrics represents the GitLab sidekiq process metrics.
  52. //
  53. // GitLab API docs:
  54. // https://docs.gitlab.com/ce/api/sidekiq_metrics.html#get-the-current-process-metrics
  55. type ProcessMetrics struct {
  56. Processes []struct {
  57. Hostname string `json:"hostname"`
  58. Pid int `json:"pid"`
  59. Tag string `json:"tag"`
  60. StartedAt *time.Time `json:"started_at"`
  61. Queues []string `json:"queues"`
  62. Labels []string `json:"labels"`
  63. Concurrency int `json:"concurrency"`
  64. Busy int `json:"busy"`
  65. } `json:"processes"`
  66. }
  67. // GetProcessMetrics lists information about all the Sidekiq workers registered
  68. // to process your queues.
  69. //
  70. // GitLab API docs:
  71. // https://docs.gitlab.com/ce/api/sidekiq_metrics.html#get-the-current-process-metrics
  72. func (s *SidekiqService) GetProcessMetrics(options ...OptionFunc) (*ProcessMetrics, *Response, error) {
  73. req, err := s.client.NewRequest("GET", "/sidekiq/process_metrics", nil, options)
  74. if err != nil {
  75. return nil, nil, err
  76. }
  77. p := new(ProcessMetrics)
  78. resp, err := s.client.Do(req, p)
  79. if err != nil {
  80. return nil, resp, err
  81. }
  82. return p, resp, err
  83. }
  84. // JobStats represents the GitLab sidekiq job stats.
  85. //
  86. // GitLab API docs:
  87. // https://docs.gitlab.com/ce/api/sidekiq_metrics.html#get-the-current-job-statistics
  88. type JobStats struct {
  89. Jobs struct {
  90. Processed int `json:"processed"`
  91. Failed int `json:"failed"`
  92. Enqueued int `json:"enqueued"`
  93. } `json:"jobs"`
  94. }
  95. // GetJobStats list information about the jobs that Sidekiq has performed.
  96. //
  97. // GitLab API docs:
  98. // https://docs.gitlab.com/ce/api/sidekiq_metrics.html#get-the-current-job-statistics
  99. func (s *SidekiqService) GetJobStats(options ...OptionFunc) (*JobStats, *Response, error) {
  100. req, err := s.client.NewRequest("GET", "/sidekiq/job_stats", nil, options)
  101. if err != nil {
  102. return nil, nil, err
  103. }
  104. j := new(JobStats)
  105. resp, err := s.client.Do(req, j)
  106. if err != nil {
  107. return nil, resp, err
  108. }
  109. return j, resp, err
  110. }
  111. // CompoundMetrics represents the GitLab sidekiq compounded stats.
  112. //
  113. // GitLab API docs:
  114. // https://docs.gitlab.com/ce/api/sidekiq_metrics.html#get-a-compound-response-of-all-the-previously-mentioned-metrics
  115. type CompoundMetrics struct {
  116. QueueMetrics
  117. ProcessMetrics
  118. JobStats
  119. }
  120. // GetCompoundMetrics lists all the currently available information about Sidekiq.
  121. // Get a compound response of all the previously mentioned metrics
  122. //
  123. // GitLab API docs: https://docs.gitlab.com/ce/api/sidekiq_metrics.html#get-the-current-job-statistics
  124. func (s *SidekiqService) GetCompoundMetrics(options ...OptionFunc) (*CompoundMetrics, *Response, error) {
  125. req, err := s.client.NewRequest("GET", "/sidekiq/compound_metrics", nil, options)
  126. if err != nil {
  127. return nil, nil, err
  128. }
  129. c := new(CompoundMetrics)
  130. resp, err := s.client.Do(req, c)
  131. if err != nil {
  132. return nil, resp, err
  133. }
  134. return c, resp, err
  135. }