Skip to content

Instantly share code, notes, and snippets.

@Geesu
Created June 26, 2020 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Geesu/d2040afaf2079caf2110979c3dca073f to your computer and use it in GitHub Desktop.
Save Geesu/d2040afaf2079caf2110979c3dca073f to your computer and use it in GitHub Desktop.
# 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