Module: ScoutApm::Instruments::GrapeEndpointInstrumentsPrepend

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

Instance Method Summary collapse

Instance Method Details

#run(*args) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/scout_apm/instruments/grape.rb', line 92

def run(*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
    super(*args)
  rescue
    req.error!
    raise
  ensure
    req.stop_layer
  end
end