lib/contrast/agent/patching/policy/patcher.rb in contrast-agent-4.6.0 vs lib/contrast/agent/patching/policy/patcher.rb in contrast-agent-4.7.0

- old
+ new

@@ -11,11 +11,12 @@ require 'contrast/utils/class_util' # assess require 'contrast/agent/assess/policy/policy' require 'contrast/agent/assess/policy/policy_scanner' -require 'contrast/agent/assess/policy/rewriter_patch' +# TODO: RUBY-714 remove guard w/ EOL of 2.5 +require 'contrast/agent/assess/policy/rewriter_patch' if RUBY_VERSION < '2.6.0' require 'contrast/agent/assess/policy/source_method' require 'contrast/agent/assess/policy/trigger_method' # deadzone require 'contrast/agent/deadzone/policy/policy' @@ -51,11 +52,11 @@ # instrumentation of the application - this only occurs once, during # startup to catchup on everything we didn't see get loaded def patch catchup_after_load_patches catchup_loaded_methods - Contrast::Agent::Assess::Policy::RewriterPatch.rewrite_interpolations + Contrast::Agent::Assess::Policy::RewriterPatch.rewrite_interpolations if RUBY_VERSION < '2.6.0' # TODO: RUBY-714 remove guard w/ EOL of 2.5 end # Hook to only monkeypatch Contrast. This will not trigger any # other functions, like rewriting or scanning. Exposed for those # situations, like ActiveRecord dynamically defining functions, @@ -83,11 +84,11 @@ return unless Contrast::Utils::ClassUtil.truly_defined?(mod_name) return if AGENT.skip_instrumentation?(mod_name) load_patches_for_module(mod_name) - return unless all_module_names.any? { |name| name == mod_name } + return unless all_module_names.any?(mod_name) module_data = Contrast::Agent::ModuleData.new(mod, mod_name) patch_into_module(module_data) all_module_names.delete(mod_name) if status_type.get_status(mod).patched? rescue StandardError => e @@ -141,11 +142,12 @@ end # @return [Array<String>] the names of all the Modules for which # there patches in our policies def all_module_names - @_all_module_names ||= POLICIES.each_with_object(Set.new) { |policy, set| set.merge(policy.instance.module_names) } + @_all_module_names ||= + POLICIES.each_with_object(Set.new) { |policy, set| set.merge(policy.instance.module_names) } end # Hook to only monkeypatch Contrast. This will not trigger any # other functions, like rewriting or scanning. This method should # only be invoked by the patch_methods method above in order to @@ -177,35 +179,34 @@ status = status_type.get_status(module_data.mod) return if (status&.patched? || status&.patching?) && !redo_patch # Begin patching our sources into the given module. Any patcher that has the name of the module will be # evaluated for patching. Find all the patchers that apply to this class, sorted by type. - module_policy = Contrast::Agent::Patching::Policy::ModulePolicy.create_module_policy(module_data.name) + module_policy = Contrast::Agent::Patching::Policy::ModulePolicy.create_module_policy(module_data.mod_name) # If there's nothing to match, then set that status and exit if module_policy.empty? status.no_patch! return end status.patching! num_applied_patches = patch_into_instance_methods(module_data, module_policy) num_applied_patches += patch_into_singleton_methods(module_data, module_policy) - if adjust_for_prepend(module_data) || - module_policy.num_expected_patches == num_applied_patches + if adjust_for_prepend(module_data) || module_policy.num_expected_patches == num_applied_patches status.patched! else status.partial_patch! end rescue StandardError => e status&.failed_patch! - logger.warn('Patching failed', e, module: module_data.name) + logger.warn('Patching failed', e, module: module_data.mod_name) ensure logger.trace('Patching complete', - module: module_data.name, - result: Contrast::Agent::Patching::Policy::PatchStatus.get_status( - module_data.mod).patch_status) + module: module_data.mod_name, + result: + Contrast::Agent::Patching::Policy::PatchStatus.get_status(module_data.mod).patch_status) end # Get all of the instance methods on the given module, excluding # those from super classes. this list will always include the # initialize method @@ -268,10 +269,12 @@ # this module. # @return [Integer] number of methods patched. def patch_into_methods mod, methods, module_policy, is_instance_method count = 0 methods.each do |method| - method_policy = Contrast::Agent::Patching::Policy::MethodPolicy.build_method_policy(method, module_policy, is_instance_method) + method_policy = Contrast::Agent::Patching::Policy::MethodPolicy.build_method_policy(method, + module_policy, + is_instance_method) next if method_policy.empty? patched = patch_method(mod, methods, method_policy) count += 1 if patched end