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
SINATRA_PATH_INFO =
'sinatra.route'.freeze
GRAPE_PATH_INFO =
'api.endpoint'.freeze
RAILS_PATH_INFO =
'action_controller.instance'.freeze

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Requests.



10
11
12
13
# File 'lib/vitals/integrations/rack/requests.rb', line 10

def initialize(app, options = {})
  @app = app
  @prefix = options[:prefix] ? options[:prefix] + "." : nil
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vitals/integrations/rack/requests.rb', line 15

def call(env)
  start = Time.now
  status, header, body = @app.call(env)
  t = Time.now - start
  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
  m = "requests.#{@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