Skip to content

Commit 7acecb3

Browse files
committed
move DD log correlation patch for Active Job to wrap full execution
The current Active Job integration injects a log correlation tag into the logger via `around_perform`, which is too late to pick up the following logs: ``` [ActiveJob] [FooJob] [540c4718-6668-4f3c-9411-ca52a4cffd15] Performing FooJob (Job ID: 540c4718-6668-4f3c-9411-ca52a4cffd15) from Sidekiq(default) enqueued at 2025-09-26T02:17:44.160363327Z with arguments: #<GlobalID:0x00007f0fae88cb50 @uri=#<URI::GID gid://foo-rails/Foo/019983cf-8422-72d5-85b7-f3dc09a4887e>> [ActiveJob] [FooJob] [540c4718-6668-4f3c-9411-ca52a4cffd15] Performed FooJob (Job ID: 540c4718-6668-4f3c-9411-ca52a4cffd15) from Sidekiq(default) in 990.29ms ``` Instead of using `around_perform`, we now do the same thing that [Active Job itself](https://github.com/rails/rails/blob/v8.0.3/activejob/lib/active_job/logging.rb#L31-L33) does to tag logs with `[ActiveJob]`, the job class name, and job ID, which is wrapping `perform_now`. Note that this only works on Active Job 6+, so we inject different log correlation logic depending on the Active Job version.
1 parent ce02077 commit 7acecb3

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

lib/datadog/tracing/contrib/active_job/log_injection.rb

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,31 @@ module Contrib
66
module ActiveJob
77
# Active Job log injection wrapped around job execution
88
module LogInjection
9-
def self.included(base)
10-
base.class_eval do
11-
around_perform do |_, block|
12-
if Datadog.configuration.tracing.log_injection && logger.respond_to?(:tagged)
13-
logger.tagged(Tracing.log_correlation, &block)
14-
else
15-
block.call
9+
# Active Job 4 / 5 don't execute `perform_now` at the right point, so we do best effort log correlation tagging
10+
module ActiveJob4
11+
def self.included(base)
12+
base.class_eval do
13+
around_perform do |_, block|
14+
if Datadog.configuration.tracing.log_injection && logger.respond_to?(:tagged)
15+
logger.tagged(Tracing.log_correlation, &block)
16+
else
17+
block.call
18+
end
1619
end
1720
end
1821
end
1922
end
23+
24+
# Active Job 6+ executes `perform_now` at the right point, so we can provide better log correlation tagging
25+
module ActiveJob6
26+
def perform_now
27+
if Datadog.configuration.tracing.log_injection && logger.respond_to?(:tagged)
28+
logger.tagged(Tracing.log_correlation) { super }
29+
else
30+
super
31+
end
32+
end
33+
end
2034
end
2135
end
2236
end

lib/datadog/tracing/contrib/active_job/patcher.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ def patch
2626

2727
def inject_log_correlation
2828
::ActiveSupport.on_load(:active_job) do
29-
include LogInjection
29+
if target_version < Gem::Version.new('6.0.0')
30+
include LogInjection::ActiveJob4
31+
else
32+
include LogInjection::ActiveJob6
33+
end
3034
end
3135
end
3236
end

0 commit comments

Comments
 (0)