From 7acecb3af10d71ec34e7de47624985cca9b9d57b Mon Sep 17 00:00:00 2001 From: Alex Robbin Date: Thu, 25 Sep 2025 22:26:29 -0400 Subject: [PATCH] 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: #> [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. --- .../contrib/active_job/log_injection.rb | 28 ++++++++++++++----- .../tracing/contrib/active_job/patcher.rb | 6 +++- 2 files changed, 26 insertions(+), 8 deletions(-) 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