Class: ScoutApm::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/config.rb

Defined Under Namespace

Classes: BooleanCoercion, ConfigDefaults, ConfigEnvironment, ConfigFile, ConfigNull, IntegerCoercion, JsonCoercion, NullCoercion

Constant Summary collapse

KNOWN_CONFIG_OPTIONS =
[
    'application_root',
    'async_recording',
    'collect_remote_ip',
    'compress_payload',
    'config_file',
    'data_file',
    'database_metric_limit',
    'database_metric_report_limit',
    'detailed_middleware',
    'dev_trace',
    'direct_host',
    'disabled_instruments',
    'enable_background_jobs',
    'host',
    'hostname',
    'ignore',
    'key',
    'log_class',
    'log_file_path',
    'log_level',
    'log_stderr',
    'log_stdout',
    'max_traces',
    'monitor',
    'name',
    'profile',
    'proxy',
    'remote_agent_host',
    'remote_agent_port',
    'report_format',
    'revision_sha',
    'scm_subdirectory',
    'start_resque_server_instrument',
    'uri_reporting',
    'instrument_http_url_length',
    'timeline_traces',
    'auto_instruments',
    'auto_instruments_ignore'
]
SETTING_COERCIONS =
{
  'async_recording' => BooleanCoercion.new,
  'detailed_middleware' => BooleanCoercion.new,
  'dev_trace' => BooleanCoercion.new,
  'enable_background_jobs' => BooleanCoercion.new,
  'ignore' => JsonCoercion.new,
  'max_traces' => IntegerCoercion.new,
  'monitor' => BooleanCoercion.new,
  'collect_remote_ip' => BooleanCoercion.new,
  'compress_payload' => BooleanCoercion.new,
  'database_metric_limit'  => IntegerCoercion.new,
  'database_metric_report_limit' => IntegerCoercion.new,
  'instrument_http_url_length' => IntegerCoercion.new,
  'start_resque_server_instrument' => BooleanCoercion.new,
  'timeline_traces' => BooleanCoercion.new,
  'auto_instruments' => BooleanCoercion.new,
  'auto_instruments_ignore' => JsonCoercion.new,
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, overlays) ⇒ Config

Returns a new instance of Config.



209
210
211
212
# File 'lib/scout_apm/config.rb', line 209

def initialize(context, overlays)
  @context = context
  @overlays = Array(overlays)
end

Class Method Details

.with_file(context, file_path = nil, config = {}) ⇒ Object

Load up a config instance, attempting to load a yaml file. Allows a definite location if requested, or will attempt to load the default configuration file: APP_ROOT/config/scout_apm.yml



199
200
201
202
203
204
205
206
207
# File 'lib/scout_apm/config.rb', line 199

def self.with_file(context, file_path=nil, config={})
  overlays = [
    ConfigEnvironment.new,
    ConfigFile.new(context, file_path, config),
    ConfigDefaults.new,
    ConfigNull.new,
  ]
  new(context, overlays)
end

.without_file(context) ⇒ Object

Load up a config instance without attempting to load a file. Useful for bootstrapping.



187
188
189
190
191
192
193
194
# File 'lib/scout_apm/config.rb', line 187

def self.without_file(context)
  overlays = [
    ConfigEnvironment.new,
    ConfigDefaults.new,
    ConfigNull.new,
  ]
  new(context, overlays)
end

Instance Method Details

#all_settingsObject

Returns an array of config keys, values, and source “monitor”, value: “true”, source: “environment”



244
245
246
247
248
249
# File 'lib/scout_apm/config.rb', line 244

def all_settings
  KNOWN_CONFIG_OPTIONS.inject([]) do |memo, key|
    o = overlay_for_key(key)
    memo << {:key => key, :value => value(key).inspect, :source => o.name}
  end
end

#any_keys_found?Boolean

Did we load anything for configuration?

Returns:

  • (Boolean)


237
238
239
# File 'lib/scout_apm/config.rb', line 237

def any_keys_found?
  @overlays.any? { |overlay| overlay.any_keys_found? }
end

#log_settings(logger) ⇒ Object



251
252
253
254
255
256
# File 'lib/scout_apm/config.rb', line 251

def log_settings(logger)
  logger.debug(
    "Resolved Setting Values:\n" +
    all_settings.map{|hsh| "#{hsh[:source]} - #{hsh[:key]}: #{hsh[:value]}"}.join("\n")
  )
end

#loggerObject



258
259
260
# File 'lib/scout_apm/config.rb', line 258

def logger
  @context.logger
end

#overlay_for_key(key) ⇒ Object

For a given key, what is the first overlay says that it can handle it?



215
216
217
# File 'lib/scout_apm/config.rb', line 215

def overlay_for_key(key)
  @overlays.detect{ |overlay| overlay.has_key?(key) }
end

#value(key) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/scout_apm/config.rb', line 219

def value(key)
  if ! KNOWN_CONFIG_OPTIONS.include?(key)
    logger.debug("Requested looking up a unknown configuration key: #{key} (not a problem. Evaluate and add to config.rb)")
  end

  o = overlay_for_key(key)
  raw_value = if o
                o.value(key)
              else
                # No overlay said it could handle this key, bail out with nil.
                nil
              end

  coercion = SETTING_COERCIONS.fetch(key, NullCoercion.new)
  coercion.coerce(raw_value)
end