class Vitals::Integrations::Rack::Requests

Constants

GRAPE_PATH_INFO
RACK_PATH_INFO
RACK_ROUTER_INFO
RAILS_PATH_INFO
REQUEST_METHOD
REQ_PREF
SINATRA_PATH_INFO

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/vitals/integrations/rack/requests.rb, line 12
def initialize(app, options = {})
  @app = app
  @prefix = options[:prefix] ? options[:prefix] + "." : nil
end

Private Class Methods

grape_path(env) click to toggle source
# File lib/vitals/integrations/rack/requests.rb, line 48
def self.grape_path(env)
  route = if env[RACK_ROUTER_INFO]
    # grape 0.11 route bug workaround with http_basic.
    # when unauthenticated, GRAPE_PATH_INFO route has a nil env. this one
    # here doesn't:
    env[RACK_ROUTER_INFO][:route_info]
  else
    # grape > 0.11
    env[GRAPE_PATH_INFO].route
  end
  Vitals::Utils.grape_path(route)
end
rack_path(env) click to toggle source
# File lib/vitals/integrations/rack/requests.rb, line 66
def self.rack_path(env)
  ''
end
rails_path(env) click to toggle source
# File lib/vitals/integrations/rack/requests.rb, line 61
def self.rails_path(env)
  ctrl = env[RAILS_PATH_INFO]
  "#{ctrl.controller_name}_#{ctrl.action_name}"
end
sinatra_path(env) click to toggle source
# File lib/vitals/integrations/rack/requests.rb, line 44
def self.sinatra_path(env)
  env[SINATRA_PATH_INFO].gsub(/^\w+\s+\//, '')
end

Public Instance Methods

call(env) click to toggle source
# File lib/vitals/integrations/rack/requests.rb, line 17
def call(env)
  start = Time.now
  status, header, body = @app.call(env)
  t = Time.now - start
  req_prefix = env[REQ_PREF] ? "#{env[REQ_PREF]}." : ''

  path = if env[SINATRA_PATH_INFO]
        Requests.sinatra_path(env)
      elsif env[GRAPE_PATH_INFO]
        Requests.grape_path(env)
      elsif env[RAILS_PATH_INFO]
        Requests.rails_path(env)
      else
        Requests.rack_path(env)
      end

  path = !path.empty? ? path + '.' : path
  m = "requests.#{@prefix}#{req_prefix}#{path}#{env[REQUEST_METHOD].downcase}.#{status}"

  # TODO add option to customize 'requests' through options
  Vitals.timing(m, Vitals::Utils.sec_to_ms(t))

  [status, header, body]
end