Class: Rack::Perf

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/perf.rb,
lib/rack/perf/version.rb

Defined Under Namespace

Classes: DetermineIPAddress, NormalizePath, PerformanceTracker, SendDataToPerf

Constant Summary collapse

HIDDEN_READABLE_FIELDS =
[
  :starttime, :endtime, :api_key,
  :debug, :stack, :env
]
VERSION =
"1.1.6"

Instance Method Summary collapse

Constructor Details

#initialize(stack, api_key, debug = false) ⇒ Perf

Returns a new instance of Perf.



14
15
16
17
18
# File 'lib/rack/perf.rb', line 14

def initialize(stack, api_key, debug = false)
  @stack   = stack
  @api_key = api_key
  @debug   = debug
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rack/perf.rb', line 20

def call(env)
  @env = env

  # log the current time now before the request starts
  performance_tracker = PerformanceTracker.new
  performance_tracker.start

  # run the current request
  request = Rack::Request.new(env)
  status, headers, body = stack.call(env)

  # log the end time of the request
  performance_tracker.end

  # normalize url
  normalized_path = NormalizePath.new(request)

  # send it up as long as we don't get nil, this helps
  # in cases when we intercept asset urls that don't
  # really matter
  if normalized_path.path?
    send_data                = SendDataToPerf.new
    send_data.api_key        = @api_key
    send_data.ip_addr        = DetermineIPAddress.new(request).ip_address
    send_data.request_method = request.request_method
    send_data.request_url    = request.url
    send_data.normalized_uri = normalized_path.path
    send_data.status_code    = status
    send_data.time_in_millis = performance_tracker.time
    send_data.perform!
  end

  # send back intended data
  [status, headers, body]
end