Skip to content

Commit 66d9ae9

Browse files
authored
move DD log correlation patch for Active Job to wrap full execution (#4916)
1 parent 01568d4 commit 66d9ae9

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 AroundPerformPatch
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 PerformNowPatch
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 ::ActiveJob.gem_version < Gem::Version.new('6.0.0')
30+
include LogInjection::AroundPerformPatch
31+
else
32+
include LogInjection::PerformNowPatch
33+
end
3034
end
3135
end
3236
end

0 commit comments

Comments
 (0)