Sha256: 685ac92d19060676d737e9fd68eb76cd8b72734a9f2335ad05a1f19b62918a4e

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module PrometheusExporter::Instrumentation
  class SidekiqQueue
    def self.start(client: nil, frequency: 30)
      client ||= PrometheusExporter::Client.default
      sidekiq_queue_collector = new

      Thread.new do
        loop do
          begin
            client.send_json(sidekiq_queue_collector.collect)
          rescue StandardError => e
            STDERR.puts("Prometheus Exporter Failed To Collect Sidekiq Queue metrics #{e}")
          ensure
            sleep frequency
          end
        end
      end
    end

    def collect
      {
        type: 'sidekiq_queue',
        queues: collect_queue_stats
      }
    end

    def collect_queue_stats
      hostname = Socket.gethostname
      pid = ::Process.pid
      ps = ::Sidekiq::ProcessSet.new

      process = ps.find do |sp|
        sp['hostname'] == hostname && sp['pid'] == pid
      end

      queues = process.nil? ? [] : process['queues']

      ::Sidekiq::Queue.all.map do |queue|
        next unless queues.include? queue.name
        {
          backlog_total: queue.size,
          latency_seconds: queue.latency.to_i,
          labels: { queue: queue.name }
        }
      end.compact
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
prometheus_exporter-0.8.0 lib/prometheus_exporter/instrumentation/sidekiq_queue.rb
prometheus_exporter-0.7.0 lib/prometheus_exporter/instrumentation/sidekiq_queue.rb
prometheus_exporter-0.6.0 lib/prometheus_exporter/instrumentation/sidekiq_queue.rb