class OneApm::Configuration::YamlSource

Attributes

file_path[RW]

Public Class Methods

new(path, env) click to toggle source
Calls superclass method OneApm::Support::DottedHash::new
# File lib/one_apm/configuration/yaml_source.rb, line 13
def initialize(path, env)
  config = {}

  begin
    @file_path = validate_config_file_path(path)
    return unless @file_path

    OneApm::Manager.logger.info("Reading configuration from #{path} (#{Dir.pwd})")
    raw_file = if RUBY_VERSION >= '1.9.3'
      File.read(@file_path, :encoding => 'utf-8') 
    else
      File.read(@file_path)
    end
    erb_file = process_erb(raw_file)
    config   = process_yaml(erb_file, env, config, @file_path)
    add_notified!(config)
  rescue ScriptError, StandardError => e
    OneApm::Manager.logger.error("Failed to read or parse configuration file at #{path}", e)
  end

  substitute_transaction_threshold(config)
  booleanify_values(config, 'agent_enabled', 'enabled', 'monitor_daemons')

  super(config, true)
end

Protected Instance Methods

add_notified!(config) click to toggle source

add notified to key of config before: {'a' => 1, 'b' => {'c' => 2}}

after: {'a' => 1, 'a.notified' => true, 'b' => {'c' => 1, 'c.notified' => true}}

# File lib/one_apm/configuration/yaml_source.rb, line 108
def add_notified! config
  config.keys.each do |key|
    if config[key].respond_to?(:has_key?)
      config[key].keys.each do |inner_key|
        config[key]["#{inner_key}.notified"] = true unless config[key].has_key?("#{inner_key}.notified")
      end
      next
    end
    config["#{key}.notified"] = true unless config.has_key?("#{key}.notified")
  end
  config
end
booleanify_values(config, *keys) click to toggle source
# File lib/one_apm/configuration/yaml_source.rb, line 139
def booleanify_values(config, *keys)
  # auto means defer ro default
  keys.each do |option|
    if config[option] == 'auto'
      config.delete(option)
    elsif !config[option].nil? && !is_boolean?(config[option])
      config[option] = !!(config[option] =~ /yes|on|true/i)
    end
  end
end
is_boolean?(value) click to toggle source
# File lib/one_apm/configuration/yaml_source.rb, line 150
def is_boolean?(value)
  value == !!value
end
process_erb(file) click to toggle source
# File lib/one_apm/configuration/yaml_source.rb, line 75
def process_erb(file)
  begin
    # Exclude lines that are commented out so failing Ruby code in an
    # ERB template commented at the YML level is fine. Leave the line,
    # though, so ERB line numbers remain correct.
    file.gsub!(/^\s*#.*$/, '#')

    # Next two are for populating the oneapm.yml via erb binding, necessary
    # when using the default oneapm.yml file
    generated_for_user = ''
    license_key = ''

    ERB.new(file).result(binding)
  rescue ScriptError, StandardError => e
    OneApm::Manager.logger.error("Failed ERB processing configuration file. This is typically caused by a Ruby error in <% %> templating blocks in your oneapm.yml file.", e)
    nil
  end
end
process_yaml(file, env, config, path) click to toggle source
# File lib/one_apm/configuration/yaml_source.rb, line 94
def process_yaml(file, env, config, path)
  if file
    confighash = with_yaml_engine { YAML.load(file) }
    OneApm::Manager.logger.error("Config file at #{path} doesn't include a '#{env}' section!") unless confighash.key?(env)

    config = confighash[env] || {}
  end

  config
end
substitute_transaction_threshold(config) click to toggle source
# File lib/one_apm/configuration/yaml_source.rb, line 121
def substitute_transaction_threshold(config)
  if config['transaction_tracer'] &&
      config['transaction_tracer']['transaction_threshold'] =~ /apdex_f/i
    # when value is "apdex_f" remove the config and defer to default
    config['transaction_tracer'].delete('transaction_threshold')
  end
end
validate_config_file_path(path) click to toggle source
# File lib/one_apm/configuration/yaml_source.rb, line 41
def validate_config_file_path(path)
  expanded_path = File.expand_path(path)

  if path.empty? || !File.exists?(expanded_path)
    warn_missing_config_file(expanded_path)
    return
  end

  expanded_path
end
warn_missing_config_file(path) click to toggle source
# File lib/one_apm/configuration/yaml_source.rb, line 52
def warn_missing_config_file(path)
  based_on        = 'unknown'
  source          = OneApm::Manager.config.source(:config_path)
  candidate_paths = [path]

  case source
  when DefaultSource
    based_on = 'defaults'
    candidate_paths = OneApm::Manager.config[:config_search_paths].map do |p|
      File.expand_path(p)
    end
  when EnvironmentSource
    based_on = 'environment variable'
  when ManualSource
    based_on = 'API call'
  end

  OneApm::Manager.logger.warn(
    "No configuration file found. Working directory = #{Dir.pwd}",
    "Looked in these locations (based on #{based_on}): #{candidate_paths.join(", ")}"
  )
end
with_yaml_engine() { || ... } click to toggle source
# File lib/one_apm/configuration/yaml_source.rb, line 129
def with_yaml_engine
  return yield unless OneApm::LanguageSupport.needs_syck?

  yamler = ::YAML::ENGINE.yamler
  ::YAML::ENGINE.yamler = 'syck'
  result = yield
  ::YAML::ENGINE.yamler = yamler
  result
end