Sha256: f7938e5d21ada0d3cd2482be0a6688417baf10ee6525425853d2818eae2dabc8

Contents?: true

Size: 1.48 KB

Versions: 9

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Datadog
  module Profiling
    module Tasks
      # Wraps command with Datadog profiling
      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 "ddprofrb exec failed: #{e.class.name} #{e.message} (command was '#{args.join(" ")}')"
          Kernel.exit 127
        rescue Errno::EACCES, Errno::ENOEXEC => e
          Kernel.warn "ddprofrb exec failed: #{e.class.name} #{e.message} (command was '#{args.join(" ")}')"
          Kernel.exit 126
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
datadog-2.10.0 lib/datadog/profiling/tasks/exec.rb
datadog-2.9.0 lib/datadog/profiling/tasks/exec.rb
datadog-2.8.0 lib/datadog/profiling/tasks/exec.rb
datadog-2.7.1 lib/datadog/profiling/tasks/exec.rb
datadog-2.7.0 lib/datadog/profiling/tasks/exec.rb
datadog-2.6.0 lib/datadog/profiling/tasks/exec.rb
datadog-2.5.0 lib/datadog/profiling/tasks/exec.rb
datadog-2.4.0 lib/datadog/profiling/tasks/exec.rb
datadog-2.3.0 lib/datadog/profiling/tasks/exec.rb