Skip to content

Instantly share code, notes, and snippets.

@mikesea
Created October 25, 2013 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikesea/7157553 to your computer and use it in GitHub Desktop.
Save mikesea/7157553 to your computer and use it in GitHub Desktop.
wd_sinatra with NewRelic instrumentation
require 'new_relic/agent/instrumentation/controller_instrumentation'
DependencyDetection.defer do
depends_on do
defined?(WeaselDiesel) && defined?(WeaselDiesel::RequestHandler) &&
WeaselDiesel::RequestHandler.method_defined?(:dispatch)
end
executes do
NewRelic::Agent.logger.debug 'Installing WeaselDiesel instrumentation'
end
executes do
::WeaselDiesel::RequestHandler.class_eval do
include NewRelic::Agent::Instrumentation::WeaselDiesel
include NewRelic::Agent::Instrumentation::ControllerInstrumentation
alias dispatch_without_newrelic dispatch
alias dispatch dispatch_with_newrelic
add_method_tracer :params_preprocessor_hook if self.method_defined?(:params_preprocessor_hook)
add_method_tracer :params_postprocessor_hook if self.method_defined?(:params_postprocessor_hook)
add_method_tracer :pre_dispatch_hook if self.method_defined?(:pre_dispatch_hook)
end
#
# Monitor the actual dispatch of each service
WSList.all.each do |service|
service.alpha_handler.singleton_class.class_eval do
add_method_tracer :service_dispatch, 'Service/Dispatch'
end if service.alpha_handler && service.alpha_handler.respond_to?(:singleton_class)
end
end
end
module NewRelic
module Agent
module Instrumentation
module WeaselDiesel
include ::NewRelic::Agent::Instrumentation::ControllerInstrumentation
def dispatch_with_newrelic(app)
perform_action_with_newrelic_trace(:category => :controller,
:class_name => service.class.name,
:controller => service.class.name,
:path => "#{service.verb.upcase} #{service.url}",
:name => "#{service.verb.upcase} #{service.url}",
:params => filter_newrelic_params(app.params),
:request => app.request) do
dispatch_without_newrelic(app)
end
end
# filter params based the newrelic.yml entry if it exists.
def filter_newrelic_params(params)
@filters ||= NewRelic::Control.instance['filter_parameters']
return params if @filters.nil? || @filters.empty?
duped_params = params.dup
duped_params.each{|k,v| duped_params[k] = 'FILTERED' if @filters.include?(k) }
duped_params
end
end
end
end
end
DependencyDetection.detect!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment