Class: Appsignal::Config

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

Defined Under Namespace

Classes: ConfigDSL

Constant Summary collapse

DEFAULT_CONFIG =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  :activejob_report_errors => "all",
  :ca_file_path => File.expand_path(File.join("../../../resources/cacert.pem"), __FILE__),
  :dns_servers => [],
  :enable_allocation_tracking => true,
  :enable_at_exit_reporter => true,
  :enable_host_metrics => true,
  :enable_minutely_probes => true,
  :enable_statsd => true,
  :enable_nginx_metrics => false,
  :enable_gvl_global_timer => true,
  :enable_gvl_waiting_threads => true,
  :enable_rails_error_reporter => true,
  :enable_rake_performance_instrumentation => false,
  :endpoint => "https://push.appsignal.com",
  :files_world_accessible => true,
  :filter_metadata => [],
  :filter_parameters => [],
  :filter_session_data => [],
  :ignore_actions => [],
  :ignore_errors => [],
  :ignore_logs => [],
  :ignore_namespaces => [],
  :instrument_http_rb => true,
  :instrument_net_http => true,
  :instrument_redis => true,
  :instrument_sequel => true,
  :log => "file",
  :logging_endpoint => "https://appsignal-endpoint.net",
  :request_headers => %w[
    HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING
    HTTP_ACCEPT_LANGUAGE HTTP_CACHE_CONTROL HTTP_CONNECTION
    CONTENT_LENGTH PATH_INFO HTTP_RANGE
    REQUEST_METHOD REQUEST_PATH SERVER_NAME SERVER_PORT
    SERVER_PROTOCOL
  ],
  :send_environment_metadata => true,
  :send_params => true,
  :send_session_data => true,
  :sidekiq_report_errors => "all"
}.freeze
DEFAULT_LOG_LEVEL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

::Logger::INFO
LOG_LEVEL_MAP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Map from the log_level config option to Ruby's Logger level value.

The trace level doesn't exist in the Ruby logger so it's mapped to debug.

{
  "error" => ::Logger::ERROR,
  "warn" => ::Logger::WARN,
  "warning" => ::Logger::WARN,
  "info" => ::Logger::INFO,
  "debug" => ::Logger::DEBUG,
  "trace" => ::Logger::DEBUG
}.freeze
ENV_STRING_KEYS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  "APPSIGNAL_ACTIVEJOB_REPORT_ERRORS" => :activejob_report_errors,
  "APPSIGNAL_APP_NAME" => :name,
  "APPSIGNAL_BIND_ADDRESS" => :bind_address,
  "APPSIGNAL_CA_FILE_PATH" => :ca_file_path,
  "APPSIGNAL_HOSTNAME" => :hostname,
  "APPSIGNAL_HOST_ROLE" => :host_role,
  "APPSIGNAL_HTTP_PROXY" => :http_proxy,
  "APPSIGNAL_LOG" => :log,
  "APPSIGNAL_LOG_LEVEL" => :log_level,
  "APPSIGNAL_LOG_PATH" => :log_path,
  "APPSIGNAL_LOGGING_ENDPOINT" => :logging_endpoint,
  "APPSIGNAL_PUSH_API_ENDPOINT" => :endpoint,
  "APPSIGNAL_PUSH_API_KEY" => :push_api_key,
  "APPSIGNAL_SIDEKIQ_REPORT_ERRORS" => :sidekiq_report_errors,
  "APPSIGNAL_STATSD_PORT" => :statsd_port,
  "APPSIGNAL_WORKING_DIRECTORY_PATH" => :working_directory_path,
  "APP_REVISION" => :revision
}.freeze
ENV_BOOLEAN_KEYS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  "APPSIGNAL_ACTIVE" => :active,
  "APPSIGNAL_ENABLE_ALLOCATION_TRACKING" => :enable_allocation_tracking,
  "APPSIGNAL_ENABLE_AT_EXIT_REPORTER" => :enable_at_exit_reporter,
  "APPSIGNAL_ENABLE_HOST_METRICS" => :enable_host_metrics,
  "APPSIGNAL_ENABLE_MINUTELY_PROBES" => :enable_minutely_probes,
  "APPSIGNAL_ENABLE_STATSD" => :enable_statsd,
  "APPSIGNAL_ENABLE_NGINX_METRICS" => :enable_nginx_metrics,
  "APPSIGNAL_ENABLE_GVL_GLOBAL_TIMER" => :enable_gvl_global_timer,
  "APPSIGNAL_ENABLE_GVL_WAITING_THREADS" => :enable_gvl_waiting_threads,
  "APPSIGNAL_ENABLE_RAILS_ERROR_REPORTER" => :enable_rails_error_reporter,
  "APPSIGNAL_ENABLE_RAKE_PERFORMANCE_INSTRUMENTATION" =>
    :enable_rake_performance_instrumentation,
  "APPSIGNAL_FILES_WORLD_ACCESSIBLE" => :files_world_accessible,
  "APPSIGNAL_INSTRUMENT_HTTP_RB" => :instrument_http_rb,
  "APPSIGNAL_INSTRUMENT_NET_HTTP" => :instrument_net_http,
  "APPSIGNAL_INSTRUMENT_REDIS" => :instrument_redis,
  "APPSIGNAL_INSTRUMENT_SEQUEL" => :instrument_sequel,
  "APPSIGNAL_RUNNING_IN_CONTAINER" => :running_in_container,
  "APPSIGNAL_SEND_ENVIRONMENT_METADATA" => :send_environment_metadata,
  "APPSIGNAL_SEND_PARAMS" => :send_params,
  "APPSIGNAL_SEND_SESSION_DATA" => :send_session_data
}.freeze
ENV_ARRAY_KEYS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  "APPSIGNAL_DNS_SERVERS" => :dns_servers,
  "APPSIGNAL_FILTER_METADATA" => :filter_metadata,
  "APPSIGNAL_FILTER_PARAMETERS" => :filter_parameters,
  "APPSIGNAL_FILTER_SESSION_DATA" => :filter_session_data,
  "APPSIGNAL_IGNORE_ACTIONS" => :ignore_actions,
  "APPSIGNAL_IGNORE_ERRORS" => :ignore_errors,
  "APPSIGNAL_IGNORE_LOGS" => :ignore_logs,
  "APPSIGNAL_IGNORE_NAMESPACES" => :ignore_namespaces,
  "APPSIGNAL_REQUEST_HEADERS" => :request_headers
}.freeze
ENV_FLOAT_KEYS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  "APPSIGNAL_CPU_COUNT" => :cpu_count
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path, env, logger = Appsignal.internal_logger) ⇒ Config

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new configuration object for AppSignal.

Parameters:

  • root_path (String)

    Root path of the app.

  • initial_env (String)

    The environment to load when AppSignal is started. It will look for an environment with this name in the config/appsignal.yml config file.

  • logger (Logger) (defaults to: Appsignal.internal_logger)

    The logger to use for the AppSignal gem. This is used by the configuration class only. Default: Appsignal.internal_logger. See also Appsignal.start.

  • config_file (String)

    Custom config file location. Default config/appsignal.yml.

See Also:



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/appsignal/config.rb', line 233

def initialize(
  root_path,
  env,
  logger = Appsignal.internal_logger
)
  @root_path = root_path
  @config_file_error = false
  @config_file = config_file
  @logger = logger
  @valid = false

  @env = env.to_s
  @config_hash = {}
  @system_config = {}
  @loaders_config = {}
  @initial_config = {}
  @file_config = {}
  @env_config = {}
  @override_config = {}
  @dsl_config = {} # Can be set using `Appsignal.configure`

  load_config
end

Instance Attribute Details

#config_hashObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



# File 'lib/appsignal/config.rb', line 183

#dsl_configObject (readonly)

Returns the value of attribute dsl_config.



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

def dsl_config
  @dsl_config
end

#envObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



208
209
210
# File 'lib/appsignal/config.rb', line 208

def env
  @env
end

#env_configHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Config loaded from the system environment. Used in diagnose report.

Returns:

  • (Hash)


# File 'lib/appsignal/config.rb', line 183

#file_configHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Config loaded from config/appsignal.yml config file. Used in diagnose report.

Returns:

  • (Hash)


# File 'lib/appsignal/config.rb', line 183

#initial_configObject (readonly)

Returns the value of attribute initial_config.



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

def initial_config
  @initial_config
end

#loaders_configObject (readonly)

Returns the value of attribute loaders_config.



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

def loaders_config
  @loaders_config
end

#loggerObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



212
213
214
# File 'lib/appsignal/config.rb', line 212

def logger
  @logger
end

#override_configObject (readonly)

Returns the value of attribute override_config.



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

def override_config
  @override_config
end

#root_pathObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



208
209
210
# File 'lib/appsignal/config.rb', line 208

def root_path
  @root_path
end

#system_configHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Config detected on the system level. Used in diagnose report.

Returns:

  • (Hash)


# File 'lib/appsignal/config.rb', line 183

Class Method Details

.add_loader_defaults(name, env: nil, root_path: nil, **options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/appsignal/config.rb', line 17

def self.add_loader_defaults(name, env: nil, root_path: nil, **options)
  if Appsignal.config
    Appsignal.internal_logger.warn(
      "The config defaults from the '#{name}' loader are ignored since " \
        "the AppSignal config has already been initialized."
    )
  end

  loader_defaults << {
    :name => name,
    :env => env,
    :root_path => root_path,
    :options => options.compact
  }
end

.determine_env(initial_env = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine which env AppSignal should initialize with.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/appsignal/config.rb', line 35

def self.determine_env(initial_env = nil)
  [
    initial_env,
    ENV.fetch("APPSIGNAL_APP_ENV", nil),
    ENV.fetch("RAILS_ENV", nil),
    ENV.fetch("RACK_ENV", nil)
  ].compact.each do |env|
    return env if env
  end

  loader_defaults.reverse.each do |loader_defaults|
    env = loader_defaults[:env]
    return env if env
  end

  nil
end

.determine_root_pathObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine which root path AppSignal should initialize with.



55
56
57
58
59
60
61
62
# File 'lib/appsignal/config.rb', line 55

def self.determine_root_path
  loader_defaults.reverse.each do |loader_defaults|
    root_path = loader_defaults[:root_path]
    return root_path if root_path
  end

  Dir.pwd
end

.loader_defaultsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



12
13
14
# File 'lib/appsignal/config.rb', line 12

def self.loader_defaults
  @loader_defaults ||= []
end

.system_tmp_dirString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns System's tmp directory.

Returns:

  • (String)

    System's tmp directory.



295
296
297
298
299
300
301
# File 'lib/appsignal/config.rb', line 295

def self.system_tmp_dir
  if Gem.win_platform?
    Dir.tmpdir
  else
    File.realpath("/tmp")
  end
end

Instance Method Details

#[](key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



304
305
306
# File 'lib/appsignal/config.rb', line 304

def [](key)
  config_hash[key]
end

#[]=(key, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Update the internal config hash.

This method does not update the config in the extension and agent. It should not be used to update the config after AppSignal has started.



314
315
316
# File 'lib/appsignal/config.rb', line 314

def []=(key, value)
  config_hash[key] = value
end

#active?Boolean

Returns:

  • (Boolean)


360
361
362
# File 'lib/appsignal/config.rb', line 360

def active?
  @valid && config_hash[:active]
end

#freezeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deep freeze the config object so it cannot be modified during the runtime of the Ruby app.

Since:

  • 4.0.0



442
443
444
445
446
# File 'lib/appsignal/config.rb', line 442

def freeze
  super
  config_hash.freeze
  config_hash.transform_values(&:freeze)
end

#load_configObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/appsignal/config.rb', line 258

def load_config
  # Set defaults
  # Deep duplicate each frozen default value
  merge(DEFAULT_CONFIG.transform_values(&:dup))

  # Set config based on the system
  @system_config = detect_from_system
  merge(system_config)

  # Set defaults from loaders in reverse order so the first register
  # loader's defaults overwrite all others
  self.class.loader_defaults.reverse.each do |loader_defaults|
    defaults = loader_defaults[:options]
    @loaders_config.merge!(defaults.merge(
      :root_path => loader_defaults[:root_path],
      :env => loader_defaults[:env]
    ))
    merge(defaults)
  end

  # Track origin of env
  @initial_config[:env] = @env

  # Load the config file if it exists
  @file_config = load_from_disk || {}
  merge(file_config)

  # Load config from environment variables
  @env_config = load_from_environment
  merge(env_config)
  # Track origin of env
  env_loaded_from_env = ENV.fetch("APPSIGNAL_APP_ENV", nil)
  @env_config[:env] = env_loaded_from_env if env_loaded_from_env
end

#log_file_pathObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/appsignal/config.rb', line 330

def log_file_path
  return @log_file_path if defined? @log_file_path

  path = config_hash[:log_path] || (root_path && File.join(root_path, "log"))
  if path && File.writable?(path)
    @log_file_path = File.join(File.realpath(path), "appsignal.log")
    return @log_file_path
  end

  system_tmp_dir = self.class.system_tmp_dir
  if File.writable? system_tmp_dir
    $stdout.puts "appsignal: Unable to log to '#{path}'. Logging to " \
      "'#{system_tmp_dir}' instead. " \
      "Please check the permissions for the application's (log) " \
      "directory."
    @log_file_path = File.join(system_tmp_dir, "appsignal.log")
  else
    $stdout.puts "appsignal: Unable to log to '#{path}' or the " \
      "'#{system_tmp_dir}' fallback. Please check the permissions " \
      "for the application's (log) directory."
    @log_file_path = nil
  end

  @log_file_path
end

#log_levelObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



319
320
321
322
323
324
325
326
327
# File 'lib/appsignal/config.rb', line 319

def log_level
  option = config_hash[:log_level]
  level =
    if option
      log_level_option = LOG_LEVEL_MAP[option]
      log_level_option
    end
  level.nil? ? Appsignal::Config::DEFAULT_LOG_LEVEL : level
end

#merge_dsl_options(options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



406
407
408
409
# File 'lib/appsignal/config.rb', line 406

def merge_dsl_options(options)
  @dsl_config = options
  merge(options)
end

#valid?Boolean

Returns:

  • (Boolean)


356
357
358
# File 'lib/appsignal/config.rb', line 356

def valid?
  @valid
end

#validateObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/appsignal/config.rb', line 412

def validate
  # Apply any overrides for invalid settings.
  @override_config = determine_overrides
  merge(override_config)

  # Strip path from endpoint so we're backwards compatible with
  # earlier versions of the gem.
  # TODO: Move to its own method, maybe in `#[]=`?
  endpoint_uri = URI(config_hash[:endpoint])
  config_hash[:endpoint] =
    if endpoint_uri.port == 443
      "#{endpoint_uri.scheme}://#{endpoint_uri.host}"
    else
      "#{endpoint_uri.scheme}://#{endpoint_uri.host}:#{endpoint_uri.port}"
    end

  push_api_key = config_hash[:push_api_key] || ""
  if push_api_key.strip.empty?
    @valid = false
    @logger.error "Push API key not set after loading config"
  else
    @valid = true
  end
end

#write_to_environmentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/appsignal/config.rb', line 365

def write_to_environment # rubocop:disable Metrics/AbcSize
  ENV["_APPSIGNAL_ACTIVE"]                       = active?.to_s
  ENV["_APPSIGNAL_AGENT_PATH"]                   = File.expand_path("../../ext", __dir__).to_s
  ENV["_APPSIGNAL_APP_NAME"]                     = config_hash[:name]
  ENV["_APPSIGNAL_APP_PATH"]                     = root_path.to_s
  ENV["_APPSIGNAL_BIND_ADDRESS"]                 = config_hash[:bind_address].to_s
  ENV["_APPSIGNAL_CA_FILE_PATH"]                 = config_hash[:ca_file_path].to_s
  ENV["_APPSIGNAL_CPU_COUNT"]                    = config_hash[:cpu_count].to_s
  ENV["_APPSIGNAL_DNS_SERVERS"]                  = config_hash[:dns_servers].join(",")
  ENV["_APPSIGNAL_ENABLE_HOST_METRICS"]          = config_hash[:enable_host_metrics].to_s
  ENV["_APPSIGNAL_ENABLE_STATSD"]                = config_hash[:enable_statsd].to_s
  ENV["_APPSIGNAL_ENABLE_NGINX_METRICS"]         = config_hash[:enable_nginx_metrics].to_s
  ENV["_APPSIGNAL_APP_ENV"]                      = env
  ENV["_APPSIGNAL_FILES_WORLD_ACCESSIBLE"]       = config_hash[:files_world_accessible].to_s
  ENV["_APPSIGNAL_FILTER_PARAMETERS"]            = config_hash[:filter_parameters].join(",")
  ENV["_APPSIGNAL_FILTER_SESSION_DATA"]          = config_hash[:filter_session_data].join(",")
  ENV["_APPSIGNAL_HOSTNAME"]                     = config_hash[:hostname].to_s
  ENV["_APPSIGNAL_HOST_ROLE"]                    = config_hash[:host_role].to_s
  ENV["_APPSIGNAL_HTTP_PROXY"]                   = config_hash[:http_proxy]
  ENV["_APPSIGNAL_IGNORE_ACTIONS"]               = config_hash[:ignore_actions].join(",")
  ENV["_APPSIGNAL_IGNORE_ERRORS"]                = config_hash[:ignore_errors].join(",")
  ENV["_APPSIGNAL_IGNORE_LOGS"]                  = config_hash[:ignore_logs].join(",")
  ENV["_APPSIGNAL_IGNORE_NAMESPACES"]            = config_hash[:ignore_namespaces].join(",")
  ENV["_APPSIGNAL_LANGUAGE_INTEGRATION_VERSION"] = "ruby-#{Appsignal::VERSION}"
  ENV["_APPSIGNAL_LOG"]                          = config_hash[:log]
  ENV["_APPSIGNAL_LOG_LEVEL"]                    = config_hash[:log_level]
  ENV["_APPSIGNAL_LOG_FILE_PATH"]                = log_file_path.to_s if log_file_path
  ENV["_APPSIGNAL_LOGGING_ENDPOINT"]             = config_hash[:logging_endpoint]
  ENV["_APPSIGNAL_PROCESS_NAME"]                 = $PROGRAM_NAME
  ENV["_APPSIGNAL_PUSH_API_ENDPOINT"]            = config_hash[:endpoint]
  ENV["_APPSIGNAL_PUSH_API_KEY"]                 = config_hash[:push_api_key]
  ENV["_APPSIGNAL_RUNNING_IN_CONTAINER"]         = config_hash[:running_in_container].to_s
  ENV["_APPSIGNAL_SEND_ENVIRONMENT_METADATA"]    = config_hash[:send_environment_metadata].to_s
  ENV["_APPSIGNAL_STATSD_PORT"]                  = config_hash[:statsd_port].to_s
  if config_hash[:working_directory_path]
    ENV["_APPSIGNAL_WORKING_DIRECTORY_PATH"] = config_hash[:working_directory_path]
  end
  ENV["_APP_REVISION"] = config_hash[:revision].to_s
end