Last active
January 11, 2016 15:15
-
-
Save maciejmajewski/d4fe02efded8ebf3d31e to your computer and use it in GitHub Desktop.
Grape instrumentation with NewRelic RPM 3.9.x & Rails
This file contains hidden or 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
if defined?(NewRelic) | |
module NewRelic | |
module Agent | |
module Instrumentation | |
class Grape < ::Grape::Middleware::Base | |
def before | |
begin | |
NewRelic::Agent.set_transaction_name(transaction_name, | |
category: :rack) | |
rescue => e | |
Rails.logger.error { "Can't set name for Grape API call: #{e}" } | |
end | |
end | |
private | |
def transaction_name | |
"#{api_class}/#{route_path}\##{route_method}" | |
end | |
def api_class | |
env['api.endpoint'].source.binding.eval('self') | |
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('/', '-') | |
"#{route.route_version}.#{path}" | |
end | |
end | |
end | |
end | |
end | |
DependencyDetection.defer do | |
@name = :grape | |
depends_on do | |
defined?(::Grape) && ! ::NewRelic::Control.instance['disable_grape'] && | |
! ENV['DISABLE_NEW_RELIC_GRAPE'] | |
end | |
executes do | |
NewRelic::Agent.logger.info 'Installing Grape instrumentation' | |
Grape::API.class_eval do | |
class << self | |
def reset_with_newrelic!(*args) | |
reset_without_newrelic!(*args) | |
use NewRelic::Agent::Instrumentation::Grape | |
end | |
alias_method_chain :reset!, :newrelic | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment