Class: Corn::Rack::SlowRequestProfiler::ProfilingApp

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

Instance Method Summary collapse

Constructor Details

#initialize(app, slow_request_threshold = 5, sampling_interval = 0.1, post_interval = 2) ⇒ ProfilingApp



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/corn/rack/slow_request_profiler.rb', line 9

def initialize(app,
               slow_request_threshold=5,
               sampling_interval=0.1,
               post_interval=2)
  @app = app
  @slow_request_threshold = slow_request_threshold
  @sampling_interval = sampling_interval
  @post = Post.new(post_interval)
  @prof = SamplingProf.new(@sampling_interval)
  at_exit { terminate }
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
# File 'lib/corn/rack/slow_request_profiler.rb', line 21

def call(env)
  @prof.profile(output_handler(env)) { @app.call(env) }
end

#output_handler(env) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/corn/rack/slow_request_profiler.rb', line 30

def output_handler(env)
  request_env = RequestEnv.new(env)
  lambda do |data|
    if request_env.time > @slow_request_threshold
      @post.enqueue(request_env.to_report.merge("data" => data))
    end
  end
end

#terminateObject



25
26
27
28
# File 'lib/corn/rack/slow_request_profiler.rb', line 25

def terminate
  @prof.terminate rescue nil
  @post.terminate rescue nil
end