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 = 5, sampling_interval = 0.1) ⇒ SlowRequestProfiler

Returns a new instance of SlowRequestProfiler.



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

def initialize(app,
               slow_request=5,
               sampling_interval=0.1)
  @app = app
  @post = Post.new
  @prof = SamplingProf.new(sampling_interval) do |data|
    t = Thread.current
    if (Time.now - t['corn_start_time']) > slow_request
      name = [t['corn_start_time'], t['corn_path_info']].join(',')
      @post.enqueue(data, name)
    end
  end
  at_exit { terminate }
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  Thread.current['corn_path_info'] = env['PATH_INFO']
  Thread.current['corn_start_time'] = Time.now
  @prof.profile { @app.call(env) }
end

#terminateObject



28
29
30
31
# File 'lib/corn/rack/slow_request_profiler.rb', line 28

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