Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scout Sneakers Instrumentation - see http://help.apm.scoutapp.com/#sneakers
# A module for instrumenting Sneaker workers.
#
# These can appear under "Background Jobs" (default) or "Web Endpoints" in the UI, depending on the value of
# +SCOUT_TRANSACTION_TYPE+.
#
# Appearing under "Web Endpoints" may be preferred if the app is only processing Sneaker
# transactions so that the app overview chart isn't blank.
#
# Example usage:
#
# class BaseWorker
# include Sneakers::Worker
# def work(attributes)
# # Do work
# end
# # This MUST be included AFTER the work method is defined.
# include ScoutApm::BackgroundJobIntegrations::Sneakers::Instruments
# end
module ScoutApm
module BackgroundJobIntegrations
class Sneakers
module Instruments
SCOUT_TRANSACTION_TYPE = 'Job' # Job | Controller
def self.included(instrumented_class)
ScoutApm::Agent.instance.logger.debug "Instrumenting #{instrumented_class.inspect}"
instrumented_class.class_eval do
unless instrumented_class.method_defined?(:work_without_scout_instruments)
alias_method :work_without_scout_instruments, :work
alias_method :work, :work_with_scout_instruments
end
end
end
def work_with_scout_instruments(*args)
ensure_scout_started!
req = ScoutApm::RequestManager.lookup
started_queue = false
if scout_job_transaction?
req.start_layer(ScoutApm::Layer.new('Queue', @queue.name))
started_queue = true
end
req.start_layer(ScoutApm::Layer.new(SCOUT_TRANSACTION_TYPE, scout_job_name))
started_transaction = true
begin
work_without_scout_instruments(*args)
rescue
req.error!
raise
ensure
req.stop_layer if started_transaction
req.stop_layer if started_queue
end
end
def scout_job_name
self.class.to_s
end
def scout_job_transaction?
SCOUT_TRANSACTION_TYPE == 'Job'
end
# Sneakers starts a number of worker processes. Ensure that a background worker is running
# for this process so that metrics can be stored.
def ensure_scout_started!
unless ScoutApm::Agent.instance.background_worker_running?
ScoutApm::Agent.instance.start(:skip_app_server_check => true)
ScoutApm::Agent.instance.start_background_worker
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment