Class: NewRelic::Agent::Samplers::DelayedJobSampler

Inherits:
NewRelic::Agent::Sampler show all
Defined in:
lib/new_relic/agent/samplers/delayed_job_sampler.rb

Overview

This sampler records the status of your delayed job table once a minute. It assumes jobs are cleared after being run, and failed jobs are not (otherwise the failed job metric is useless).

In earlier versions it will break out the queue length by priority. In later versions of DJ where distinct queues are supported, it breaks it out by queue name.

Constant Summary collapse

FAILED_QUERY =
'failed_at is not NULL'.freeze
LOCKED_QUERY =
'locked_by is not NULL'.freeze

Instance Attribute Summary

Attributes inherited from NewRelic::Agent::Sampler

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NewRelic::Agent::Sampler

enabled?, inherited, named, sampler_classes

Constructor Details

#initializeDelayedJobSampler

Returns a new instance of DelayedJobSampler.

Raises:

[View source]

27
28
29
30
31
# File 'lib/new_relic/agent/samplers/delayed_job_sampler.rb', line 27

def initialize
  raise Unsupported, 'DJ queue sampler disabled' if Agent.config[:'instrumentation.delayed_job'] == 'disabled'
  raise Unsupported, "DJ queue sampling unsupported with backend '#{::Delayed::Worker.backend}'" unless self.class.supported_backend?
  raise Unsupported, 'No DJ worker present. Skipping DJ queue sampler' unless NewRelic::DelayedJobInjection.worker_name
end

Class Method Details

.supported_backend?Boolean

DelayedJob supports multiple backends, only some of which we can handle. Check whether we think we’ve got what we need here.

Returns:

  • (Boolean)
[View source]

23
24
25
# File 'lib/new_relic/agent/samplers/delayed_job_sampler.rb', line 23

def self.supported_backend?
  ::Delayed::Worker.backend.to_s == 'Delayed::Backend::ActiveRecord::Job'
end

.supported_on_this_platform?Boolean

Returns:

  • (Boolean)
[View source]

60
61
62
# File 'lib/new_relic/agent/samplers/delayed_job_sampler.rb', line 60

def self.supported_on_this_platform?
  defined?(::Delayed::Job)
end

Instance Method Details

#count(query) ⇒ Object

[View source]

52
53
54
55
56
57
58
# File 'lib/new_relic/agent/samplers/delayed_job_sampler.rb', line 52

def count(query)
  if ::ActiveRecord::VERSION::MAJOR.to_i < 4
    ::Delayed::Job.count(:conditions => query)
  else
    ::Delayed::Job.where(query).count
  end
end

#failed_jobsObject

[View source]

44
45
46
# File 'lib/new_relic/agent/samplers/delayed_job_sampler.rb', line 44

def failed_jobs
  count(FAILED_QUERY)
end

#locked_jobsObject

[View source]

48
49
50
# File 'lib/new_relic/agent/samplers/delayed_job_sampler.rb', line 48

def locked_jobs
  count(LOCKED_QUERY)
end

#pollObject

[View source]

64
65
66
67
68
69
70
71
# File 'lib/new_relic/agent/samplers/delayed_job_sampler.rb', line 64

def poll
  # Wrapping these queries within the same connection avoids deadlocks
  ActiveRecord::Base.connection_pool.with_connection do
    record_failed_jobs(failed_jobs)
    record_locked_jobs(locked_jobs)
    record_queue_length_metrics
  end
end

#record_failed_jobs(value) ⇒ Object

[View source]

33
34
35
# File 'lib/new_relic/agent/samplers/delayed_job_sampler.rb', line 33

def record_failed_jobs(value)
  NewRelic::Agent.record_metric('Workers/DelayedJob/failed_jobs', value)
end

#record_locked_jobs(value) ⇒ Object

[View source]

37
38
39
# File 'lib/new_relic/agent/samplers/delayed_job_sampler.rb', line 37

def record_locked_jobs(value)
  NewRelic::Agent.record_metric('Workers/DelayedJob/locked_jobs', value)
end