Sha256: 4729b4ae406734241f4282b3de65539b7e96228f37ec426df8214bee44d1f2c0

Contents?: true

Size: 1.45 KB

Versions: 18

Compression:

Stored size: 1.45 KB

Contents

module Datadog
  module Profiling
    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
          [
            '-rdatadog/profiling/preload'
          ]
        end

        private

        def set_rubyopt!
          existing_rubyopt = ENV['RUBYOPT']

          ENV['RUBYOPT'] = existing_rubyopt ? "#{existing_rubyopt} #{rubyopts.join(' ')}" : rubyopts.join(' ')
        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.class.name} #{e.message} (command was '#{args.join(' ')}')"
          Kernel.exit 127
        rescue Errno::EACCES, Errno::ENOEXEC => e
          Kernel.warn "ddtracerb exec failed: #{e.class.name} #{e.message} (command was '#{args.join(' ')}')"
          Kernel.exit 126
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
ddtrace-1.20.0 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.19.0 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.18.0 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.17.0 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.16.2 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.16.1 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.16.0 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.15.0 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.14.0 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.13.1 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.13.0 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.12.1 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.12.0 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.11.1 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.11.0 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.11.0.beta1 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.10.1 lib/datadog/profiling/tasks/exec.rb
ddtrace-1.10.0 lib/datadog/profiling/tasks/exec.rb