Class: NewRelic::Agent::Configuration::Manager

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/new_relic/agent/configuration/manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



20
21
22
23
24
# File 'lib/new_relic/agent/configuration/manager.rb', line 20

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_stackObject (readonly)

mainly for testing



18
19
20
# File 'lib/new_relic/agent/configuration/manager.rb', line 18

def config_stack
  @config_stack
end

Instance Method Details

#app_namesObject



132
133
134
135
136
137
138
# File 'lib/new_relic/agent/configuration/manager.rb', line 132

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



26
27
28
29
30
31
32
33
34
35
# File 'lib/new_relic/agent/configuration/manager.rb', line 26

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



121
122
123
124
125
126
# File 'lib/new_relic/agent/configuration/manager.rb', line 121

def apply_mask(hash)
  MASK_DEFAULTS. \
    select {|_, proc| proc.call}. \
    each {|key, _| hash.delete(key) }
  hash
end

#fetch(key) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/new_relic/agent/configuration/manager.rb', line 62

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

Returns:

  • (Boolean)


101
102
103
# File 'lib/new_relic/agent/configuration/manager.rb', line 101

def finished_configuring?
  @config_stack.any? {|s| s.is_a?(ServerSource)}
end

#flattenedObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/new_relic/agent/configuration/manager.rb', line 105

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.message} - 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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/new_relic/agent/configuration/manager.rb', line 82

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



144
145
146
147
148
# File 'lib/new_relic/agent/configuration/manager.rb', line 144

def log_config(direction, source)
  ::NewRelic::Agent.logger.debug(
    "Updating config (#{direction}) from #{source.class}. Results:",
    flattened.inspect)
end

#notify_finished_configuringObject



97
98
99
# File 'lib/new_relic/agent/configuration/manager.rb', line 97

def notify_finished_configuring
  NewRelic::Agent.instance.events.notify(:finished_configuring)
end

#register_callback(key, &proc) ⇒ Object



77
78
79
80
# File 'lib/new_relic/agent/configuration/manager.rb', line 77

def register_callback(key, &proc)
  @callbacks[key] << proc
  proc.call(@cache[key])
end

#remove_config(source = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/new_relic/agent/configuration/manager.rb', line 37

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



48
49
50
51
52
# File 'lib/new_relic/agent/configuration/manager.rb', line 48

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_cacheObject



140
141
142
# File 'lib/new_relic/agent/configuration/manager.rb', line 140

def reset_cache
  @cache = Hash.new {|hash,key| hash[key] = self.fetch(key) }
end

#source(key) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/new_relic/agent/configuration/manager.rb', line 54

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_hashObject



128
129
130
# File 'lib/new_relic/agent/configuration/manager.rb', line 128

def to_collector_hash
  DottedHash.new(apply_mask(flattened)).to_hash
end