Skip to content

Commit

Permalink
combine actions to just extend job class
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitch Hartweg committed Dec 5, 2024
1 parent e558b4f commit 5e25d12
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 47 deletions.
57 changes: 10 additions & 47 deletions lib/scout_apm/background_job_integrations/resque.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ def name

def present?
defined?(::Resque) &&
::Resque.respond_to?(:before_first_fork) &&
::Resque.respond_to?(:after_fork)# &&
# ::Resque.respond_to?(:before_perform)
::Resque.respond_to?(:before_first_fork)# &&
# ::Resque.respond_to?(:after_fork)
end

# Lies. This forks really aggressively, but we have to do handling
Expand All @@ -20,19 +19,13 @@ def forking?
end

def install
install_before_fork

ScoutApm::Agent.instance.context.logger.info "resque_debug Installing Resque Instrumentation"
install_after_fork
# # If forking is disabled, after_fork is never executed. Must instrument somewhere else.
# if ENV["FORK_PER_JOB"] == "false"
# install_before_perform
# else
# install_after_fork
# end
install_before_first_fork
# install_after_fork
# install_enqueue
install_instruments
end

def install_before_fork
def install_before_first_fork
::Resque.before_first_fork do
begin
if ScoutApm::Agent.instance.context.config.value('start_resque_server_instrument')
Expand All @@ -49,50 +42,20 @@ def install_before_fork
end
end

def install_after_fork
ScoutApm::Agent.instance.context.logger.info "resque_debug Installing Resque after_fork"
::Resque.after_fork do
begin
ScoutApm::Agent.instance.context.become_remote_client!(bind, port)
inject_job_instrument
rescue => e
ScoutApm::Agent.instance.context.logger.warn "Error while Installing Resque after_fork: #{e.inspect}"
end
end
end

def install_before_perform
ScoutApm::Agent.instance.context.logger.info "resque_debug Installing Resque before_perform"
::Resque.before_perform do
begin
ScoutApm::Agent.instance.context.become_remote_client!(bind, port)
inject_job_instrument
rescue => e
ScoutApm::Agent.instance.context.logger.warn "Error while Installing Resque before_perform: #{e.inspect}"
end
end
end

# Insert ourselves into the point when resque turns a string "TestJob"
# into the class constant TestJob, and insert our instrumentation plugin
# into that constantized class
#
# This automates away any need for the user to insert our instrumentation into
# each of their jobs
def inject_job_instrument
def install_instruments
::Resque::Job.class_eval do
def payload_class_with_scout_instruments
klass = payload_class_without_scout_instruments
klass.extend(ScoutApm::Instruments::Resque)
# ScoutApm::Agent.instance.context.logger.info "resque_debug responds to before_perform_scout_instrument? #{klass.respond_to?(:before_perform_scout_instrument)}"
# ScoutApm::Agent.instance.context.logger.info "resque_debug responds to before_perform_in_app? #{klass.respond_to?(:before_perform_scout)}"
klass
end
alias_method :payload_class_without_scout_instruments, :payload_class
alias_method :payload_class, :payload_class_with_scout_instruments
end
end

private

def bind
config.value("remote_agent_host")
end
Expand Down
54 changes: 54 additions & 0 deletions lib/scout_apm/instruments/resque.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,54 @@
module ScoutApm
module Instruments
module Resque
def bind
config.value("remote_agent_host")
end

def port
config.value("remote_agent_port")
end

def config
@config || ScoutApm::Agent.instance.context.config
end

# Insert ourselves into the point when resque turns a string "TestJob"
# into the class constant TestJob, and insert our instrumentation plugin
# into that constantized class
#
# This automates away any need for the user to insert our instrumentation into
# each of their jobs
# def inject_job_instrument
# ::Resque::Job.class_eval do
# def payload_class_with_scout_instruments
# klass = payload_class_without_scout_instruments
# klass.extend(ScoutApm::Instruments::Resque)
# klass
# end
# alias_method :payload_class_without_scout_instruments, :payload_class
# alias_method :payload_class, :payload_class_with_scout_instruments
# end
# end

# def before_perform_scout_instrument(*args)
# ScoutApm::Agent.instance.context.logger.info "resque_debug IN BEFORE PERFORM"
# begin
# ScoutApm::Agent.instance.context.become_remote_client!(bind, port)
# # inject_job_instrument
# rescue => e
# ScoutApm::Agent.instance.context.logger.warn "Error while Installing Resque before_perform: #{e.inspect}"
# end
# end

def logger
ScoutApm::Agent.instance.context.logger
end

def around_perform_with_scout_instruments(*args)
logger.info "resque_debug IN AROUND PERFORM"
ScoutApm::Agent.instance.context.become_remote_client!(bind, port)
logger.info "resque_debug REMOTE AGENT"
job_name = self.to_s
queue = find_queue

Expand All @@ -10,19 +57,26 @@ def around_perform_with_scout_instruments(*args)
queue = args.first["queue_name"] rescue queue_name
end

logger.info "resque_debug JOB: #{job_name} QUEUE: #{queue}"

req = ScoutApm::RequestManager.lookup

# logger.info "resque_debug REQUEST: #{req.inspect}"

begin
req.start_layer(ScoutApm::Layer.new('Queue', queue))
started_queue = true
req.start_layer(ScoutApm::Layer.new('Job', job_name))
started_job = true

logger.info "resque_debug DOING LAYERS"

yield
rescue => e
req.error!
raise
ensure
logger.info "resque_debug ENSURING"
req.stop_layer if started_job
req.stop_layer if started_queue
end
Expand Down

0 comments on commit 5e25d12

Please sign in to comment.