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.

[View source]

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
# File 'lib/appsignal/hooks/active_job.rb', line 21

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(
        job["provider_job_id"] || job["job_id"],
        Appsignal::Transaction::BACKGROUND_JOB,
        Appsignal::Transaction::GenericRequest.new({})
      )
    end

  super
rescue Exception => exception # rubocop:disable Lint/RescueException
  job_status = :failed
  transaction.set_error(exception)
  raise exception
ensure
  if transaction
    transaction.params =
      Appsignal::Utils::HashSanitizer.sanitize(
        job["arguments"],
        Appsignal.config[:filter_parameters]
      )

    transaction_tags = ActiveJobHelpers.transaction_tags_for(job)
    transaction_tags["active_job_id"] = job["job_id"]
    provider_job_id = job["provider_job_id"]
    if provider_job_id
      transaction_tags[:provider_job_id] = provider_job_id
    end
    transaction.set_tags(transaction_tags)

    transaction.set_action_if_nil(ActiveJobHelpers.action_name(job))
    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