Skip to content
This repository has been archived by the owner on Apr 1, 2020. It is now read-only.

Commit

Permalink
update resque instrumentation to use a plugin for most of the work
Browse files Browse the repository at this point in the history
  • Loading branch information
jaggederest committed May 12, 2011
1 parent f75ee3b commit 8990ce6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 49 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
69 changes: 20 additions & 49 deletions 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']

0 comments on commit 8990ce6

Please sign in to comment.