lib/new_relic/agent/configuration/manager.rb in newrelic_rpm-3.8.1.221 vs lib/new_relic/agent/configuration/manager.rb in newrelic_rpm-3.9.0.229

- old
+ new

@@ -11,14 +11,26 @@ module NewRelic module Agent module Configuration class Manager - extend Forwardable - def_delegators :@cache, :[], :has_key?, :keys - attr_reader :config_stack, :stripped_exceptions_whitelist + attr_reader :stripped_exceptions_whitelist + # Defining these explicitly saves object allocations that we incur + # if we use Forwardable and def_delegators. + def [](key) + @cache[key] + end + + def has_key?(key) + @cache.has_key?[key] + end + + def keys + @cache.keys + end + def initialize reset_to_defaults @callbacks = Hash.new {|hash,key| hash[key] = [] } register_callback(:'strip_exception_messages.whitelist') do |whitelist| @@ -28,55 +40,77 @@ @stripped_exceptions_whitelist = [] end end end - def apply_config(source, level=0) - was_finished = finished_configuring? - + def add_config_for_testing(source, level=0) + raise 'Invalid config type for testing' unless [Hash, DottedHash].include?(source.class) invoke_callbacks(:add, source) - @config_stack.insert(level, source.freeze) + @configs_for_testing << [source.freeze, level] reset_cache log_config(:add, source) + end - notify_finished_configuring if !was_finished && finished_configuring? + def remove_config_type(sym) + source = case sym + when :environment then @environment_source + when :server then @server_source + when :manual then @manual_source + when :yaml then @yaml_source + when :default then @default_source + end + + remove_config(source) end - def remove_config(source=nil) - @config_stack.delete_if do |c| - if block_given? - yield c - else - c.class == source.class && c == source - end + def remove_config(source) + case source + when EnvironmentSource then @environment_source = nil + when ServerSource then @server_source = nil + when ManualSource then @manual_source = nil + when YamlSource then @yaml_source = nil + when DefaultSource then @default_source = nil + else + @configs_for_testing.delete_if {|src,lvl| src == source} end reset_cache invoke_callbacks(:remove, source) log_config(:remove, source) end - def replace_or_add_config(source, level=0) - index = config_stack_index_for(source.class) - @config_stack.delete_at(index) if index - apply_config(source, index || level) - end + def replace_or_add_config(source) + source.freeze + was_finished = finished_configuring? - def config_stack_index_for(source_class) - @config_stack.map{|s| s.class}.index(source_class) + invoke_callbacks(:add, source) + case source + when EnvironmentSource then @environment_source = source + when ServerSource then @server_source = source + when ManualSource then @manual_source = source + when YamlSource then @yaml_source = source + when DefaultSource then @default_source = source + else + NewRelic::Agent.logger.warn("Invalid config format; config will be ignored: #{source}") + end + + reset_cache + log_config(:add, source) + + notify_finished_configuring if !was_finished && finished_configuring? end def source(key) - @config_stack.each do |config| + config_stack.each do |config| if config.respond_to?(key.to_sym) || config.has_key?(key.to_sym) return config end end end def fetch(key) - @config_stack.each do |config| + config_stack.each do |config| next unless config accessor = key.to_sym if config.has_key?(accessor) if config[accessor].respond_to?(:call) return instance_eval(&config[accessor]) @@ -111,15 +145,15 @@ def notify_finished_configuring NewRelic::Agent.instance.events.notify(:finished_configuring) end def finished_configuring? - @config_stack.any? {|s| s.is_a?(ServerSource)} + !@server_source.nil? end def flattened - @config_stack.reverse.inject({}) do |flat,layer| + config_stack.reverse.inject({}) do |flat,layer| thawed_layer = layer.to_hash.dup thawed_layer.each do |k,v| begin thawed_layer[k] = instance_eval(&v) if v.respond_to?(:call) rescue => e @@ -151,11 +185,18 @@ end end # Generally only useful during initial construction and tests def reset_to_defaults - @config_stack = [ EnvironmentSource.new, DefaultSource.new ] + @environment_source = EnvironmentSource.new + @server_source = nil + @manual_source = nil + @yaml_source = nil + @default_source = DefaultSource.new + + @configs_for_testing = [] + reset_cache end def reset_cache @cache = Hash.new {|hash,key| hash[key] = self.fetch(key) } @@ -169,10 +210,43 @@ ::NewRelic::Agent.logger.debug do "Updating config (#{direction}) from #{source.class}. Results: #{flattened.inspect}" end end + def delete_all_configs_for_testing + @environment_source = nil + @server_source = nil + @manual_source = nil + @yaml_source = nil + @default_source = nil + @configs_for_testing = [] + end + + def num_configs_for_testing + config_stack.size + end + + def config_classes_for_testing + config_stack.map(&:class) + end + private + + def config_stack + stack = [@environment_source, + @server_source, + @manual_source, + @yaml_source, + @default_source] + + stack.compact! + + @configs_for_testing.each do |config, index| + stack.insert(index, config) + end + + stack + end def parse_constant_list(list) list.split(/\s*,\s*/).map do |class_name| const = constantize(class_name)