Module: ScoutApm::Instruments::GrapeEndpointInstruments

Defined in:
lib/scout_apm/instruments/grape.rb

Instance Method Summary collapse

Instance Method Details

#run_with_scout_instruments(*args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/scout_apm/instruments/grape.rb', line 55

def run_with_scout_instruments(*args)
  request = ::Grape::Request.new(env || args.first)
  req = ScoutApm::RequestManager.lookup

  path = ScoutApm::Agent.instance.context.config.value("uri_reporting") == 'path' ? request.path : request.fullpath
  req.annotate_request(:uri => path)

  # IP Spoofing Protection can throw an exception, just move on w/o remote ip
  req.context.add_user(:ip => request.ip) rescue nil

  req.set_headers(request.headers)

  begin
    name = ["Grape",
            self.options[:method].first,
            self.options[:for].to_s,
            self.namespace.sub(%r{\A/}, ''), # removing leading slashes
            self.options[:path].first,
    ].compact.map{ |n| n.to_s }.join("/")
  rescue => e
    ScoutApm::Agent.instance.context.logger.info("Error getting Grape Endpoint Name. Error: #{e.message}. Options: #{self.options.inspect}")
    name = "Grape/Unknown"
  end

  req.start_layer( ScoutApm::Layer.new("Controller", name) )
  begin
    run_without_scout_instruments(*args)
  rescue
    req.error!
    raise
  ensure
    req.stop_layer
  end
end