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
# File 'lib/new_relic/agent/configuration/manager.rb', line 20

def initialize
  reset_to_defaults
  @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



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

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



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

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



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

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

#fetch(key) ⇒ Object



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

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:



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

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

#flattenedObject



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

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.message} - when accessing config key #{k}")
        thawed_layer[k] = nil
      end
      thawed_layer.delete(:config)
    end
    flat.merge(thawed_layer.to_hash)
  end
end

#invoke_callbacks(direction, source) ⇒ Object



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

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



157
158
159
160
161
# File 'lib/new_relic/agent/configuration/manager.rb', line 157

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

#notify_finished_configuringObject



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

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

#register_callback(key, &proc) ⇒ Object



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

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

#remove_config(source = nil) ⇒ Object



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

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



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

def replace_or_add_config(source, level=0)
  index = @config_stack.map{|s| s.class}.index(source.class)
  @config_stack.delete_at(index) if index
  apply_config(source, index || level)
end

#reset_cacheObject



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

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

#reset_to_defaultsObject

Generally only useful during initial construction and tests



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

def reset_to_defaults
  @config_stack = [ EnvironmentSource.new, DefaultSource.new ]
  reset_cache
end

#source(key) ⇒ Object



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

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



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

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