Skip to content

Commit 2c14a0f

Browse files
Extract ActiveJob::Instrumentation
1 parent 41441cd commit 2c14a0f

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

activejob/lib/active_job/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
require "active_job/exceptions"
1111
require "active_job/log_subscriber"
1212
require "active_job/logging"
13+
require "active_job/instrumentation"
1314
require "active_job/timezones"
1415
require "active_job/translation"
1516

@@ -69,6 +70,7 @@ class Base
6970
include Callbacks
7071
include Exceptions
7172
include Logging
73+
include Instrumentation
7274
include Timezones
7375
include Translation
7476

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveJob
4+
module Instrumentation #:nodoc:
5+
extend ActiveSupport::Concern
6+
7+
included do
8+
around_enqueue do |job, block|
9+
if job.scheduled_at
10+
ActiveSupport::Notifications.instrument("enqueue_at.active_job",
11+
adapter: job.class.queue_adapter, job: job, &block)
12+
else
13+
ActiveSupport::Notifications.instrument("enqueue.active_job",
14+
adapter: job.class.queue_adapter, job: job, &block)
15+
end
16+
end
17+
18+
around_perform do |job, block|
19+
payload = { adapter: job.class.queue_adapter, job: job }
20+
ActiveSupport::Notifications.instrument("perform_start.active_job", payload.dup)
21+
ActiveSupport::Notifications.instrument("perform.active_job", payload) do
22+
block.call
23+
end
24+
end
25+
end
26+
end
27+
end

activejob/lib/active_job/logging.rb

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,8 @@ module Logging #:nodoc:
1111
included do
1212
cattr_accessor :logger, default: ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
1313

14-
around_enqueue do |_, block|
15-
tag_logger do
16-
block.call
17-
end
18-
end
19-
20-
around_perform do |job, block|
21-
tag_logger(job.class.name, job.job_id) do
22-
payload = { adapter: job.class.queue_adapter, job: job }
23-
ActiveSupport::Notifications.instrument("perform_start.active_job", payload.dup)
24-
ActiveSupport::Notifications.instrument("perform.active_job", payload) do
25-
block.call
26-
end
27-
end
28-
end
29-
30-
around_enqueue do |job, block|
31-
if job.scheduled_at
32-
ActiveSupport::Notifications.instrument("enqueue_at.active_job",
33-
adapter: job.class.queue_adapter, job: job, &block)
34-
else
35-
ActiveSupport::Notifications.instrument("enqueue.active_job",
36-
adapter: job.class.queue_adapter, job: job, &block)
37-
end
38-
end
14+
around_enqueue { |_, block| tag_logger(&block) }
15+
around_perform { |job, block| tag_logger(job.class.name, job.job_id, &block) }
3916
end
4017

4118
private

0 commit comments

Comments
 (0)