class StackProf::Remote::Middleware

Middleware is a simple Rack middleware that handles requests to urls matching /__stackprof__ for starting/stopping a profile session and retreiving the dump files. It delegates to the ProcessReportCollector to do the actual work of collecting and combining the dumps.

Attributes

enabled[RW]
logger[RW]
options[RW]

Public Class Methods

enabled?(env) click to toggle source
# File lib/stackprof/remote/middleware.rb, line 16
def enabled?(env)
  if enabled.respond_to?(:call)
    enabled.call(env)
  else
    enabled
  end
end
new(app, options = {}) click to toggle source
# File lib/stackprof/remote/middleware.rb, line 25
def initialize(app, options = {})
  @app       = app
  self.class.logger   = options[:logger] || Logger.new(STDOUT)
  self.class.enabled  = options[:enabled] || false
  self.class.options  = options
  logger.info "[stackprof] Stackprof Middleware enabled"
end

Public Instance Methods

call(env) click to toggle source
# File lib/stackprof/remote/middleware.rb, line 33
def call(env)
  path = env['PATH_INFO']
  if self.class.enabled?(env) && in_stackprof?(path)
    handle_stackprof(path)
  else
    @app.call(env)
  end
end

Private Instance Methods

handle_stackprof(path) click to toggle source
# File lib/stackprof/remote/middleware.rb, line 51
def handle_stackprof(path)
  sp = StackProf::Remote::ProcessReportCollector.new(self.class.options)
  if path =~ /start/
    logger.debug "[stackprof] Starting StackProf"
    sp.start
    [200, {'Content-Type' => 'text/plain'}, ["StackProf Started"]]
  elsif path =~ /stop/
    logger.debug "[stackprof] Flushing StackProf"
    sp.stop
    sp.save
    if results = sp.marshaled_results
      [200, {'Content-Type' => 'binary/octet-stream'}, [results]]
    else
      [404, {'Content-Type' => 'text/plain'}, ["404 StackProf Results Not Found"]]
    end
  end
end
in_stackprof?(path) click to toggle source
# File lib/stackprof/remote/middleware.rb, line 47
def in_stackprof?(path)
  path =~ /^\/__stackprof__/
end
logger() click to toggle source
# File lib/stackprof/remote/middleware.rb, line 43
def logger
  self.class.logger
end