Module: OneApm::Agent::Instrumentation::ActiveJobHelper

Includes:
Support::MethodTracer
Defined in:
lib/one_apm/inst/background_job/active_job.rb

Constant Summary collapse

ADAPTER_REGEX =
/ActiveJob::QueueAdapters::(.*)Adapter/

Constants included from Support::MethodTracer::ClassMethods::AddMethodTracer

Support::MethodTracer::ClassMethods::AddMethodTracer::ALLOWED_KEYS, Support::MethodTracer::ClassMethods::AddMethodTracer::DEPRECATED_KEYS, Support::MethodTracer::ClassMethods::AddMethodTracer::OA_DEFAULT_SETTINGS

Class Method Summary collapse

Methods included from Support::MethodTracer

extended, included, #trace_execution_scoped, #trace_execution_unscoped

Methods included from Support::MethodTracer::ClassMethods

#add_method_tracer, #remove_method_tracer

Methods included from Support::MethodTracer::ClassMethods::AddMethodTracer

#assemble_code_header, #check_for_illegal_keys!, #check_for_push_scope_and_metric, #code_to_eval, #default_metric_name_code, #method_with_push_scope, #method_without_push_scope, #oneapm_method_exists?, #traced_method_exists?, #validate_options

Class Method Details

.adapterObject



75
76
77
78
79
80
81
82
# File 'lib/one_apm/inst/background_job/active_job.rb', line 75

def self.adapter
  name = if ::ActiveJob::Base.queue_adapter.respond_to?(:name) 
    ::ActiveJob::Base.queue_adapter.name
  else
    ::ActiveJob::Base.queue_adapter.to_s
  end
  clean_adapter_name(name)
end

.clean_adapter_name(name) ⇒ Object



86
87
88
89
# File 'lib/one_apm/inst/background_job/active_job.rb', line 86

def self.clean_adapter_name(name)
  name = "ActiveJob::#{$1}" if ADAPTER_REGEX =~ name
  name
end

.enqueue(job, block) ⇒ Object



29
30
31
# File 'lib/one_apm/inst/background_job/active_job.rb', line 29

def self.enqueue(job, block)
  run_in_trace(job, block, :Produce)
end

.perform(job, block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/one_apm/inst/background_job/active_job.rb', line 33

def self.perform(job, block)
  state = ::OneApm::TransactionState.tl_get

  # Don't nest transactions if we're already in a web transaction.
  # Probably inline processing the job if that happens, so just trace.
  if state.in_web_transaction?
    run_in_trace(job, block, :Consume)
  elsif state.in_background_transaction?
    ::OneApm::Transaction.set_default_transaction_name(
      transaction_name_suffix_for_job(job),
      transaction_category)
    block.call
  else
    run_in_transaction(state, job, block)
  end
end

.run_in_trace(job, block, event) ⇒ Object



50
51
52
53
54
# File 'lib/one_apm/inst/background_job/active_job.rb', line 50

def self.run_in_trace(job, block, event)
  trace_execution_scoped("MessageBroker/#{adapter}/Queue/#{event}/Named/#{job.queue_name}") do
    block.call
  end
end

.run_in_transaction(state, job, block) ⇒ Object



56
57
58
59
60
61
# File 'lib/one_apm/inst/background_job/active_job.rb', line 56

def self.run_in_transaction(state, job, block)
  ::OneApm::Transaction.wrap(state,
                                      transaction_name_for_job(job),
                                      :other,
                                      &block)
end

.transaction_categoryObject



63
64
65
# File 'lib/one_apm/inst/background_job/active_job.rb', line 63

def self.transaction_category
  "OtherTransaction/#{adapter}"
end

.transaction_name_for_job(job) ⇒ Object



71
72
73
# File 'lib/one_apm/inst/background_job/active_job.rb', line 71

def self.transaction_name_for_job(job)
  "#{transaction_category}/#{transaction_name_suffix_for_job(job)}"
end

.transaction_name_suffix_for_job(job) ⇒ Object



67
68
69
# File 'lib/one_apm/inst/background_job/active_job.rb', line 67

def self.transaction_name_suffix_for_job(job)
  "#{job.class}/execute"
end