diff --git a/lib/scout_apm.rb b/lib/scout_apm.rb index 14ce78a5..f3ba0d06 100644 --- a/lib/scout_apm.rb +++ b/lib/scout_apm.rb @@ -67,6 +67,7 @@ module ScoutApm require 'scout_apm/background_job_integrations/sneakers' require 'scout_apm/background_job_integrations/que' require 'scout_apm/background_job_integrations/legacy_sneakers' +require 'scout_apm/background_job_integrations/good_job' require 'scout_apm/framework_integrations/rails_2' require 'scout_apm/framework_integrations/rails_3_or_4' diff --git a/lib/scout_apm/background_job_integrations/good_job.rb b/lib/scout_apm/background_job_integrations/good_job.rb new file mode 100644 index 00000000..4e95b348 --- /dev/null +++ b/lib/scout_apm/background_job_integrations/good_job.rb @@ -0,0 +1,49 @@ +module ScoutApm + module BackgroundJobIntegrations + class GoodJob + UNKNOWN_QUEUE_PLACEHOLDER = 'default'.freeze + attr_reader :logger + + def name + :good_job + end + + def present? + defined?(::GoodJob::VERSION) + end + + def forking? + false + end + + def install + ActiveSupport.on_load(:active_job) do + include ScoutApm::Tracer + + around_perform do |job, block| + # I have a sneaking suspicion there is a better way to handle Agent starting + # Maybe hook into GoodJob lifecycle events? + req = ScoutApm::RequestManager.lookup + latency = Time.now - (job.scheduled_at || job.enqueued_at) rescue 0 + req.annotate_request(queue_latency: latency) + + begin + req.start_layer ScoutApm::Layer.new("Queue", job.queue_name.presence || UNKNOWN_QUEUE_PLACEHOLDER) + started_queue = true # Following Convention + req.start_layer ScoutApm::Layer.new("Job", job.class.name) + started_job = true # Following Convention + + block.call + rescue + req.error! + raise + ensure + req.stop_layer if started_job + req.stop_layer if started_queue + end + end + end + end + end + end +end diff --git a/lib/scout_apm/environment.rb b/lib/scout_apm/environment.rb index e4af726e..4ed29a45 100644 --- a/lib/scout_apm/environment.rb +++ b/lib/scout_apm/environment.rb @@ -31,6 +31,7 @@ class Environment ScoutApm::BackgroundJobIntegrations::DelayedJob.new, ScoutApm::BackgroundJobIntegrations::Que.new, ScoutApm::BackgroundJobIntegrations::Faktory.new, + ScoutApm::BackgroundJobIntegrations::GoodJob.new, ] FRAMEWORK_INTEGRATIONS = [