Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed Feb 28, 2023
1 parent befe4f7 commit 0499917
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions lib-injection/auto_inject.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
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

require 'bundler'
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 = "<DD_TRACE_VERSION_TO_BE_REPLACED>"
sha = "<DD_TRACE_SHA_TO_BE_REPLACED>"

Expand All @@ -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'

Expand All @@ -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

0 comments on commit 0499917

Please sign in to comment.