Class: Stackprofiler::Sidekiq::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/stackprofiler/sidekiq.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Middleware

Returns a new instance of Middleware.



7
8
9
10
# File 'lib/stackprofiler/sidekiq.rb', line 7

def initialize opts={}
  @options = opts
  @predicate = opts[:predicate] || proc { true }
end

Instance Method Details

#call(worker, job, queue, &blk) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/stackprofiler/sidekiq.rb', line 12

def call worker, job, queue, &blk
  opts = {mode: :wall, raw: true, threads: [Thread.current] }.merge @options

  if @predicate.call(worker, job, queue) && !StackProfx.running?
    profile = StackProfx.run(opts) { blk.call }

    klass, jid = job.values_at 'class', 'jid'
    profile[:suggested_rebase] = "#{klass}#perform"
    profile[:name] = "Sidekiq:#{klass}:#{jid}"

    Thread.new { send_profile profile }
  else
    blk.call
  end
end

#send_profile(profile) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/stackprofiler/sidekiq.rb', line 28

def send_profile profile
  url = URI::parse ui_url
  headers = {'Content-Type' => 'application/x-ruby-marshal'}
  req = Net::HTTP::Post.new(url.to_s, headers)
  req.body = Marshal.dump profile

  response = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
end

#ui_urlObject



37
38
39
# File 'lib/stackprofiler/sidekiq.rb', line 37

def ui_url
  @options[:ui_url] || ENV['STACKPROFILER_UI_URL']
end