Class: NewRelic::Agent::Configuration::EnvironmentSource

Inherits:
DottedHash
  • Object
show all
Defined in:
lib/new_relic/agent/configuration/environment_source.rb

Constant Summary collapse

SUPPORTED_PREFIXES =
/^new_relic_|^newrelic_/i
SPECIAL_CASE_KEYS =
[
  'NEW_RELIC_ENV', # read by NewRelic::Control::Frameworks::Ruby
  'NEW_RELIC_LOG', # read by set_log_file
  /^NEW_RELIC_METADATA_/ # read by NewRelic::Agent::Connect::RequestBuilder
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DottedHash

#inspect, symbolize, #to_hash

Constructor Details

#initializeEnvironmentSource

Returns a new instance of EnvironmentSource.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/new_relic/agent/configuration/environment_source.rb', line 18

def initialize
  set_log_file
  set_config_file

  @alias_map = {}

  DEFAULTS.each do |config_setting, value|
    set_aliases(config_setting, value)
  end

  set_values_from_new_relic_environment_variables
end

Instance Attribute Details

#alias_mapObject



16
17
18
# File 'lib/new_relic/agent/configuration/environment_source.rb', line 16

def alias_map
  @alias_map
end

Instance Method Details

#collect_new_relic_environment_variable_keysObject



87
88
89
# File 'lib/new_relic/agent/configuration/environment_source.rb', line 87

def collect_new_relic_environment_variable_keys
  ENV.keys.select { |key| key.match(SUPPORTED_PREFIXES) }
end

#convert_environment_key_to_config_key(key) ⇒ Object



82
83
84
85
# File 'lib/new_relic/agent/configuration/environment_source.rb', line 82

def convert_environment_key_to_config_key(key)
  stripped_key = key.gsub(SUPPORTED_PREFIXES, '').downcase.to_sym
  self.alias_map[stripped_key] || stripped_key
end

#serverless?Boolean

we can’t rely on the :‘serverless_mode.enabled’ config parameter being set yet to signify serverless mode given that we’re in the midst of building the config but we can always rely on the env var being set by the Lambda layer

Returns:



95
96
97
# File 'lib/new_relic/agent/configuration/environment_source.rb', line 95

def serverless?
  NewRelic::Agent::ServerlessHandler.env_var_set?
end

#set_aliases(config_setting, value) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/new_relic/agent/configuration/environment_source.rb', line 31

def set_aliases(config_setting, value)
  set_dotted_alias(config_setting)

  return unless value[:aliases]

  value[:aliases].each do |config_alias|
    self.alias_map[config_alias] = config_setting
  end
end

#set_config_fileObject



61
62
63
# File 'lib/new_relic/agent/configuration/environment_source.rb', line 61

def set_config_file
  self[:config_path] = ENV['NRCONFIG'] if ENV['NRCONFIG']
end

#set_dotted_alias(original_config_setting) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/new_relic/agent/configuration/environment_source.rb', line 41

def set_dotted_alias(original_config_setting)
  config_setting = original_config_setting.to_s

  if config_setting.include?('.')
    config_alias = config_setting.tr('.', '_').to_sym
    self.alias_map[config_alias] = original_config_setting
  end
end

#set_log_fileObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/new_relic/agent/configuration/environment_source.rb', line 50

def set_log_file
  if ENV['NEW_RELIC_LOG']
    if ENV['NEW_RELIC_LOG'].casecmp(NewRelic::STANDARD_OUT) == 0
      self[:log_file_path] = self[:log_file_name] = NewRelic::STANDARD_OUT
    else
      self[:log_file_path] = File.dirname(ENV['NEW_RELIC_LOG'])
      self[:log_file_name] = File.basename(ENV['NEW_RELIC_LOG'])
    end
  end
end

#set_values_from_new_relic_environment_variablesObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/new_relic/agent/configuration/environment_source.rb', line 65

def set_values_from_new_relic_environment_variables
  nr_env_var_keys = collect_new_relic_environment_variable_keys

  nr_env_var_keys.each do |key|
    next if SPECIAL_CASE_KEYS.any? { |pattern| pattern === key.upcase }

    config_key = convert_environment_key_to_config_key(key)

    unless DEFAULTS.key?(config_key) || serverless?
      ::NewRelic::Agent.logger.info("#{key} does not have a corresponding configuration setting (#{config_key} does not exist).")
      ::NewRelic::Agent.logger.info('Run `rake newrelic:config:docs` or visit https://docs.newrelic.com/docs/apm/agents/ruby-agent/configuration/ruby-agent-configuration to see a list of available configuration settings.')
    end

    self[config_key] = ENV[key]
  end
end