Class: Rack::SpeedGun

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/speed_gun.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ SpeedGun

Returns a new instance of SpeedGun.



9
10
11
12
# File 'lib/rack/speed_gun.rb', line 9

def initialize(app)
  @app = app

end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rack/speed_gun.rb', line 14

def call(env)
  return @app.call(env) if SpeedGun.config.disabled? || skip?(env['PATH_INFO'])

  return call_speed_gun_app(env) if under_speed_gun?(env)

  SpeedGun.current_report = SpeedGun::Report.new.tap do |report|
    report.name = "#{env['REQUEST_METHOD']} #{env['PATH_INFO']}"
  end

  SpeedGun::Profiler::RackProfiler.profile('rack', rack: rack_info(env), request: request_info(env)) do |event|
    res = SpeedGun::Profiler::LineProfiler.profile { @app.call(env) }
    res[1]['X-Speed-Gun-Report'] = SpeedGun.current_report.id
    event.payload[:response] = {
      status: res[0],
      headers: res[1]
    }
    res
  end
ensure
  if SpeedGun.current_report
    SpeedGun.config.logger.debug("Speed Gun ID: #{SpeedGun.current_report.id}")
    SpeedGun.config.logger.debug(
      "Check out here: #{base_url(env)}/reports/#{SpeedGun.current_report.id}"
    ) if SpeedGun.config.webapp
    SpeedGun.config.store.store(SpeedGun.current_report)
  end
  SpeedGun.current_report = nil
end