2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Prometheus
|
|
|
|
module PidProvider
|
|
|
|
extend self
|
|
|
|
|
|
|
|
def worker_id
|
|
|
|
if Sidekiq.server?
|
2019-12-26 22:10:19 +05:30
|
|
|
sidekiq_worker_id
|
2019-10-12 21:52:04 +05:30
|
|
|
elsif defined?(Unicorn::Worker)
|
|
|
|
unicorn_worker_id
|
|
|
|
elsif defined?(::Puma)
|
|
|
|
puma_worker_id
|
|
|
|
else
|
|
|
|
unknown_process_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def sidekiq_worker_id
|
|
|
|
if worker = ENV['SIDEKIQ_WORKER_ID']
|
|
|
|
"sidekiq_#{worker}"
|
|
|
|
else
|
|
|
|
'sidekiq'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def unicorn_worker_id
|
|
|
|
if matches = process_name.match(/unicorn.*worker\[([0-9]+)\]/)
|
|
|
|
"unicorn_#{matches[1]}"
|
|
|
|
elsif process_name =~ /unicorn/
|
|
|
|
"unicorn_master"
|
|
|
|
else
|
|
|
|
unknown_process_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def puma_worker_id
|
|
|
|
if matches = process_name.match(/puma.*cluster worker ([0-9]+):/)
|
|
|
|
"puma_#{matches[1]}"
|
|
|
|
elsif process_name =~ /puma/
|
|
|
|
"puma_master"
|
|
|
|
else
|
|
|
|
unknown_process_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def unknown_process_id
|
|
|
|
"process_#{Process.pid}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_name
|
|
|
|
$0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|