sidekiq_metrics.go

  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
 17package gitlab
 18
 19import "time"
 20
 21// SidekiqService handles communication with the sidekiq service
 22//
 23// GitLab API docs: https://docs.gitlab.com/ce/api/sidekiq_metrics.html
 24type SidekiqService struct {
 25	client *Client
 26}
 27
 28// QueueMetrics represents the GitLab sidekiq queue metrics.
 29//
 30// GitLab API docs:
 31// https://docs.gitlab.com/ce/api/sidekiq_metrics.html#get-the-current-queue-metrics
 32type QueueMetrics struct {
 33	Queues map[string]struct {
 34		Backlog int `json:"backlog"`
 35		Latency int `json:"latency"`
 36	} `json:"queues"`
 37}
 38
 39// GetQueueMetrics lists information about all the registered queues,
 40// their backlog and their latency.
 41//
 42// GitLab API docs:
 43// https://docs.gitlab.com/ce/api/sidekiq_metrics.html#get-the-current-queue-metrics
 44func (s *SidekiqService) GetQueueMetrics(options ...OptionFunc) (*QueueMetrics, *Response, error) {
 45	req, err := s.client.NewRequest("GET", "/sidekiq/queue_metrics", nil, options)
 46	if err != nil {
 47		return nil, nil, err
 48	}
 49
 50	q := new(QueueMetrics)
 51	resp, err := s.client.Do(req, q)
 52	if err != nil {
 53		return nil, resp, err
 54	}
 55
 56	return q, resp, err
 57}
 58
 59// ProcessMetrics represents the GitLab sidekiq process metrics.
 60//
 61// GitLab API docs:
 62// https://docs.gitlab.com/ce/api/sidekiq_metrics.html#get-the-current-process-metrics
 63type ProcessMetrics struct {
 64	Processes []struct {
 65		Hostname    string     `json:"hostname"`
 66		Pid         int        `json:"pid"`
 67		Tag         string     `json:"tag"`
 68		StartedAt   *time.Time `json:"started_at"`
 69		Queues      []string   `json:"queues"`
 70		Labels      []string   `json:"labels"`
 71		Concurrency int        `json:"concurrency"`
 72		Busy        int        `json:"busy"`
 73	} `json:"processes"`
 74}
 75
 76// GetProcessMetrics lists information about all the Sidekiq workers registered
 77// to process your queues.
 78//
 79// GitLab API docs:
 80// https://docs.gitlab.com/ce/api/sidekiq_metrics.html#get-the-current-process-metrics
 81func (s *SidekiqService) GetProcessMetrics(options ...OptionFunc) (*ProcessMetrics, *Response, error) {
 82	req, err := s.client.NewRequest("GET", "/sidekiq/process_metrics", nil, options)
 83	if err != nil {
 84		return nil, nil, err
 85	}
 86
 87	p := new(ProcessMetrics)
 88	resp, err := s.client.Do(req, p)
 89	if err != nil {
 90		return nil, resp, err
 91	}
 92
 93	return p, resp, err
 94}
 95
 96// JobStats represents the GitLab sidekiq job stats.
 97//
 98// GitLab API docs:
 99// https://docs.gitlab.com/ce/api/sidekiq_metrics.html#get-the-current-job-statistics
100type JobStats struct {
101	Jobs struct {
102		Processed int `json:"processed"`
103		Failed    int `json:"failed"`
104		Enqueued  int `json:"enqueued"`
105	} `json:"jobs"`
106}
107
108// GetJobStats list information about the jobs that Sidekiq has performed.
109//
110// GitLab API docs:
111// https://docs.gitlab.com/ce/api/sidekiq_metrics.html#get-the-current-job-statistics
112func (s *SidekiqService) GetJobStats(options ...OptionFunc) (*JobStats, *Response, error) {
113	req, err := s.client.NewRequest("GET", "/sidekiq/job_stats", nil, options)
114	if err != nil {
115		return nil, nil, err
116	}
117
118	j := new(JobStats)
119	resp, err := s.client.Do(req, j)
120	if err != nil {
121		return nil, resp, err
122	}
123
124	return j, resp, err
125}
126
127// CompoundMetrics represents the GitLab sidekiq compounded stats.
128//
129// GitLab API docs:
130// https://docs.gitlab.com/ce/api/sidekiq_metrics.html#get-a-compound-response-of-all-the-previously-mentioned-metrics
131type CompoundMetrics struct {
132	QueueMetrics
133	ProcessMetrics
134	JobStats
135}
136
137// GetCompoundMetrics lists all the currently available information about Sidekiq.
138// Get a compound response of all the previously mentioned metrics
139//
140// GitLab API docs: https://docs.gitlab.com/ce/api/sidekiq_metrics.html#get-the-current-job-statistics
141func (s *SidekiqService) GetCompoundMetrics(options ...OptionFunc) (*CompoundMetrics, *Response, error) {
142	req, err := s.client.NewRequest("GET", "/sidekiq/compound_metrics", nil, options)
143	if err != nil {
144		return nil, nil, err
145	}
146
147	c := new(CompoundMetrics)
148	resp, err := s.client.Do(req, c)
149	if err != nil {
150		return nil, resp, err
151	}
152
153	return c, resp, err
154}