Class: PrometheusExporter::Instrumentation::SidekiqProcess

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSidekiqProcess

Returns a new instance of SidekiqProcess.



22
23
24
25
# File 'lib/prometheus_exporter/instrumentation/sidekiq_process.rb', line 22

def initialize
  @pid = ::Process.pid
  @hostname = Socket.gethostname
end

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_process.rb', line 5

def self.start(client: nil, frequency: 30)
  client ||= PrometheusExporter::Client.default
  sidekiq_process_collector = new

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

Instance Method Details

#collectObject



27
28
29
30
31
32
# File 'lib/prometheus_exporter/instrumentation/sidekiq_process.rb', line 27

def collect
  {
    type: 'sidekiq_process',
    process: collect_stats
  }
end

#collect_statsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/prometheus_exporter/instrumentation/sidekiq_process.rb', line 34

def collect_stats
  process = current_process
  return {} unless process

  {
    busy: process['busy'],
    concurrency: process['concurrency'],
    labels: {
      labels: process['labels'].sort.join(','),
      queues: process['queues'].sort.join(','),
      quiet: process['quiet'],
      tag: process['tag'],
      hostname: process['hostname'],
      identity: process['identity'],
    }
  }
end

#current_processObject



52
53
54
55
56
# File 'lib/prometheus_exporter/instrumentation/sidekiq_process.rb', line 52

def current_process
  ::Sidekiq::ProcessSet.new.find do |sp|
    sp['hostname'] == @hostname && sp['pid'] == @pid
  end
end