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.



15
16
17
18
19
# File 'lib/new_relic/agent/configuration/manager.rb', line 15

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



13
14
15
# File 'lib/new_relic/agent/configuration/manager.rb', line 13

def config_stack
  @config_stack
end

Instance Method Details

#app_namesObject



102
103
104
105
106
107
108
# File 'lib/new_relic/agent/configuration/manager.rb', line 102

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



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

def apply_config(source, level=0)
  invoke_callbacks(:add, source)
  @config_stack.insert(level, source.freeze)
  reset_cache
end

#fetch(key) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/new_relic/agent/configuration/manager.rb', line 51

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

#flattened_configObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/new_relic/agent/configuration/manager.rb', line 86

def flattened_config
  @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::Control.instance.log.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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/new_relic/agent/configuration/manager.rb', line 71

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

#register_callback(key, &proc) ⇒ Object



66
67
68
69
# File 'lib/new_relic/agent/configuration/manager.rb', line 66

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

#remove_config(source = nil) ⇒ Object



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

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)
end

#replace_or_add_config(source, level = 0) ⇒ Object



37
38
39
40
41
# File 'lib/new_relic/agent/configuration/manager.rb', line 37

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



110
111
112
# File 'lib/new_relic/agent/configuration/manager.rb', line 110

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

#source(key) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/new_relic/agent/configuration/manager.rb', line 43

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