Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed Feb 27, 2023
1 parent e6baa62 commit 8d3e847
Showing 1 changed file with 62 additions and 47 deletions.
109 changes: 62 additions & 47 deletions lib-injection/auto_inject.rb
Original file line number Diff line number Diff line change
@@ -1,67 +1,82 @@
if !ENV['skip_autoinject']
if system 'skip_autoinject=true bundle show ddtrace'
puts 'ddtrace already installed... skipping auto-injection'

return if ENV['skip_autoinject']

if system 'skip_autoinject=true bundle show ddtrace'
puts '[DATADOG LIB INJECTION] ddtrace already installed... skipping auto-injection'
return
end

begin
require 'bundler'
# require 'shellwords'

if Bundler.frozen_bundle?
puts "[DATADOG LIB INJECTION] Cannot inject with frozen Bundler"
return
end

version = "<DD_TRACE_VERSION_TO_BE_REPLACED>"
sha = "<DD_TRACE_SHA_TO_BE_REPLACED>"

# Stronger restrict
condition = if !version.empty?
# For public release
"--version #{version.gsub(/^v/, '')}"
elsif !sha.empty?
# For internal testing
"--github datadog/dd-trace-rb --ref #{sha}"
end

unless condition
puts "[DATADOG LIB INJECTION] NO VERSION"
return
end

puts "[DATADOG LIB INJECTION] ddtrace is not installed... Perform lib injection for dd-trace-rb."

gemfile = nil
lockfile = nil

begin
gemfile = Bundler::SharedHelpers.default_gemfile
lockfile = Bundler::SharedHelpers.default_lockfile
rescue Bundler::BundlerError => e
puts "[DATADOG LIB INJECTION] #{e}"
return
end

if gemfile.basename.to_s == 'gems.rb'
datadog_gemfile = gemfile.dirname + "datadog-Gemfile"
datadog_lockfile = lockfile.dirname + "datadog-Gemfile.lock"
else
version = "<DD_TRACE_VERSION_TO_BE_REPLACED>"
sha = "<DD_TRACE_SHA_TO_BE_REPLACED>"

condition =
if !version.empty?
# For public release
"--version '#{version.gsub(/^v/, '')}'"
elsif !sha.empty?
# For internal testing
"--github 'datadog/dd-trace-rb' --ref '#{sha}'"
else
puts "NO VERSION"
end

puts "ddtrace is not installed... Perform auto-injection... for dd-trace-rb"

require 'bundler'
require 'fileutils'

gemfile = Bundler::SharedHelpers.default_gemfile
lockfile = Bundler::SharedHelpers.default_lockfile

puts gemfile
puts 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.basename}")
datadog_lockfile = lockfile.dirname + ("datadog-#{lockfile.basename}")
end

puts datadog_gemfile
puts datadog_lockfile
require 'fileutils'

begin
# Copies for trial
FileUtils.cp gemfile, datadog_gemfile
FileUtils.cp lockfile, datadog_lockfile

if system "skip_autoinject=true BUNDLE_GEMFILE=#{datadog_gemfile} bundle add ddtrace #{condition} --require ddtrace/auto_instrument"
puts 'ddtrace added to bundle...'
puts '[DATADOG LIB INJECTION] ddtrace added to bundle...'

# Trial success, replace the original
FileUtils.cp datadog_gemfile, gemfile
FileUtils.cp datadog_lockfile, lockfile
else
puts 'Something went wrong when adding ddtrace to bundle...'
puts '[DATADOG LIB INJECTION] Something went wrong when adding ddtrace to bundle...'
end

rescue => e
puts "[DATADOG LIB INJECTION] #{e}"
ensure
# Remove the copies
FileUtils.rm datadog_gemfile
FileUtils.rm datadog_lockfile
end

begin
require 'ddtrace'
rescue LoadError => e
puts e
else
puts 'ddtrace loaded...'
end
rescue LoadError => e
puts "[DATADOG LIB INJECTION] #{e}"
end

0 comments on commit 8d3e847

Please sign in to comment.