Skip to content

Instantly share code, notes, and snippets.

@benweint
Created April 10, 2015 19:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benweint/d6894d15352500edbc69 to your computer and use it in GitHub Desktop.
Save benweint/d6894d15352500edbc69 to your computer and use it in GitHub Desktop.
Recording Sidekiq job counts by job class and queue name to New Relic Insights
require 'sidekiq/api'
if File.basename($0) == 'sidekiq'
Thread.new do
loop do
begin
t0 = Time.now
qcount = 0
njobs = 0
Sidekiq::Queue.all.each do |queue|
qcount += 1
counts = Hash.new(0)
queue.each { |j| counts[j.klass] += 1 }
counts.each do |job_class, job_count|
event = {
:queue => queue.name,
:job_class => job_class,
:job_count => job_count
}
njobs += job_count
NewRelic::Agent.record_custom_event('SidekiqQueueCount', event)
end
end
elapsed = Time.now - t0
$stderr.puts "Took #{elapsed} s to count all queued Sidekiq jobs (#{qcount} queues, #{njobs} jobs in total)"
rescue => e
$stderr.puts "Exception while counting queued jobs: #{e}\n#{e.backtrace.join("\n")}"
end
sleep 60
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment