Created
April 10, 2015 19:55
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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