Class: Rack::RequestProfiler

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/request-profiler.rb,
lib/rack/request-profiler/version.rb

Direct Known Subclasses

Profilers::Stathat, Profilers::Statsd

Constant Summary collapse

VERSION =
'0.1.2'

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ RequestProfiler

Returns a new instance of RequestProfiler.

Parameters:

  • opts (Hash) (defaults to: {})

    a hash of options

  • [Symbol] (Hash)

    a customizable set of options



6
7
8
# File 'lib/rack/request-profiler.rb', line 6

def initialize(app, opts = {})
  @app, @opts = app, opts
end

Instance Method Details

#call(env, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rack/request-profiler.rb', line 18

def call(env, &block)
  self.start_time = Time.now
  request         = Request.new(env)

  # Short-circuit here and pass the response back through the
  # middleware chain if the request path matches the ignore_path
  if @opts[:ignore_path] && request.path.match(@opts[:ignore_path])
    return @app.call(env)
  end

  status, headers, body = @app.call(env)

  unless status == 404
    handle_results(env, request)
  end
  [status, headers, body]
end

#handle_results(env, request = nil) ⇒ Object

Override this method in subclasses



37
38
39
# File 'lib/rack/request-profiler.rb', line 37

def handle_results(env, request = nil)
  # override me!
end

#run_timeObject

Returns elapsed time since start_time in milliseconds.



42
43
44
# File 'lib/rack/request-profiler.rb', line 42

def run_time
  ((Time.now - start_time) * 1000).round
end

#start_timeObject



14
15
16
# File 'lib/rack/request-profiler.rb', line 14

def start_time
  @start_time
end

#start_time=(time) ⇒ Object



10
11
12
# File 'lib/rack/request-profiler.rb', line 10

def start_time=(time)
  @start_time = time
end