Sha256: 1c8e093a9897060dd7a13674e23faf2122d10fcb633b695c9b712ab11d544691

Contents?: true

Size: 1.85 KB

Versions: 22

Compression:

Stored size: 1.85 KB

Contents

require_relative '../../core/utils/only_once'
require_relative '../ext/forking'

module Datadog
  module Profiling
    module Tasks
      # Takes care of loading our extensions/monkey patches to handle fork() and validating if CPU-time profiling is usable
      class Setup
        ACTIVATE_EXTENSIONS_ONLY_ONCE = Core::Utils::OnlyOnce.new

        def run
          ACTIVATE_EXTENSIONS_ONLY_ONCE.run do
            begin
              activate_forking_extensions
              setup_at_fork_hooks
            rescue StandardError, ScriptError => e
              Datadog.logger.warn do
                "Profiler extensions unavailable. Cause: #{e.class.name} #{e.message} " \
                "Location: #{Array(e.backtrace).first}"
              end
            end
          end
        end

        private

        def activate_forking_extensions
          if Ext::Forking.supported?
            Ext::Forking.apply!
          elsif Datadog.configuration.profiling.enabled
            Datadog.logger.debug('Profiler forking extensions skipped; forking not supported.')
          end
        rescue StandardError, ScriptError => e
          Datadog.logger.warn do
            "Profiler forking extensions unavailable. Cause: #{e.class.name} #{e.message} " \
            "Location: #{Array(e.backtrace).first}"
          end
        end

        def setup_at_fork_hooks
          if Process.respond_to?(:at_fork)
            Process.at_fork(:child) do
              begin
                # Restart profiler, if enabled
                Profiling.start_if_enabled
              rescue StandardError => e
                Datadog.logger.warn do
                  "Error during post-fork hooks. Cause: #{e.class.name} #{e.message} " \
                  "Location: #{Array(e.backtrace).first}"
                end
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
ddtrace-1.23.3 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.23.2 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.23.1 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.22.0 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.21.1 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.21.0 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.20.0 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.19.0 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.18.0 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.17.0 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.16.2 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.16.1 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.16.0 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.15.0 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.14.0 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.13.1 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.13.0 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.12.1 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.12.0 lib/datadog/profiling/tasks/setup.rb
ddtrace-1.11.1 lib/datadog/profiling/tasks/setup.rb