BigW Consortium Gitlab

sidekiq_metrics.rb 1.91 KB
Newer Older
Patricio Cano committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
require 'sidekiq/api'

module API
  class SidekiqMetrics < Grape::API
    before { authenticated_as_admin! }

    helpers do
      def queue_metrics
        Sidekiq::Queue.all.each_with_object({}) do |queue, hash|
          hash[queue.name] = {
            backlog: queue.size,
            latency: queue.latency.to_i
          }
        end
      end

      def process_metrics
        Sidekiq::ProcessSet.new.map do |process|
          {
            hostname:    process['hostname'],
            pid:         process['pid'],
            tag:         process['tag'],
            started_at:  Time.at(process['started_at']),
            queues:      process['queues'],
            labels:      process['labels'],
            concurrency: process['concurrency'],
            busy:        process['busy']
          }
        end
      end

      def job_stats
        stats = Sidekiq::Stats.new
        {
          processed: stats.processed,
          failed: stats.failed,
          enqueued: stats.enqueued
        }
      end
    end

    # Get Sidekiq Queue metrics
    #
    # Parameters:
    #   None
    #
    # Example:
    #   GET /sidekiq/queue_metrics
    #
    get 'sidekiq/queue_metrics' do
      { queues: queue_metrics }
    end

    # Get Sidekiq Process metrics
    #
    # Parameters:
    #   None
    #
    # Example:
    #   GET /sidekiq/process_metrics
    #
    get 'sidekiq/process_metrics' do
      { processes: process_metrics }
    end

    # Get Sidekiq Job statistics
    #
    # Parameters:
    #   None
    #
    # Example:
    #   GET /sidekiq/job_stats
    #
    get 'sidekiq/job_stats' do
      { jobs: job_stats }
    end

    # Get Sidekiq Compound metrics. Includes all previous metrics
    #
    # Parameters:
    #   None
    #
    # Example:
    #   GET /sidekiq/compound_metrics
    #
    get 'sidekiq/compound_metrics' do
      { queues: queue_metrics, processes: process_metrics, jobs: job_stats }
    end
  end
end