Sha256: fdc46c4ff41664a1dba1d3c5448f548b549b34402b7d5197a286593460995996

Contents?: true

Size: 1.32 KB

Versions: 5

Compression:

Stored size: 1.32 KB

Contents

module Datadog
  module Tasks
    # Wraps command with Datadog tracing
    class Exec
      attr_reader :args

      def initialize(args)
        @args = args
      end

      def run
        set_rubyopt!
        exec_with_error_handling(args)
      end

      def rubyopts
        [
          '-rddtrace/profiling/preload'
        ]
      end

      private

      def set_rubyopt!
        if ENV.key?('RUBYOPT')
          ENV['RUBYOPT'] += " #{rubyopts.join(' ')}"
        else
          ENV['RUBYOPT'] = rubyopts.join(' ')
        end
      end

      # If there's an error here, rather than throwing a cryptic stack trace, let's instead have clearer messages, and
      # follow the same status codes as the shell uses
      # See also:
      # * https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
      # * https://github.com/rubygems/rubygems/blob/dd93966cac224532035deda533cba2685dfa30cc/bundler/lib/bundler/cli/exec.rb#L45
      def exec_with_error_handling(args)
        Kernel.exec(*args)
      rescue Errno::ENOENT => e
        Kernel.warn "ddtracerb exec failed: #{e.message} (command was '#{args.join(' ')}')"
        Kernel.exit 127
      rescue Errno::EACCES, Errno::ENOEXEC => e
        Kernel.warn "ddtracerb exec failed: #{e.message} (command was '#{args.join(' ')}')"
        Kernel.exit 126
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ddtrace-0.51.1 lib/ddtrace/tasks/exec.rb
ddtrace-0.51.0 lib/ddtrace/tasks/exec.rb
ddtrace-0.50.0 lib/ddtrace/tasks/exec.rb
ddtrace-0.49.0 lib/ddtrace/tasks/exec.rb
ddtrace-0.48.0 lib/ddtrace/tasks/exec.rb