Class: Corn::Rack::SlowRequestProfiler

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) ⇒ SlowRequestProfiler

Returns a new instance of SlowRequestProfiler.



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

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

Instance Method Details

#call(env) ⇒ Object



18
19
20
# File 'lib/corn/rack/slow_request_profiler.rb', line 18

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

#output_handler(env) ⇒ Object



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

def output_handler(env)
  request_env = RequestEnv.new(env, @slow_request_threshold)
  lambda do |data|
    if request_env.slow_request?
      @post.enqueue(data,
                    request_env.report_name,
                    request_env.start_time)
    end
  end
end

#terminateObject



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

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