Skip to content

Instantly share code, notes, and snippets.

@adamruzicka
Created August 11, 2021 14:51
Show Gist options
  • Save adamruzicka/1bf2e3ba27bc6fef19e8e38780df105e to your computer and use it in GitHub Desktop.
Save adamruzicka/1bf2e3ba27bc6fef19e8e38780df105e to your computer and use it in GitHub Desktop.
# Save this script as $LOCATION/queues.rb
# Run with foreman-rake console < $LOCATION/queues.rb
# Kill with C-c
# It collects sidekiq statistics every $INTERVAL seconds
# The statistics are saved into three CSV files
INTERVAL = (ENV['interval'] || 15).to_i
stats_csv = CSV.open(ENV['STATS'] || '/tmp/stats.csv', 'wb')
stats = ::Sidekiq::Stats.new
stats_keys = stats.instance_variable_get('@stats').keys.sort
stats_csv << (['timestamp'] + stats_keys)
queue_csv = CSV.open(ENV['QUEUES'] || '/tmp/queues.csv', 'wb')
queue_csv << ['timestamp', 'name', 'size', 'latency']
worker_csv = CSV.open(ENV['WORKERS'] || '/tmp/workers.csv', 'wb')
worker_csv << ['timestamp', 'pid', 'tid', 'queue', 'jid', 'class', 'args']
workers = ::Sidekiq::Workers.new
loop do
now = Time.now
stats = ::Sidekiq::Stats.new
stats_csv << [now] + stats_keys.map { |key| stats.public_send key }
Sidekiq::Queue.all.each do |queue|
row = [now]
row += [queue.name, queue.size, queue.latency]
queue_csv << row
end
workers.each do |pid, tid, work|
payload = work['payload']
row = [now]
row += [pid, tid, payload['queue'], payload['jid'], payload['class'], payload['args'].to_json]
worker_csv << row
end
[stats_csv, queue_csv, worker_csv].each(&:flush)
sleep INTERVAL
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment