Created
November 29, 2012 17:12
-
-
Save dblock/4170469 to your computer and use it in GitHub Desktop.
NewRelic Instrumentation for Grape API DSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey guy, In line 8, the
request
object is nil. I change this line to: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!!