diff --git a/lib/datadog.rb b/lib/datadog.rb index 034d928148e..4f872ad22d3 100644 --- a/lib/datadog.rb +++ b/lib/datadog.rb @@ -7,5 +7,9 @@ # Load other products (must follow tracing) require_relative 'datadog/profiling' require_relative 'datadog/appsec' -require_relative 'datadog/di' +# Line probes will not work on Ruby < 2.6 because of lack of :script_compiled +# trace point. Only load DI on supported Ruby versions. +if RUBY_VERSION >= '2.6' + require_relative 'datadog/di' +end require_relative 'datadog/kit' diff --git a/lib/datadog/di.rb b/lib/datadog/di.rb index 3e2aa895fb2..931d6806753 100644 --- a/lib/datadog/di.rb +++ b/lib/datadog/di.rb @@ -86,6 +86,20 @@ def component end end -# Activate code tracking by default because line trace points will not work -# without it. -Datadog::DI.activate_tracking! +# :script_compiled trace point was added in Ruby 2.6. +if RUBY_VERSION >= '2.6' + begin + # Activate code tracking by default because line trace points will not work + # without it. + Datadog::DI.activate_tracking! + rescue => exc + if defined?(Datadog.logger) + Datadog.logger.warn("Failed to activate code tracking for DI: #{exc.class}: #{exc}" + else + # We do not have Datadog logger potentially because DI code tracker is + # being loaded early in application boot process and the rest of datadog + # wasn't loaded yet. Output to standard error. + warn("Failed to activate code tracking for DI: #{exc.class}: #{exc}" + end + end +end