Skip to content

Commit

Permalink
Extract Active Job metadata earlier
Browse files Browse the repository at this point in the history
In issue #779, I'm looking at supporting the Rails error reporter. For
this I want to copy the context (transaction namespace, action name and
tags) from the current transaction when an error is reported by Rails.

This will make determining the namespace and action name for the
transaction easier for Active Job jobs, in the error reporter.

To have the context available to the error handler, it needs to be set
on the transaction before the Active Job job is performed. I have moved
those parts of the instrumentation up to above the `super` call, and not
within the `ensure` block at the very end. The job will already know
what job is run and with what arguments at the start of the
instrumentation.

I can't apply the same improvement for Rails controllers. We only have
the necessary request env values to determine this the controller name
and action after the request has been processed, which is too late for
the error reporter.
  • Loading branch information
tombruijn committed Apr 5, 2023
1 parent 5673a2c commit e033279
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: "patch"
type: "change"
---

Set the AppSignal transaction namespace, action name and some tags, before Active Job jobs are performed. This allows us to check what the namespace, action name and some tags are during the instrumentation itself.
15 changes: 9 additions & 6 deletions lib/appsignal/hooks/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ def execute(job)
)
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(
Expand All @@ -59,6 +53,15 @@ def execute(job)
transaction.set_tags(transaction_tags)

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

super
rescue Exception => exception # rubocop:disable Lint/RescueException
job_status = :failed
transaction.set_error(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)
Expand Down

0 comments on commit e033279

Please sign in to comment.