Skip to content

Commit 723c0f6

Browse files
committed
Honor appsignal_name in Delayed Job enqueue title
The invoke side already lets a Delayed Job payload override its action name with an `appsignal_name` method. Honor that same override when titling the `enqueue.delayed_job` event, so the enqueue and perform events stay tied to the same name. The override is a full action name, so an enqueue that uses it reads as `enqueue Class#method job` rather than the bare `enqueue Class job` we use otherwise. We accept that inconsistency for the rare job that sets it, since keeping the enqueue and perform names aligned matters more than the title's shape.
1 parent 0a63cdd commit 723c0f6

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

lib/appsignal/integrations/delayed_job_plugin.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,25 @@ def self.enqueue_with_instrumentation(job, block)
3232
return block.call(job)
3333
end
3434

35-
Appsignal.instrument("enqueue.delayed_job", "enqueue #{job.name} job") do
35+
Appsignal.instrument("enqueue.delayed_job", "enqueue #{enqueue_name(job)} job") do
3636
block.call(job)
3737
end
3838
end
3939

40+
# Titles the enqueue event after the job. The `appsignal_name` override is
41+
# honored verbatim, as it is when naming the perform action. That override
42+
# is a full action name, so an enqueue that uses it reads as
43+
# `enqueue Class#method job` rather than the bare `enqueue Class job`. We
44+
# accept that inconsistency so the enqueue and perform events stay tied to
45+
# the same name for the rare job that sets it.
46+
def self.enqueue_name(job)
47+
payload = job.payload_object
48+
appsignal_name = extract_value(payload, :appsignal_name, nil)
49+
return appsignal_name if appsignal_name.is_a?(String)
50+
51+
job.name
52+
end
53+
4054
def self.invoke_with_instrumentation(job, block)
4155
transaction =
4256
Appsignal::Transaction.create(Appsignal::Transaction::BACKGROUND_JOB)

spec/lib/appsignal/integrations/delayed_job_plugin_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ def perform_job(job)
4848
end
4949
end
5050

51+
context "with a custom appsignal_name" do
52+
before do
53+
stub_const("DelayedNamedJob", Class.new do
54+
def perform
55+
end
56+
57+
def appsignal_name
58+
"CustomName#perform"
59+
end
60+
end)
61+
end
62+
63+
it "titles the enqueue event with the custom name" do
64+
start_agent
65+
transaction = http_request_transaction
66+
set_current_transaction(transaction)
67+
68+
Delayed::Job.enqueue(DelayedNamedJob.new)
69+
70+
event = transaction.to_h["events"].find { |e| e["name"] == "enqueue.delayed_job" }
71+
expect(event["title"]).to eq("enqueue CustomName#perform job")
72+
end
73+
end
74+
5175
context "without an active transaction" do
5276
it "is a transparent pass-through" do
5377
start_agent

0 commit comments

Comments
 (0)