class Skylight::Core::Normalizers::ActiveJob::Perform

Constants

CAT
DELAYED_JOB_WRAPPER
DELIVERY_JOB

Public Class Methods

normalize_title(job_instance) click to toggle source
# File lib/skylight/core/normalizers/active_job/perform.rb, line 10
def self.normalize_title(job_instance)
  job_instance.class.name.to_s.tap do |str|
    if str.match(DELIVERY_JOB)
      mailer_class, mailer_method, * = job_instance.arguments
      return ["#{mailer_class}##{mailer_method}", str]
    end
  end
end

Public Instance Methods

normalize(trace, _name, payload) click to toggle source
# File lib/skylight/core/normalizers/active_job/perform.rb, line 21
def normalize(trace, _name, payload)
  title = payload[:job].class.to_s
  adapter_name = normalize_adapter_name(payload[:adapter])
  desc = "{ adapter: '#{adapter_name}', queue: '#{payload[:job].queue_name}' }"

  maybe_set_endpoint(trace, payload)

  [CAT, title, desc]
end
normalize_after(trace, _span, _name, payload) click to toggle source
# File lib/skylight/core/normalizers/active_job/perform.rb, line 31
def normalize_after(trace, _span, _name, payload)
  return unless config.enable_segments? && assign_endpoint?(trace, payload)

  trace.segment = payload[:job].queue_name
end

Private Instance Methods

assign_endpoint?(trace, payload) click to toggle source
# File lib/skylight/core/normalizers/active_job/perform.rb, line 52
def assign_endpoint?(trace, payload)
  # Always assign the endpoint if it has not yet been assigned by the ActiveJob probe.
  return true unless trace.endpoint
  return true if defined?(Skylight::Core::Probes::ActiveJob::Probe::TITLE) &&
    trace.endpoint == Skylight::Core::Probes::ActiveJob::Probe::TITLE
  return true if defined?(SKylight::Core::Probes::DelayedJob::Probe::UNKNOWN) &&
    trace.endpoint == Skylight::Core::Probes::DelayedJob::Probe::UNKNOWN

  # If a job is called using #perform_now inside a controller action
  # or within another job's #perform method, we do not want this to
  # overwrite the existing endpoint name (unless it is the default from ActiveJob).
  #
  # If the current endpoint name matches this payload, return true to allow the
  # segment to be assigned by normalize_after.
  trace.endpoint == DELIVERY_JOB ||
    trace.endpoint == normalize_title(payload[:job]) ||
    # This adapter wrapper needs to be handled specifically due to interactions with the
    # standalone Delayed::Job probe, as there is no consistent way to get the wrapped
    # job name among all Delayed::Job backends.
    trace.endpoint == DELAYED_JOB_WRAPPER
end
maybe_set_endpoint(trace, payload) click to toggle source
# File lib/skylight/core/normalizers/active_job/perform.rb, line 46
def maybe_set_endpoint(trace, payload)
  if assign_endpoint?(trace, payload)
    trace.endpoint = normalize_title(payload[:job])
  end
end
normalize_adapter_name(adapter) click to toggle source
# File lib/skylight/core/normalizers/active_job/perform.rb, line 39
def normalize_adapter_name(adapter)
  adapter_string = adapter.is_a?(Class) ? adapter.to_s : adapter.class.to_s
  adapter_string[/ActiveJob::QueueAdapters::(\w+)Adapter/, 1].underscore
rescue
  "active_job"
end
normalize_title(job_instance) click to toggle source
# File lib/skylight/core/normalizers/active_job/perform.rb, line 74
def normalize_title(job_instance)
  title, * = self.class.normalize_title(job_instance)
  title
end