Class: Stackprofiler::Middleware

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/stackprofiler/middleware.rb', line 8

def initialize(app, options={})
  @app = app

  pred = options[:predicate] || /profile=true/
  if pred.respond_to? :call
    @predicate = pred
  else
    regex = Regexp.new pred

    @predicate = proc do |env|
      req = Rack::Request.new env
      req.fullpath =~ regex
    end
  end

  @ui_url = options[:ui_url] || ENV['PRY_STACKPROFILER_UI_URL']
  @stackprof_opts = {mode: :wall, interval: 1000, raw: true}.merge(options[:stackprof] || {})
end

Instance Attribute Details

#ui_urlObject

Returns the value of attribute ui_url.



6
7
8
# File 'lib/stackprofiler/middleware.rb', line 6

def ui_url
  @ui_url
end

Instance Method Details

#call(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/stackprofiler/middleware.rb', line 27

def call(env)
  if @predicate.call(env)
    out = nil
    profile = StackProf.run(@stackprof_opts) { out = @app.call env }

    Thread.new do
      profile[:name] = Rack::Request.new(env).fullpath

      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

    out
  else
    @app.call env
  end
end