Sha256: c457254862b48da8ed70f1b7ef92d3a576ad89660a2753350b0235f35859fe07

Contents?: true

Size: 1.81 KB

Versions: 27

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

require 'forwardable'
require 'elastic_apm/util/inflector'

module ElasticAPM
  # @api private
  module Spies
    # @api private
    class Registration
      extend Forwardable

      def initialize(const_name, require_paths, spy)
        @const_name = const_name
        @require_paths = Array(require_paths)
        @spy = spy
      end

      attr_reader :const_name, :require_paths

      def_delegator :@spy, :install
    end

    def self.require_hooks
      @require_hooks ||= {}
    end

    def self.installed
      @installed ||= {}
    end

    def self.register(*args)
      registration = Registration.new(*args)

      if safe_defined?(registration.const_name)
        registration.install
        installed[registration.const_name] = registration
      else
        register_require_hook registration
      end
    end

    def self.register_require_hook(registration)
      registration.require_paths.each do |path|
        require_hooks[path] = registration
      end
    end

    def self.hook_into(name)
      return unless (registration = require_hooks[name])
      return unless safe_defined?(registration.const_name)

      installed[registration.const_name] = registration
      registration.install

      registration.require_paths.each do |path|
        require_hooks.delete path
      end
    end

    def self.safe_defined?(const_name)
      Util::Inflector.safe_constantize(const_name)
    end
  end
end

# @api private
module Kernel
  private

  alias require_without_apm require

  def require(path)
    res = require_without_apm(path)

    begin
      ElasticAPM::Spies.hook_into(path)
    rescue ::Exception => e
      puts "Failed hooking into '#{path}'. Please report this at " \
        'github.com/elastic/apm-agent-ruby'
      puts e.backtrace.join("\n")
    end

    res
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
elastic-apm-3.1.0 lib/elastic_apm/spies.rb
elastic-apm-3.0.0 lib/elastic_apm/spies.rb
elastic-apm-2.12.0 lib/elastic_apm/spies.rb
elastic-apm-2.11.0 lib/elastic_apm/spies.rb
elastic-apm-2.10.1 lib/elastic_apm/spies.rb
elastic-apm-2.10.0 lib/elastic_apm/spies.rb
elastic-apm-2.9.1 lib/elastic_apm/spies.rb
elastic-apm-2.9.0 lib/elastic_apm/spies.rb
elastic-apm-2.8.1 lib/elastic_apm/spies.rb
elastic-apm-2.8.0 lib/elastic_apm/spies.rb
elastic-apm-2.7.0 lib/elastic_apm/spies.rb
elastic-apm-2.6.1 lib/elastic_apm/spies.rb
elastic-apm-2.6.0 lib/elastic_apm/spies.rb
elastic-apm-2.5.0 lib/elastic_apm/spies.rb
elastic-apm-2.4.0 lib/elastic_apm/spies.rb
elastic-apm-2.3.1 lib/elastic_apm/spies.rb
elastic-apm-2.3.0 lib/elastic_apm/spies.rb
elastic-apm-2.2.0 lib/elastic_apm/spies.rb
elastic-apm-2.1.2 lib/elastic_apm/spies.rb
elastic-apm-2.1.1 lib/elastic_apm/spies.rb