Class: NewRelic::Agent::Configuration::Manager
- Inherits:
-
Object
- Object
- NewRelic::Agent::Configuration::Manager
- Extended by:
- Forwardable
- Defined in:
- lib/new_relic/agent/configuration/manager.rb
Instance Attribute Summary collapse
-
#config_stack ⇒ Object
readonly
mainly for testing.
Instance Method Summary collapse
- #app_names ⇒ Object
- #apply_config(source, level = 0) ⇒ Object
- #apply_mask(hash) ⇒ Object
- #fetch(key) ⇒ Object
- #finished_configuring? ⇒ Boolean
- #flattened ⇒ Object
-
#initialize ⇒ Manager
constructor
A new instance of Manager.
- #invoke_callbacks(direction, source) ⇒ Object
- #log_config(direction, source) ⇒ Object
- #notify_finished_configuring ⇒ Object
- #register_callback(key, &proc) ⇒ Object
- #remove_config(source = nil) ⇒ Object
- #replace_or_add_config(source, level = 0) ⇒ Object
- #reset_cache ⇒ Object
- #source(key) ⇒ Object
- #to_collector_hash ⇒ Object
Constructor Details
#initialize ⇒ Manager
Returns a new instance of Manager.
16 17 18 19 20 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 16 def initialize @config_stack = [ EnvironmentSource.new, DEFAULTS ] @cache = Hash.new {|hash,key| hash[key] = self.fetch(key) } @callbacks = Hash.new {|hash,key| hash[key] = [] } end |
Instance Attribute Details
#config_stack ⇒ Object (readonly)
mainly for testing
14 15 16 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 14 def config_stack @config_stack end |
Instance Method Details
#app_names ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 128 def app_names case self[:app_name] when Array then self[:app_name] when String then self[:app_name].split(';') else [] end end |
#apply_config(source, level = 0) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 22 def apply_config(source, level=0) was_finished = finished_configuring? invoke_callbacks(:add, source) @config_stack.insert(level, source.freeze) reset_cache log_config(:add, source) notify_finished_configuring if !was_finished && finished_configuring? end |
#apply_mask(hash) ⇒ Object
117 118 119 120 121 122 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 117 def apply_mask(hash) MASK_DEFAULTS. \ select {|_, proc| proc.call}. \ each {|key, _| hash.delete(key) } hash end |
#fetch(key) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 58 def fetch(key) @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]) else return config[accessor] end end end nil end |
#finished_configuring? ⇒ Boolean
97 98 99 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 97 def finished_configuring? @config_stack.any? {|s| s.is_a?(ServerSource)} end |
#flattened ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 101 def flattened @config_stack.reverse.inject({}) do |flat,layer| thawed_layer = layer.dup thawed_layer.each do |k,v| begin thawed_layer[k] = instance_eval(&v) if v.respond_to?(:call) rescue => e ::NewRelic::Agent.logger.debug("#{e.class.name} : #{e.} - when accessing config key #{k}") thawed_layer[k] = nil end thawed_layer.delete(:config) end flat.merge(thawed_layer) end end |
#invoke_callbacks(direction, source) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 78 def invoke_callbacks(direction, source) return unless source source.keys.each do |key| if @cache[key] != source[key] @callbacks[key].each do |proc| if direction == :add proc.call(source[key]) else proc.call(@cache[key]) end end end end end |
#log_config(direction, source) ⇒ Object
140 141 142 143 144 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 140 def log_config(direction, source) ::NewRelic::Agent.logger.debug( "Updating config (#{direction}) from #{source.class}. Results:", flattened.inspect) end |
#notify_finished_configuring ⇒ Object
93 94 95 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 93 def notify_finished_configuring NewRelic::Agent.instance.events.notify(:finished_configuring) end |
#register_callback(key, &proc) ⇒ Object
73 74 75 76 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 73 def register_callback(key, &proc) @callbacks[key] << proc proc.call(@cache[key]) end |
#remove_config(source = nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 33 def remove_config(source=nil) if block_given? @config_stack.delete_if {|c| yield c } else @config_stack.delete(source) end reset_cache invoke_callbacks(:remove, source) log_config(:remove, source) end |
#replace_or_add_config(source, level = 0) ⇒ Object
44 45 46 47 48 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 44 def replace_or_add_config(source, level=0) idx = @config_stack.map{|s| s.class}.index(source.class) @config_stack.delete_at(idx) if idx apply_config(source, idx || level) end |
#reset_cache ⇒ Object
136 137 138 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 136 def reset_cache @cache = Hash.new {|hash,key| hash[key] = self.fetch(key) } end |
#source(key) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 50 def source(key) @config_stack.each do |config| if config.respond_to?(key.to_sym) || config.has_key?(key.to_sym) return config end end end |
#to_collector_hash ⇒ Object
124 125 126 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 124 def to_collector_hash DottedHash.new(apply_mask(flattened)).to_hash end |