Class: Napa::Middleware::RequestStats

Inherits:
Object
  • Object
show all
Defined in:
lib/napa/middleware/request_stats.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RequestStats

Returns a new instance of RequestStats.



4
5
6
# File 'lib/napa/middleware/request_stats.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/napa/middleware/request_stats.rb', line 17

def call(env)
  # Mark the request time
  start = Time.now

  # Process the request
  status, headers, body = @app.call(env)

  # Mark the response time
  stop = Time.now

  # Calculate total response time
  response_time = (stop - start) * 1000

  request = Rack::Request.new(env)
  path = normalize_path(request.path_info)

  # Emit stats to StatsD
  Napa::Stats.emitter.timing('response_time', response_time)
  Napa::Stats.emitter.timing("path.#{Napa::Stats.path_to_key(request.request_method, path)}.response_time", response_time)

  # Return the results
  [status, headers, body]
end

#normalize_path(path) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/napa/middleware/request_stats.rb', line 8

def normalize_path(path)
  case
    when path == '/'
      'root'
    else
      path.start_with?('/') ? path[1..-1] : path
  end
end