Last active
April 18, 2018 00:37
-
-
Save itsderek23/685c7485a3bd020b6cdd9b1d61cb847f to your computer and use it in GitHub Desktop.
Scout Sneakers Instrumentation - see http://help.apm.scoutapp.com/#sneakers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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