Class: ScoutApm::BackgroundJobIntegrations::Sneakers

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/background_job_integrations/sneakers.rb

Constant Summary collapse

ACTIVE_JOB_KLASS =
'ActiveJob::QueueAdapters::SneakersAdapter::JobWrapper'.freeze
UNKNOWN_CLASS_PLACEHOLDER =
'UnknownJob'.freeze
UNKNOWN_QUEUE_PLACEHOLDER =
'default'.freeze

Instance Method Summary collapse

Instance Method Details

#forking?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/scout_apm/background_job_integrations/sneakers.rb', line 22

def forking?
  false
end

#installObject



26
27
28
# File 'lib/scout_apm/background_job_integrations/sneakers.rb', line 26

def install
  install_worker_override
end

#install_worker_overrideObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/scout_apm/background_job_integrations/sneakers.rb', line 30

def install_worker_override
  ::Sneakers::Worker.module_eval do
    def initialize_with_scout(*args)
      agent = ::ScoutApm::Agent.instance
      agent.start
      initialize_without_scout(*args)
    end

    alias_method :initialize_without_scout, :initialize
    alias_method :initialize, :initialize_with_scout

    def process_work_with_scout(*args)
      delivery_info, , msg, _handler = args

      queue = delivery_info[:routing_key] || UNKNOWN_QUEUE_PLACEHOLDER

      job_class = begin
        if self.class == ActiveJob::QueueAdapters::SneakersAdapter::JobWrapper
          msg["job_class"] || UNKNOWN_CLASS_PLACEHOLDER
        else
          self.class.name
        end
      rescue => e
        UNKNOWN_CLASS_PLACEHOLDER
      end

      req = ScoutApm::RequestManager.lookup

      # RabbitMQ does not store a created-at timestamp
      # req.annotate_request(:queue_latency => latency(msg))

      begin
        req.start_layer(ScoutApm::Layer.new('Queue', queue))
        started_queue = true
        req.start_layer(ScoutApm::Layer.new('Job', job_class))
        started_job = true

        process_work_without_scout(*args)
      rescue Exception => e
        req.error!
        raise
      ensure
        req.stop_layer if started_job
        req.stop_layer if started_queue
      end
    end

    alias_method :process_work_without_scout, :process_work
    alias_method :process_work, :process_work_with_scout
  end
end

#nameObject



4
5
6
# File 'lib/scout_apm/background_job_integrations/sneakers.rb', line 4

def name
  :sneakers
end

#present?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/scout_apm/background_job_integrations/sneakers.rb', line 8

def present?
  defined?(::Sneakers) && supported_version?
end

#supported_version?Boolean

Only support Sneakers 2.7 and up

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
# File 'lib/scout_apm/background_job_integrations/sneakers.rb', line 13

def supported_version?
  result = Gem::Version.new(::Sneakers::VERSION) > Gem::Version.new("2.7.0")
  ScoutApm::Agent.instance.logger.info("Skipping Sneakers instrumentation. Only versions 2.7+ are supported. See docs or contact [email protected] for instrumentation of older versions.")
  result
rescue
  ScoutApm::Agent.instance.logger.info("Failed comparing Sneakers Version. Skipping")
  false
end