Skip to content

Commit a08b76f

Browse files
committed
Title the Que enqueue events with the job
The `enqueue.que` event now carries an "enqueue <Class> job" title, and `bulk_enqueue.que` a "bulk enqueue <Class> jobs" title, so the timeline shows which job was enqueued. The class is resolved the way Que resolves it: an explicit `:job_class`, else the class the enqueue was called on. For a bulk enqueue called on `Que::Job` itself the class isn't known up front, so the title is left class-less.
1 parent b1cf340 commit a08b76f

2 files changed

Lines changed: 31 additions & 8 deletions

File tree

lib/appsignal/integrations/que.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def _run(*args)
4343
# from within a web request or another job); otherwise it's a transparent
4444
# pass-through.
4545
module QueClientPlugin
46-
def enqueue(*_args, **_rest)
46+
def enqueue(*_args, job_options: {}, **_rest)
4747
# Inside a Que 2 `bulk_enqueue` block the batch is recorded once by the
4848
# `bulk_enqueue` wrapper, so each inner enqueue is a pass-through to
4949
# avoid recording an event per job.
@@ -54,7 +54,10 @@ def enqueue(*_args, **_rest)
5454
return super if Appsignal::Transaction.current? &&
5555
Appsignal::Transaction.current.job_enqueue_events_suppressed?
5656

57-
Appsignal.instrument("enqueue.que") { super }
57+
# Resolve the job class the way Que does: an explicit `:job_class`, else
58+
# the class `enqueue` was called on.
59+
title = "enqueue #{job_options[:job_class] || name} job"
60+
Appsignal.instrument("enqueue.que", title) { super }
5861
end
5962

6063
private
@@ -71,13 +74,27 @@ def bulk_insert_in_progress?
7174
# `bulk_enqueue` on Que versions that have none. The whole batch records a
7275
# single `bulk_enqueue.que` event; the inner enqueues are pass-throughs.
7376
module QueBulkClientPlugin
74-
def bulk_enqueue(*_args, **_rest)
77+
def bulk_enqueue(*_args, job_options: {}, **_rest)
7578
# Under Active Job the enqueue is already recorded as an
7679
# `enqueue.active_job` event, so skip recording it again here.
7780
return super if Appsignal::Transaction.current? &&
7881
Appsignal::Transaction.current.job_enqueue_events_suppressed?
7982

80-
Appsignal.instrument("bulk_enqueue.que") { super }
83+
Appsignal.instrument("bulk_enqueue.que", bulk_enqueue_title(job_options)) { super }
84+
end
85+
86+
private
87+
88+
# The batch's job class is known up front only from an explicit
89+
# `:job_class` or when `bulk_enqueue` is called on a concrete subclass;
90+
# called on `Que::Job` itself the class isn't known until the inner
91+
# enqueues run, so the title is left class-less.
92+
def bulk_enqueue_title(job_options)
93+
job_class = job_options[:job_class]
94+
job_class ||= name unless equal?(::Que::Job)
95+
return "bulk enqueue jobs" unless job_class
96+
97+
"bulk enqueue #{job_class} jobs"
8198
end
8299
end
83100
end

spec/lib/appsignal/integrations/que_spec.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,10 @@ def enqueue(tags: ["user:42"])
225225

226226
enqueue
227227

228-
event_names = transaction.to_h["events"].map { |event| event["name"] }
229-
expect(event_names).to include("enqueue.que")
228+
# Records an enqueue event on the transaction, titled after the job.
229+
event = transaction.to_h["events"].find { |e| e["name"] == "enqueue.que" }
230+
expect(event).to_not be_nil
231+
expect(event["title"]).to eq("enqueue MyQueJob job")
230232
expect(enqueued_tags).to eq(["user:42"])
231233
end
232234
end
@@ -280,9 +282,13 @@ def bulk_enqueue(tags: ["user:42"])
280282

281283
bulk_enqueue
282284

285+
# One event for the whole batch, titled after the job -- the inner
286+
# enqueues don't add their own.
287+
bulk_events =
288+
transaction.to_h["events"].select { |e| e["name"] == "bulk_enqueue.que" }
289+
expect(bulk_events.size).to eq(1)
290+
expect(bulk_events.first["title"]).to eq("bulk enqueue MyQueJob jobs")
283291
event_names = transaction.to_h["events"].map { |event| event["name"] }
284-
# One event for the whole batch -- the inner enqueues don't add their own.
285-
expect(event_names.count { |name| name == "bulk_enqueue.que" }).to eq(1)
286292
expect(event_names).to_not include("enqueue.que")
287293
expect(enqueued_tags).to eq(["user:42"])
288294
end

0 commit comments

Comments
 (0)