diff --git a/lib/datadog/tracing/contrib/active_job/log_injection.rb b/lib/datadog/tracing/contrib/active_job/log_injection.rb index 193f010a148..3b4c1cb9238 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 AroundPerformPatch + 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 PerformNowPatch + 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..031d68ff1fe 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 ::ActiveJob.gem_version < Gem::Version.new('6.0.0') + include LogInjection::AroundPerformPatch + else + include LogInjection::PerformNowPatch + end end end end