...

Source file src/github.com/xanzy/go-gitlab/sidekiq_metrics.go

Documentation: github.com/xanzy/go-gitlab

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

View as plain text