Skip to content

Instantly share code, notes, and snippets.

@gaiottino
Forked from dblock/api_new_relic_instrumenter.rb
Last active December 15, 2015 20:19
Show Gist options
  • Save gaiottino/5317815 to your computer and use it in GitHub Desktop.
Save gaiottino/5317815 to your computer and use it in GitHub Desktop.
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 NewRelic::Agent.config[:monitor_mode]
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
def response
return if @app_response.nil?
super
end
end
@gaiottino
Copy link
Author

The def response is needed. Described here: http://artsy.github.com/blog/2012/11/29/measuring-performance-in-grape-apis-with-new-relic/#comment-731239906. Not sure why. 204s?

Also, updated it to rely on the Agent config to determine whether it should run on line 20

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