Module: Appsignal::Hooks::ActiveJobHook::ActiveJobClassInstrumentation Private

Defined in:
lib/appsignal/hooks/active_job.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#execute(job) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/appsignal/hooks/active_job.rb', line 45

def execute(job)
  job_status = nil
  has_wrapper_transaction = Appsignal::Transaction.current?
  transaction =
    if has_wrapper_transaction
      Appsignal::Transaction.current
    else
      # No standalone integration started before ActiveJob integration.
      # We don't have a separate integration for this QueueAdapter like
      # we do for Sidekiq.
      #
      # Prefer job_id from provider, instead of ActiveJob's internal ID.
      Appsignal::Transaction.create(Appsignal::Transaction::BACKGROUND_JOB)
    end

  if transaction
    transaction.add_params_if_nil(job["arguments"])

    transaction_tags = ActiveJobHelpers.transaction_tags_for(job)
    transaction.add_tags(transaction_tags)

    transaction.set_action(ActiveJobHelpers.action_name(job))
  end

  super
rescue Exception => exception # rubocop:disable Lint/RescueException
  job_status = :failed
  transaction_set_error(transaction, exception)
  raise exception
ensure
  if transaction
    enqueued_at = job["enqueued_at"]
    if enqueued_at # Present in Rails 6 and up
      transaction.set_queue_start((Time.parse(enqueued_at).to_f * 1_000).to_i)
    end

    unless has_wrapper_transaction
      # Only complete transaction if ActiveJob is not wrapped in
      # another supported integration, such as Sidekiq.
      Appsignal::Transaction.complete_current!
    end
  end

  metrics = ActiveJobHelpers.metrics_for(job)
  metrics.each do |(metric_name, tags)|
    if job_status
      ActiveJobHelpers.increment_counter metric_name, 1,
        tags.merge(:status => job_status)
    end
    ActiveJobHelpers.increment_counter metric_name, 1,
      tags.merge(:status => :processed)
  end
end