-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sidekiq profiler middleware that puts periodic heap dumps on s3
- Loading branch information
1 parent
4883e2a
commit 7d819bd
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require "objspace" | ||
require "tempfile" | ||
|
||
class HeapDumpUploader < CarrierWave::Uploader::Base | ||
storage :fog | ||
|
||
def store_dir | ||
"heap_dumps" | ||
end | ||
end | ||
|
||
module Sidekiq | ||
module Middleware | ||
module Server | ||
class Profiler | ||
|
||
# Number of jobs to process before reporting | ||
JOBS = ENV.fetch("SIDEKIQ_PROFILER_REPORT_INCREMENT") { 100 }.to_i | ||
STAMP = Time.now.to_i | ||
|
||
class << self | ||
mattr_accessor :counter | ||
self.counter = 0 | ||
|
||
def synchronize(&block) | ||
@lock ||= Mutex.new | ||
@lock.synchronize(&block) | ||
end | ||
end | ||
|
||
def call(worker_instance, item, queue) | ||
begin | ||
yield | ||
ensure | ||
self.class.synchronize do | ||
self.class.counter += 1 | ||
|
||
if self.class.counter % JOBS == 0 | ||
Sidekiq.logger.info "reporting allocations after #{self.class.counter} jobs" | ||
GC.start | ||
basename = "heap-#{STAMP}-#{self.class.counter}-" | ||
f = Tempfile.new([basename, '.json']) | ||
begin | ||
ObjectSpace.dump_all(output: f) | ||
Sidekiq.logger.info "heap saved to #{f.path}" | ||
if ENV["UPLOAD_SIDEKIQ_HEAP_DUMPS"].present? | ||
uploader = HeapDumpUploader.new | ||
uploader.store!(f) | ||
Sidekiq.logger.info "heap uploaded to #{uploader.url}" | ||
end | ||
ensure | ||
f.close | ||
f.unlink | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |