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',
    'external_service_metric_limit',
    'external_service_metric_report_limit',
    'host',
    'hostname',
    'ignore',
    'key',
    'log_class',
    'log_file_path',
    'log_level',
    'log_stderr',
    'log_stdout',
    'max_traces',
    'monitor',
    'name',
    'profile',
    'proxy',
    'record_queue_time',
    'remote_agent_host',
    'remote_agent_port',
    'report_format',
    'revision_sha',
    'scm_subdirectory',
    'start_resque_server_instrument',
    'ssl_cert_file',
    'uri_reporting',
    'instrument_http_url_length',
    'timeline_traces',
    'auto_instruments',
    'auto_instruments_ignore',

    # Error Service Related Configuration
    'errors_enabled',
    'errors_ignored_exceptions',
    'errors_filtered_params',
    'errors_host',
]
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,
  'external_service_metric_limit'  => IntegerCoercion.new,
  'external_service_metric_report_limit' => IntegerCoercion.new,
  'instrument_http_url_length' => IntegerCoercion.new,
  'record_queue_time' => BooleanCoercion.new,
  'start_resque_server_instrument' => BooleanCoercion.new,
  'timeline_traces' => BooleanCoercion.new,
  'auto_instruments' => BooleanCoercion.new,
  'auto_instruments_ignore' => JsonCoercion.new,
  'errors_enabled' => BooleanCoercion.new,
  'errors_ignored_exceptions' => JsonCoercion.new,
  'errors_filtered_params' => JsonCoercion.new,
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, overlays) ⇒ Config

Returns a new instance of Config.



226
227
228
229
# File 'lib/scout_apm/config.rb', line 226

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



216
217
218
219
220
221
222
223
224
# File 'lib/scout_apm/config.rb', line 216

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.



204
205
206
207
208
209
210
211
# File 'lib/scout_apm/config.rb', line 204

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”



261
262
263
264
265
266
# File 'lib/scout_apm/config.rb', line 261

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)


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

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

#log_settings(logger) ⇒ Object



268
269
270
271
272
273
# File 'lib/scout_apm/config.rb', line 268

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

#loggerObject



275
276
277
# File 'lib/scout_apm/config.rb', line 275

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?



232
233
234
# File 'lib/scout_apm/config.rb', line 232

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

#value(key) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/scout_apm/config.rb', line 236

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