Skip to content

Instantly share code, notes, and snippets.

@dblock
Created November 29, 2012 17:12
Show Gist options
  • Save dblock/4170469 to your computer and use it in GitHub Desktop.
Save dblock/4170469 to your computer and use it in GitHub Desktop.
NewRelic Instrumentation for Grape API DSL
class ApiNewRelicInstrumenter < Grape::Middleware::Base
include NewRelic::Agent::Instrumentation::ControllerInstrumentation
def call_with_newrelic(&block)
trace_options = {
:category => :rack,
:path => "#{route_path}\##{route_method}",
:request => request
}
perform_action_with_newrelic_trace(trace_options) do
result = yield
MetricFrame.abort_transaction! if result.first == 404 # ignore cascaded calls
result
end
end
def call(env)
@env = env
if ENV['NEW_RELIC_ID']
call_with_newrelic do
super
end
else
super
end
end
def env
@env
end
def route
env['api.endpoint'].routes.first
end
def route_method
route.route_method.downcase
end
def route_path
path = route.route_path.gsub(/^.+:version\/|^\/|:|\(.+\)/, '').tr('/', '-')
"api.#{route.route_version}.#{path}"
end
end
@coffeencoke
Copy link

Hey - What does the route_path method do exactly?

@mfdeveloper
Copy link

Hey guy, In line 8, the request object is nil. I change this line to:

Grape::Request.new(@env)

I saw this piece of code in: grape/lib/grape/middleware/globals.rb file of Grape repository.
I didn't a fork because the solution is very simple!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment