Created
June 26, 2020 20:52
-
-
Save Geesu/d2040afaf2079caf2110979c3dca073f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# frozen_string_literal: true | |
# Instruments a block of code and reports it to scout as a background job. Do NOT use this to instrument a small | |
# amount of code. It is meant to be used for ruby processes that we start and scout doesn't automatically instrument. | |
class ScoutReporter | |
@@agent = nil | |
def self.initialize_agent | |
if @@agent.nil? | |
@@agent = ::ScoutApm::Agent.instance | |
@@agent.start | |
end | |
end | |
def self.instrument(job_name, &block) | |
initialize_agent | |
return if @@agent.nil? | |
req = ScoutApm::RequestManager.lookup | |
begin | |
req.start_layer(ScoutApm::Layer.new('Queue', 'default')) #UNKNOWN_QUEUE_PLACEHOLDER | |
started_queue = true | |
req.start_layer(ScoutApm::Layer.new('Job', job_name)) | |
started_job = true | |
yield | |
rescue Exception => e | |
req.error! | |
raise | |
ensure | |
req.stop_layer if started_job | |
req.stop_layer if started_queue | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment