class Napa::Middleware::RequestStats

Public Class Methods

new(app) click to toggle source
# File lib/napa/middleware/request_stats.rb, line 4
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# 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) click to toggle source
# 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