Class: Vitals::Integrations::Rack::Requests

Inherits:
Object
  • Object
show all
Defined in:
lib/vitals/integrations/rack/requests.rb

Constant Summary collapse

REQUEST_METHOD =
'REQUEST_METHOD'.freeze
RACK_PATH_INFO =
'PATH_INFO'.freeze
RACK_ROUTER_INFO =
'rack.routing_args'.freeze
SINATRA_PATH_INFO =
'sinatra.route'.freeze
GRAPE_PATH_INFO =
'api.endpoint'.freeze
RAILS_PATH_INFO =
'action_controller.instance'.freeze
REQ_PREF =
'vitals.req_prefix'

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Requests

Returns a new instance of Requests.



12
13
14
15
# File 'lib/vitals/integrations/rack/requests.rb', line 12

def initialize(app, options = {})
  @app = app
  @prefix = options[:prefix] ? options[:prefix] + "." : nil
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
40
# 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