Class: NewRelic::Agent::Configuration::Manager
- Inherits:
-
Object
- Object
- NewRelic::Agent::Configuration::Manager
- Defined in:
- lib/new_relic/agent/configuration/manager.rb
Constant Summary collapse
- DEPENDENCY_DETECTION_VALUES =
%i[prepend chain unsatisfied].freeze
- MALFORMED_LABELS_WARNING =
'Skipping malformed labels configuration'
- PARSING_LABELS_FAILURE =
'Failure during parsing labels. Ignoring and carrying on with connect.'
- MAX_LABEL_COUNT =
64
- MAX_LABEL_LENGTH =
255
Instance Method Summary collapse
-
#[](key) ⇒ Object
Defining these explicitly saves object allocations that we incur if we use Forwardable and def_delegators.
- #add_config_for_testing(source, level = 0) ⇒ Object
- #apply_mask(hash) ⇒ Object
- #apply_transformations(key, value) ⇒ Object
- #break_label_string_into_pairs(labels) ⇒ Object
- #config_classes_for_testing ⇒ Object
- #delete_all_configs_for_testing ⇒ Object
- #evaluate_and_apply_transformations(key, value) ⇒ Object
- #evaluate_procs(value) ⇒ Object
- #fetch(key) ⇒ Object
- #finished_configuring? ⇒ Boolean
- #flattened ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize ⇒ Manager
constructor
A new instance of Manager.
- #invoke_callbacks(direction, source) ⇒ Object
- #keys ⇒ Object
- #limit_number_of_labels(pairs) ⇒ Object
- #log_config(direction, source) ⇒ Object
- #make_label_hash(pairs, labels = nil) ⇒ Object
- #new_cache ⇒ Object
-
#notify_finished_configuring ⇒ Object
This event is intended to be fired once during the entire lifespan of an agent run, after the server source has been applied for the first time.
-
#notify_server_source_added ⇒ Object
This event is intended to be fired every time the server source is applied.
- #num_configs_for_testing ⇒ Object
- #parse_labels_from_dictionary ⇒ Object
- #parse_labels_from_string ⇒ Object
- #parsed_labels ⇒ Object
- #register_callback(key) {|| ... } ⇒ Object
- #remove_config(source) ⇒ Object
- #remove_config_type(sym) ⇒ Object
-
#remove_duplicates(pairs) ⇒ Object
We only take the last value provided for a given label type key.
- #replace_or_add_config(source) ⇒ Object
-
#reset_cache ⇒ Object
reset the configuration hash, but do not replace previously auto determined dependency detection values with nil or ‘auto’.
-
#reset_to_defaults ⇒ Object
Generally only useful during initial construction and tests.
- #source(key) ⇒ Object
- #to_collector_hash ⇒ Object
- #transform_from_default(key) ⇒ Object
- #truncate(text, key = nil) ⇒ Object
- #valid_label_item?(item) ⇒ Boolean
- #valid_label_pairs?(label_pairs) ⇒ Boolean
Constructor Details
#initialize ⇒ Manager
Returns a new instance of Manager.
34 35 36 37 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 34 def initialize reset_to_defaults @callbacks = Hash.new { |hash, key| hash[key] = [] } end |
Instance Method Details
#[](key) ⇒ Object
Defining these explicitly saves object allocations that we incur if we use Forwardable and def_delegators.
22 23 24 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 22 def [](key) @cache[key] end |
#add_config_for_testing(source, level = 0) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 39 def add_config_for_testing(source, level = 0) raise 'Invalid config type for testing' unless [Hash, DottedHash].include?(source.class) invoke_callbacks(:add, source) @configs_for_testing << [source.freeze, level] reset_cache log_config(:add, source) end |
#apply_mask(hash) ⇒ Object
225 226 227 228 229 230 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 225 def apply_mask(hash) MASK_DEFAULTS \ .select { |_, proc| proc.call } \ .each { |key, _| hash.delete(key) } hash end |
#apply_transformations(key, value) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 143 def apply_transformations(key, value) if transform = transform_from_default(key) begin transform.call(value) rescue => e ::NewRelic::Agent.logger.error("Error applying transformation for #{key}, pre-transform value was: #{value}.", e) raise e end else value end end |
#break_label_string_into_pairs(labels) ⇒ Object
270 271 272 273 274 275 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 270 def break_label_string_into_pairs(labels) stripped_labels = labels.strip.sub(/^;*/, '').sub(/;*$/, '') stripped_labels.split(';').map do |pair| pair.split(':').map(&:strip) end end |
#config_classes_for_testing ⇒ Object
403 404 405 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 403 def config_classes_for_testing config_stack.map(&:class) end |
#delete_all_configs_for_testing ⇒ Object
388 389 390 391 392 393 394 395 396 397 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 388 def delete_all_configs_for_testing @security_policy_source = nil @high_security_source = nil @environment_source = nil @server_source = nil @manual_source = nil @yaml_source = nil @default_source = nil @configs_for_testing = [] end |
#evaluate_and_apply_transformations(key, value) ⇒ Object
139 140 141 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 139 def evaluate_and_apply_transformations(key, value) apply_transformations(key, evaluate_procs(value)) end |
#evaluate_procs(value) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 131 def evaluate_procs(value) if value.respond_to?(:call) instance_eval(&value) else value end end |
#fetch(key) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 113 def fetch(key) config_stack.each do |config| next unless config accessor = key.to_sym if config.has_key?(accessor) begin return evaluate_and_apply_transformations(accessor, config[accessor]) rescue next end end end nil end |
#finished_configuring? ⇒ Boolean
205 206 207 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 205 def finished_configuring? !@server_source.nil? end |
#flattened ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 209 def flattened 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 ::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.to_hash) end end |
#has_key?(key) ⇒ Boolean
26 27 28 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 26 def has_key?(key) @cache.has_key?(key) end |
#invoke_callbacks(direction, source) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 165 def invoke_callbacks(direction, source) return unless source source.keys.each do |key| begin # we need to evaluate and apply transformations for the value to deal with procs as values # this is usually done by the fetch method when accessing config, however the callbacks bypass that evaluated_cache = evaluate_and_apply_transformations(key, @cache[key]) evaluated_source = evaluate_and_apply_transformations(key, source[key]) rescue next end if evaluated_cache != evaluated_source @callbacks[key].each do |proc| if direction == :add proc.call(evaluated_source) else proc.call(evaluated_cache) end end end end end |
#keys ⇒ Object
30 31 32 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 30 def keys @cache.keys end |
#limit_number_of_labels(pairs) ⇒ Object
328 329 330 331 332 333 334 335 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 328 def limit_number_of_labels(pairs) if pairs.length > MAX_LABEL_COUNT NewRelic::Agent.logger.warn("Too many labels defined. Only taking first #{MAX_LABEL_COUNT}") pairs[0...64] else pairs end end |
#log_config(direction, source) ⇒ Object
378 379 380 381 382 383 384 385 386 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 378 def log_config(direction, source) # Just generating this log message (specifically calling # flattened.inspect) is expensive enough that we don't want to do it # unless we're actually going to be logging the message based on our # current log level. ::NewRelic::Agent.logger.debug do "Updating config (#{direction}) from #{source.class}. Results: #{flattened.inspect}" end end |
#make_label_hash(pairs, labels = nil) ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 295 def make_label_hash(pairs, labels = nil) # This can accept a hash, so force it down to an array of pairs first pairs = Array(pairs) unless valid_label_pairs?(pairs) NewRelic::Agent.logger.warn("#{MALFORMED_LABELS_WARNING}: #{labels || pairs}") return NewRelic::EMPTY_ARRAY end pairs = limit_number_of_labels(pairs) pairs = remove_duplicates(pairs) pairs.map do |key, value| { 'label_type' => truncate(key), 'label_value' => truncate(value.to_s, key) } end end |
#new_cache ⇒ Object
374 375 376 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 374 def new_cache @cache = Hash.new { |hash, key| hash[key] = self.fetch(key) } end |
#notify_finished_configuring ⇒ Object
This event is intended to be fired once during the entire lifespan of an agent run, after the server source has been applied for the first time. This should indicate that all configuration has been applied, and the main functions of the agent are safe to start.
201 202 203 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 201 def notify_finished_configuring NewRelic::Agent.instance.events.notify(:initial_configuration_complete) end |
#notify_server_source_added ⇒ Object
This event is intended to be fired every time the server source is applied. This happens after the agent’s initial connect, and again on every forced reconnect.
193 194 195 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 193 def notify_server_source_added NewRelic::Agent.instance.events.notify(:server_source_configuration_added) end |
#num_configs_for_testing ⇒ Object
399 400 401 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 399 def num_configs_for_testing config_stack.size end |
#parse_labels_from_dictionary ⇒ Object
343 344 345 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 343 def parse_labels_from_dictionary make_label_hash(NewRelic::Agent.config[:labels]) end |
#parse_labels_from_string ⇒ Object
264 265 266 267 268 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 264 def parse_labels_from_string labels = NewRelic::Agent.config[:labels] label_pairs = break_label_string_into_pairs(labels) make_label_hash(label_pairs, labels) end |
#parsed_labels ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 252 def parsed_labels case NewRelic::Agent.config[:labels] when String parse_labels_from_string else parse_labels_from_dictionary end rescue => e NewRelic::Agent.logger.error(PARSING_LABELS_FAILURE, e) NewRelic::EMPTY_ARRAY end |
#register_callback(key) {|| ... } ⇒ Object
160 161 162 163 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 160 def register_callback(key, &proc) @callbacks[key] << proc yield(@cache[key]) end |
#remove_config(source) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 62 def remove_config(source) case source when SecurityPolicySource then @security_policy_source = nil when HighSecuritySource then @high_security_source = nil 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 |
#remove_config_type(sym) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 48 def remove_config_type(sym) source = case sym when :security_policy then @security_policy_source when :high_security then @high_security_source 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 |
#remove_duplicates(pairs) ⇒ Object
We only take the last value provided for a given label type key
338 339 340 341 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 338 def remove_duplicates(pairs) grouped_by_type = pairs.group_by(&:first) grouped_by_type.values.map(&:last) end |
#replace_or_add_config(source) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 80 def replace_or_add_config(source) source.freeze was_finished = finished_configuring? invoke_callbacks(:add, source) case source when SecurityPolicySource then @security_policy_source = source when HighSecuritySource then @high_security_source = 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_server_source_added if ServerSource === source notify_finished_configuring if !was_finished && finished_configuring? end |
#reset_cache ⇒ Object
reset the configuration hash, but do not replace previously auto determined dependency detection values with nil or ‘auto’
364 365 366 367 368 369 370 371 372 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 364 def reset_cache return new_cache unless defined?(@cache) && @cache preserved = @cache.select { |_k, v| DEPENDENCY_DETECTION_VALUES.include?(v) } new_cache preserved.each { |k, v| @cache[k] = v } @cache end |
#reset_to_defaults ⇒ Object
Generally only useful during initial construction and tests
348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 348 def reset_to_defaults @security_policy_source = nil @high_security_source = nil @environment_source = EnvironmentSource.new @server_source = nil @manual_source = nil @yaml_source = nil @default_source = DefaultSource.new @configs_for_testing = [] reset_cache end |
#source(key) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 105 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
232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 232 def to_collector_hash DottedHash.new(apply_mask(flattened)).to_hash.delete_if do |k, v| default = DEFAULTS[k] if default default[:exclude_from_reported_settings] else # In our tests, we add totally bogus configs, because testing. # In those cases, there will be no default. So we'll just let # them through. false end end end |
#transform_from_default(key) ⇒ Object
156 157 158 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 156 def transform_from_default(key) ::NewRelic::Agent::Configuration::DefaultSource.transform_for(key) end |
#truncate(text, key = nil) ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 314 def truncate(text, key = nil) if text.length > MAX_LABEL_LENGTH if key msg = "The value for the label '#{key}' is longer than the allowed #{MAX_LABEL_LENGTH} and will be truncated. Value = '#{text}'" else msg = "Label name longer than the allowed #{MAX_LABEL_LENGTH} will be truncated. Name = '#{text}'" end NewRelic::Agent.logger.warn(msg) text[0..MAX_LABEL_LENGTH - 1] else text end end |
#valid_label_item?(item) ⇒ Boolean
285 286 287 288 289 290 291 292 293 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 285 def valid_label_item?(item) case item when String then !item.empty? when Numeric then true when true then true when false then true else false end end |
#valid_label_pairs?(label_pairs) ⇒ Boolean
277 278 279 280 281 282 283 |
# File 'lib/new_relic/agent/configuration/manager.rb', line 277 def valid_label_pairs?(label_pairs) label_pairs.all? do |pair| pair.length == 2 && valid_label_item?(pair.first) && valid_label_item?(pair.last) end end |