diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb index be8f68731a5..a548fb200e6 100644 --- a/lib-injection/auto_inject.rb +++ b/lib-injection/auto_inject.rb @@ -1,12 +1,15 @@ -return if ENV['skip_autoinject'] +return if ENV['DD_TRACE_SKIP_LIB_INJECTION'] == 'true' begin require 'open3' - _, status = Open3.capture2e({'skip_autoinject' => 'true'}, 'bundle show ddtrace') + failure_prefix = 'Datadog lib injection failed:' + support_message = 'For help solving this issue, please contact Datadog support at https://docs.datadoghq.com/help/.' + + _, status = Open3.capture2e({'DD_TRACE_SKIP_LIB_INJECTION' => 'true'}, 'bundle show ddtrace') if status.success? - puts '[DATADOG LIB INJECTION] ddtrace already installed... skipping auto-injection' + STDOUT.puts '[ddtrace] ddtrace already installed... skipping auto-injection' if ENV['DD_TRACE_DEBUG'] == 'true' return end @@ -14,10 +17,11 @@ require 'shellwords' if Bundler.frozen_bundle? - puts "[DATADOG LIB INJECTION] Cannot inject with frozen Bundler" + STDERR.puts "[ddtrace] #{failure_prefix} Cannot inject with frozen Gemfile, run `bundle config unset deployment` to allow lib injection. To learn more about bundler deployment, check https://bundler.io/guides/deploying.html#deploying-your-application. #{support_message}" return end + # `version` and `sha` should be replaced by docker build arguments version = "" sha = "" @@ -31,22 +35,17 @@ end unless bundle_add_ddtrace_cmd - puts "[DATADOG LIB INJECTION] NO VERSION" + STDERR.puts "[ddtrace] #{failure_prefix} Missing version specification. #{support_message}" return end - puts "[DATADOG LIB INJECTION] ddtrace is not installed... Perform lib injection for dd-trace-rb." + STDOUT.puts "[ddtrace] Performing lib injection with `#{bundle_add_ddtrace_cmd}`" if ENV['DD_TRACE_DEBUG'] == 'true' gemfile = Bundler::SharedHelpers.default_gemfile lockfile = Bundler::SharedHelpers.default_lockfile - if gemfile.basename.to_s == 'gems.rb' - datadog_gemfile = gemfile.dirname + "datadog-Gemfile" - datadog_lockfile = lockfile.dirname + "datadog-Gemfile.lock" - else - datadog_gemfile = gemfile.dirname + ("datadog-#{gemfile.basename}") - datadog_lockfile = lockfile.dirname + ("datadog-#{lockfile.basename}") - end + datadog_gemfile = gemfile.dirname + "datadog-Gemfile" + datadog_lockfile = lockfile.dirname + "datadog-Gemfile.lock" require 'fileutils' @@ -56,29 +55,23 @@ FileUtils.cp lockfile, datadog_lockfile output, status = Open3.capture2e( - { 'skip_autoinject' => 'true', 'BUNDLE_GEMFILE' => datadog_gemfile.to_s }, + { 'DD_TRACE_SKIP_LIB_INJECTION' => 'true', 'BUNDLE_GEMFILE' => datadog_gemfile.to_s }, bundle_add_ddtrace_cmd ) if status.success? - puts '[DATADOG LIB INJECTION] ddtrace added to bundle...' + STDOUT.puts '[ddtrace] Datadog lib injection successfully added ddtrace to the application.' FileUtils.cp datadog_gemfile, gemfile FileUtils.cp datadog_lockfile, lockfile else - puts "[DATADOG LIB INJECTION] Unable to add ddtrace: #{output}" + STDERR.puts "[ddtrace] #{failure_prefix} Unable to add ddtrace. Error output:\n#{output.split("\n").map {|l| "[ddtrace] #{l}"}.join("\n")}\n#{support_message}" end - rescue => e - puts "[DATADOG LIB INJECTION] trial error: #{e}" ensure # Remove the copies FileUtils.rm datadog_gemfile FileUtils.rm datadog_lockfile end -rescue Bundler::BundlerError => e - puts "[DATADOG LIB INJECTION] BundlerError: #{e}" -rescue LoadError => e - puts "[DATADOG LIB INJECTION] LoadError: #{e}" rescue Exception => e - puts "[DATADOG LIB INJECTION] Exception: #{e}" + STDERR.puts "[ddtrace] #{failure_prefix} #{e.class.name} #{e.message}\nBacktrace: #{e.backtrace.join("\n")}\n#{support_message}" end