diff --git a/README.md b/README.md index 01c59cc..01aaab5 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,13 @@ Both MongoMapper and Mongoid are supported. You can disable them both by settin ### Resque +Make sure that your jobs either inherit from Resque::Job or else include our instrumentation: + +class MyJob + require 'rpm_contrib/instrumentation/resque' + include Resque::Plugins::NewRelicInstrumentation +end + To disable resque, set 'disable_resque' to true in your newrelic.yml file. ### Redis diff --git a/lib/rpm_contrib/instrumentation/resque.rb b/lib/rpm_contrib/instrumentation/resque.rb index 021e058..37ef674 100644 --- a/lib/rpm_contrib/instrumentation/resque.rb +++ b/lib/rpm_contrib/instrumentation/resque.rb @@ -1,61 +1,32 @@ - -module RPMContrib - SUPPORTS_FORK = - begin - # jruby has a Kernel.fork method, but it raises NotImplementedError by default - if Kernel.respond_to?(:fork) - Kernel.fork { exit! } - true - else - false +module Resque + module Plugins + module NewRelicInstrumentation + include NewRelic::Agent::Instrumentation::ControllerInstrumentation + + def around_perform_with_monitoring(*args) + perform_action_with_newrelic_trace(:name => 'perform', :class_name => class_name, :category => 'OtherTransaction/ResqueJob') do + yield(*args) + end end - rescue NotImplementedError - false end + end +end +module RPMContrib module Instrumentation - # == Resque Instrumentation - # - # Installs a hook to ensure the agent starts manually when the worker - # starts and also adds the tracer to the process method which executes - # in the forked task. module Resque - def self.flush_metric_data - return if !NewRelic::Agent.agent.started? || NewRelic::Agent.agent.instance_variable_get(:@worker_loop).nil? - NewRelic::Agent.agent.instance_variable_get(:@worker_loop).run_task + ::Resque.before_first_fork do + NewRelic::Agent.manual_start(:dispatcher => :resque) end - ::Resque::Job.class_eval do - include NewRelic::Agent::Instrumentation::ControllerInstrumentation - - old_perform_method = instance_method(:perform) - - define_method(:perform) do - class_name = (payload_class ||self.class).name - NewRelic::Agent.reset_stats if NewRelic::Agent.respond_to? :reset_stats - perform_action_with_newrelic_trace(:name => 'perform', :class_name => class_name, - :category => 'OtherTransaction/ResqueJob') do - old_perform_method.bind(self).call - end - - if ::RPMContrib::SUPPORTS_FORK - NewRelic::Agent.shutdown unless defined?(::Resque.before_child_exit) - else - ::RPMContrib::Instrumentation::ResqueInstrumentation.flush_metric_data unless defined?(::Resque.before_child_exit) - end - - end + ::Resque.after_fork do + NewRelic::Agent.after_fork(:force_reconnect => false) end - if defined?(::Resque.before_child_exit) - ::Resque.before_child_exit do |worker| - if ::RPMContrib::SUPPORTS_FORK - NewRelic::Agent.shutdown - else - ::RPMContrib::Instrumentation::ResqueInstrumentation.flush_metric_data - end - end + ::Resque::Job.class_eval do + include Resque::Plugins::NewRelicInstrumentation end end end -end if defined?(::Resque::Job) and not NewRelic::Control.instance['disable_resque'] +end if defined?(::Resque) and not NewRelic::Control.instance['disable_resque'] +