Module: NewRelic::Agent::Instrumentation::DelayedJob::Naming

Defined in:
lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb

Constant Summary collapse

CLASS_METHOD_DELIMITER =
'.'.freeze
INSTANCE_METHOD_DELIMITER =
'#'.freeze
LEGACY_DJ_FORMAT_DELIMITER =
';'.freeze
LEGACY_DJ_FORMAT_PREFIX =
'LOAD'.freeze
LEGACY_DJ_DEFAULT_CLASS =
'(unknown class)'.freeze

Class Method Summary collapse

Class Method Details

.class_name_from_legacy_performable_method(payload_object) ⇒ Object

If parsing for the class name fails, return a sensible default



41
42
43
# File 'lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb', line 41

def class_name_from_legacy_performable_method(payload_object)
  payload_object.object.split(LEGACY_DJ_FORMAT_DELIMITER)[1] || LEGACY_DJ_DEFAULT_CLASS
end

.delimiter(payload_object) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb', line 55

def delimiter(payload_object)
  if payload_object.object.is_a?(Class)
    CLASS_METHOD_DELIMITER
  else
    INSTANCE_METHOD_DELIMITER
  end
end

.legacy_performable_method?(payload_object) ⇒ Boolean

Older versions of Delayed Job use a semicolon-delimited string to stash the class name. The format of this string is “LOAD;<class name>;<ORM ID>”

Returns:

  • (Boolean)


36
37
38
# File 'lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb', line 36

def legacy_performable_method?(payload_object)
  payload_object.object.is_a?(String) && payload_object.object.start_with?(LEGACY_DJ_FORMAT_PREFIX)
end

.method_name(payload_object) ⇒ Object

DelayedJob’s interface for the async method’s name varies across the gem’s versions



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

def method_name(payload_object)
  if payload_object.respond_to?(:method_name)
    payload_object.method_name
  else
    # early versions of Delayed Job override Object#method with the method name
    payload_object.method
  end
end

.name_from_payload(payload_object) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb', line 23

def name_from_payload(payload_object)
  if payload_object.is_a?(::Delayed::PerformableMethod)
    # payload_object contains a reference to an object
    # that received an asynchronous method call via .delay or .handle_asynchronously
    "#{object_name(payload_object)}#{delimiter(payload_object)}#{method_name(payload_object)}"
  else
    # payload_object is a user-defined job enqueued via Delayed::Job.enqueue
    payload_object.class.name
  end
end

.object_name(payload_object) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb', line 45

def object_name(payload_object)
  if payload_object.object.is_a?(Class)
    payload_object.object.to_s
  elsif legacy_performable_method?(payload_object)
    class_name_from_legacy_performable_method(payload_object)
  else
    payload_object.object.class.name
  end
end