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
25
26
27
28
29
30
31
32
# 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] = [] }

  register_callback(:'strip_exception_messages.whitelist') do |whitelist|
    if whitelist
      @stripped_exceptions_whitelist = parse_constant_list(whitelist).compact
    else
      @stripped_exceptions_whitelist = []
    end
  end
end

Instance Attribute Details

#config_stackObject (readonly)

Returns the value of attribute config_stack.



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

def config_stack
  @config_stack
end

#stripped_exceptions_whitelistObject (readonly)

Returns the value of attribute stripped_exceptions_whitelist.



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

def stripped_exceptions_whitelist
  @stripped_exceptions_whitelist
end

Instance Method Details

#app_namesObject



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

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



34
35
36
37
38
39
40
41
42
43
# File 'lib/new_relic/agent/configuration/manager.rb', line 34

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



129
130
131
132
133
134
# File 'lib/new_relic/agent/configuration/manager.rb', line 129

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

#fetch(key) ⇒ Object



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

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)


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

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

#flattenedObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/new_relic/agent/configuration/manager.rb', line 113

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/new_relic/agent/configuration/manager.rb', line 90

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



152
153
154
155
156
# File 'lib/new_relic/agent/configuration/manager.rb', line 152

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

#notify_finished_configuringObject



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

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

#register_callback(key, &proc) ⇒ Object



85
86
87
88
# File 'lib/new_relic/agent/configuration/manager.rb', line 85

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

#remove_config(source = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/new_relic/agent/configuration/manager.rb', line 45

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



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

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



148
149
150
# File 'lib/new_relic/agent/configuration/manager.rb', line 148

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

#source(key) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/new_relic/agent/configuration/manager.rb', line 62

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



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

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