diff --git a/lib/datadog/tracing/contrib/active_job/log_injection.rb b/lib/datadog/tracing/contrib/active_job/log_injection.rb index 193f010a148..984b34bf008 100644 --- a/lib/datadog/tracing/contrib/active_job/log_injection.rb +++ b/lib/datadog/tracing/contrib/active_job/log_injection.rb @@ -6,17 +6,31 @@ module Contrib module ActiveJob # Active Job log injection wrapped around job execution module LogInjection - def self.included(base) - base.class_eval do - around_perform do |_, block| - if Datadog.configuration.tracing.log_injection && logger.respond_to?(:tagged) - logger.tagged(Tracing.log_correlation, &block) - else - block.call + # Active Job 4 / 5 don't execute `perform_now` at the right point, so we do best effort log correlation tagging + module ActiveJob4 + def self.included(base) + base.class_eval do + around_perform do |_, block| + if Datadog.configuration.tracing.log_injection && logger.respond_to?(:tagged) + logger.tagged(Tracing.log_correlation, &block) + else + block.call + end end end end end + + # Active Job 6+ executes `perform_now` at the right point, so we can provide better log correlation tagging + module ActiveJob6 + def perform_now + if Datadog.configuration.tracing.log_injection && logger.respond_to?(:tagged) + logger.tagged(Tracing.log_correlation) { super } + else + super + end + end + end end end end diff --git a/lib/datadog/tracing/contrib/active_job/patcher.rb b/lib/datadog/tracing/contrib/active_job/patcher.rb index 15f74e356ff..2dca34a004c 100644 --- a/lib/datadog/tracing/contrib/active_job/patcher.rb +++ b/lib/datadog/tracing/contrib/active_job/patcher.rb @@ -26,7 +26,11 @@ def patch def inject_log_correlation ::ActiveSupport.on_load(:active_job) do - include LogInjection + if target_version < Gem::Version.new('6.0.0') + include LogInjection::ActiveJob4 + else + include LogInjection::ActiveJob6 + end end end end