Class: PrometheusExporter::Instrumentation::SidekiqQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/prometheus_exporter/instrumentation/sidekiq_queue.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(client: nil, frequency: 30) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/prometheus_exporter/instrumentation/sidekiq_queue.rb', line 5

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
        client.logger.error("Prometheus Exporter Failed To Collect Sidekiq Queue metrics #{e}")
      ensure
        sleep frequency
      end
    end
  end
end

Instance Method Details

#collectObject



22
23
24
25
26
27
# File 'lib/prometheus_exporter/instrumentation/sidekiq_queue.rb', line 22

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

#collect_queue_statsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/prometheus_exporter/instrumentation/sidekiq_queue.rb', line 29

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