15
16
17
18
19
20
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
|
# File 'lib/beacon/integrations/active_job.rb', line 15
def self.install(client: Beacon.client, config: Beacon.config)
unless defined?(::ActiveSupport::Notifications)
warn "[beacon] ActiveJob integration requires ActiveSupport — skipping"
return
end
ambient = config.ambient
::ActiveSupport::Notifications.subscribe("perform.active_job") do |_name, started, finished, _id, payload|
begin
duration_ms = ((finished - started) * 1000).to_i
job = payload[:job]
client.push({
kind: :perf,
name: job.class.name,
created_at_ns: Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond),
properties: {
duration_ms: duration_ms,
queue: job.queue_name,
success: payload[:exception_object].nil?,
},
})
if ambient
client.push({
kind: :ambient,
name: "job_lifecycle",
created_at_ns: Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond),
properties: {
class: job.class.name,
queue: job.queue_name,
duration_ms: duration_ms,
status: payload[:exception_object].nil? ? "success" : "failure",
},
})
end
rescue => e
warn "[beacon] active_job subscriber rescued #{e.class}: #{e.message}"
end
end
end
|