Class: TingYun::Configuration::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/ting_yun/configuration/manager.rb

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



14
15
16
# File 'lib/ting_yun/configuration/manager.rb', line 14

def initialize
  reset_to_defaults
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  @cache[key]
end

#add_config_for_testing(source, level = 0) ⇒ Object



91
92
93
94
95
96
# File 'lib/ting_yun/configuration/manager.rb', line 91

def add_config_for_testing(source, level=0)
  raise 'Invalid config type for testing' unless [Hash, DottedHash].include?(source.class)
  @configs_for_testing << [source.freeze, level]
  reset_cache
  log_config(:add, source)
end

#app_namesObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ting_yun/configuration/manager.rb', line 30

def app_names
  if TingYun::Agent.config[:'nbs.auto_app_naming']
    begin
    [::TingYun::Frameworks.framework.root.split('/').last]
    rescue Exception => e
      get_name
    end
  else
    get_name
  end

end

#config_classes_for_testingObject



193
194
195
# File 'lib/ting_yun/configuration/manager.rb', line 193

def config_classes_for_testing
  config_stack.map(&:class)
end

#evaluate_procs(value) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/ting_yun/configuration/manager.rb', line 83

def evaluate_procs(value)
  if value.respond_to?(:call)
    instance_eval(&value)
  else
    value
  end
end

#fetch(key) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ting_yun/configuration/manager.rb', line 71

def fetch(key)
  config_stack.each do |config|
    next unless config
    accessor = key.to_sym

    if config.has_key?(accessor)
      return evaluated = evaluate_procs(config[accessor]) #if it's proc
    end
  end
  nil
end

#finished_configuring?Boolean

Returns:



154
155
156
# File 'lib/ting_yun/configuration/manager.rb', line 154

def finished_configuring?
  !@server_source.nil?
end

#flattenedObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/ting_yun/configuration/manager.rb', line 177

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
        ::TingYun::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

#get_nameObject



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

def get_name
  case TingYun::Agent.config[:app_name]
    when Array then
      TingYun::Agent.config[:app_name]
    when String then
      TingYun::Agent.config[:app_name].split(';')
    else
      []
  end
end

#has_key?(key) ⇒ Boolean

Returns:



22
23
24
# File 'lib/ting_yun/configuration/manager.rb', line 22

def has_key?(key)
  @cache.has_key?(key)
end

#keysObject



26
27
28
# File 'lib/ting_yun/configuration/manager.rb', line 26

def keys
  @cache.keys
end

#log_config(direction, source) ⇒ Object



167
168
169
170
171
172
173
174
175
# File 'lib/ting_yun/configuration/manager.rb', line 167

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.
  ::TingYun::Agent.logger.debug do
    "Updating config (#{direction}) from #{source.class}. Results: #{flattened.inspect}"
  end
end

#notify_finished_configuringObject



150
151
152
# File 'lib/ting_yun/configuration/manager.rb', line 150

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

#remove_config(source) ⇒ Object



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

def remove_config(source)
  case source
    when YamlSource         then  @yaml_source          = nil
    when DefaultSource      then  @default_source       = nil
    when EnvironmentSource  then  @environment_source   = nil
    when ManualSource       then  @manual_source        = nil
    when ServerSource       then  @server_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



98
99
100
101
102
103
104
105
106
107
# File 'lib/ting_yun/configuration/manager.rb', line 98

def remove_config_type(sym)
  source = case sym
             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

#replace_or_add_config(source) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ting_yun/configuration/manager.rb', line 129

def replace_or_add_config(source)
  source.freeze

  was_finished = finished_configuring?

  case source
    when YamlSource        then   @yaml_source          = source
    when DefaultSource     then   @default_source       = source
    when EnvironmentSource then   @environment_source   = source
    when ServerSource      then   @server_source        = source
    when ManualSource      then   @manual_source        = source
    else
      TingYun::Agent.logger.warn("Invalid config format; config will be ignored: #{source}")
  end
  reset_cache

  log_config(:add, source)

  notify_finished_configuring if !was_finished && finished_configuring?
end

#reset_cacheObject



67
68
69
# File 'lib/ting_yun/configuration/manager.rb', line 67

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

#reset_to_defaultsObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ting_yun/configuration/manager.rb', line 54

def reset_to_defaults
  @default_source = DefaultSource.new
  @environment_source = EnvironmentSource.new
  @yaml_source = nil
  @server_source  = nil
  @manual_source = nil
  # @callbacks = Hash.new {|hash,key| hash[key] =[]}#存放需要merge本地和服务端配置的info'

  @configs_for_testing = []

  reset_cache
end

#source(key) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/ting_yun/configuration/manager.rb', line 159

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



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/ting_yun/configuration/manager.rb', line 200

def to_collector_hash
  DottedHash.new(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