Class: Appsignal::Config

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

Constant Summary collapse

DEFAULT_CONFIG =
{
  :activejob_report_errors => "all",
  :ca_file_path => File.expand_path(File.join("../../../resources/cacert.pem"), __FILE__),
  :debug => false,
  :dns_servers => [],
  :enable_allocation_tracking => 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,
  :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,
  :transaction_debug_mode => false
}.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_TO_KEY_MAPPING =
{
  "APPSIGNAL_ACTIVE" => :active,
  "APPSIGNAL_ACTIVE_JOB_REPORT_ERRORS" => :activejob_report_errors,
  "APPSIGNAL_APP_NAME" => :name,
  "APPSIGNAL_BIND_ADDRESS" => :bind_address,
  "APPSIGNAL_CA_FILE_PATH" => :ca_file_path,
  "APPSIGNAL_CPU_COUNT" => :cpu_count,
  "APPSIGNAL_DEBUG" => :debug,
  "APPSIGNAL_DNS_SERVERS" => :dns_servers,
  "APPSIGNAL_ENABLE_ALLOCATION_TRACKING" => :enable_allocation_tracking,
  "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_FILES_WORLD_ACCESSIBLE" => :files_world_accessible,
  "APPSIGNAL_FILTER_METADATA" => :filter_metadata,
  "APPSIGNAL_FILTER_PARAMETERS" => :filter_parameters,
  "APPSIGNAL_FILTER_SESSION_DATA" => :filter_session_data,
  "APPSIGNAL_HOSTNAME" => :hostname,
  "APPSIGNAL_HOST_ROLE" => :host_role,
  "APPSIGNAL_HTTP_PROXY" => :http_proxy,
  "APPSIGNAL_IGNORE_ACTIONS" => :ignore_actions,
  "APPSIGNAL_IGNORE_ERRORS" => :ignore_errors,
  "APPSIGNAL_IGNORE_LOGS" => :ignore_logs,
  "APPSIGNAL_IGNORE_NAMESPACES" => :ignore_namespaces,
  "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_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_REQUEST_HEADERS" => :request_headers,
  "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,
  "APPSIGNAL_SKIP_SESSION_DATA" => :skip_session_data,
  "APPSIGNAL_STATSD_PORT" => :statsd_port,
  "APPSIGNAL_TRANSACTION_DEBUG_MODE" => :transaction_debug_mode,
  "APPSIGNAL_WORKING_DIRECTORY_PATH" => :working_directory_path,
  "APPSIGNAL_WORKING_DIR_PATH" => :working_dir_path,
  "APP_REVISION" => :revision
}.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.

%w[
  APPSIGNAL_ACTIVE_JOB_REPORT_ERRORS
  APPSIGNAL_APP_NAME
  APPSIGNAL_BIND_ADDRESS
  APPSIGNAL_CA_FILE_PATH
  APPSIGNAL_HOSTNAME
  APPSIGNAL_HOST_ROLE
  APPSIGNAL_HTTP_PROXY
  APPSIGNAL_LOG
  APPSIGNAL_LOG_LEVEL
  APPSIGNAL_LOG_PATH
  APPSIGNAL_LOGGING_ENDPOINT
  APPSIGNAL_PUSH_API_ENDPOINT
  APPSIGNAL_PUSH_API_KEY
  APPSIGNAL_STATSD_PORT
  APPSIGNAL_WORKING_DIRECTORY_PATH
  APPSIGNAL_WORKING_DIR_PATH
  APP_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.

%w[
  APPSIGNAL_ACTIVE
  APPSIGNAL_DEBUG
  APPSIGNAL_ENABLE_ALLOCATION_TRACKING
  APPSIGNAL_ENABLE_HOST_METRICS
  APPSIGNAL_ENABLE_MINUTELY_PROBES
  APPSIGNAL_ENABLE_STATSD
  APPSIGNAL_ENABLE_NGINX_METRICS
  APPSIGNAL_ENABLE_GVL_GLOBAL_TIMER
  APPSIGNAL_ENABLE_GVL_WAITING_THREADS
  APPSIGNAL_ENABLE_RAILS_ERROR_REPORTER
  APPSIGNAL_FILES_WORLD_ACCESSIBLE
  APPSIGNAL_INSTRUMENT_HTTP_RB
  APPSIGNAL_INSTRUMENT_NET_HTTP
  APPSIGNAL_INSTRUMENT_REDIS
  APPSIGNAL_INSTRUMENT_SEQUEL
  APPSIGNAL_RUNNING_IN_CONTAINER
  APPSIGNAL_SEND_ENVIRONMENT_METADATA
  APPSIGNAL_SEND_PARAMS
  APPSIGNAL_SEND_SESSION_DATA
  APPSIGNAL_SKIP_SESSION_DATA
  APPSIGNAL_TRANSACTION_DEBUG_MODE
].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.

%w[
  APPSIGNAL_DNS_SERVERS
  APPSIGNAL_FILTER_METADATA
  APPSIGNAL_FILTER_PARAMETERS
  APPSIGNAL_FILTER_SESSION_DATA
  APPSIGNAL_IGNORE_ACTIONS
  APPSIGNAL_IGNORE_ERRORS
  APPSIGNAL_IGNORE_LOGS
  APPSIGNAL_IGNORE_NAMESPACES
  APPSIGNAL_REQUEST_HEADERS
].freeze
ENV_FLOAT_KEYS =
%w[
  APPSIGNAL_CPU_COUNT
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::StdoutAndLoggerMessage

#stdout_and_logger_warning, warning

Constructor Details

#initialize(root_path, env, initial_config = {}, logger = Appsignal.internal_logger, config_file = nil) ⇒ Config

Initialize a new configuration object for AppSignal.

If this is manually initialized, and not by Appsignal.start, it needs to be assigned to the Appsignal.config attribute.

Examples:

require "appsignal"
Appsignal.config = Appsignal::Config.new(
  app_path,
  "production"
)
Appsignal.start

Parameters:

  • root_path (String)

    Root path of the app.

  • 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.

  • initial_config (Hash<String, Object>) (defaults to: {})

    The initial configuration to use. This will be overwritten by the file config and environment variables config.

  • 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_logger.

  • config_file (String) (defaults to: nil)

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

See Also:



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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
# File 'lib/appsignal/config.rb', line 243

def initialize(
  root_path,
  env,
  initial_config = {},
  logger = Appsignal.internal_logger,
  config_file = nil
)
  @config_file_error = false
  @root_path = root_path
  @config_file = config_file
  @logger = logger
  @valid = false
  @config_hash = DEFAULT_CONFIG.dup
  env_loaded_from_initial = env.to_s
  @env =
    if ENV.key?("APPSIGNAL_APP_ENV")
      env_loaded_from_env = ENV["APPSIGNAL_APP_ENV"]
    else
      env_loaded_from_initial
    end

  # Set config based on the system
  @system_config = detect_from_system
  merge(system_config)
  # Initial config
  @initial_config = initial_config
  merge(initial_config)
  # 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)
  # Load config overrides
  @override_config = determine_overrides
  merge(override_config)
  # Handle deprecated config options
  maintain_backwards_compatibility
  # Validate that we have a correct config
  validate
  # Track origin of env
  @initial_config[:env] = env_loaded_from_initial if env_loaded_from_initial
  @env_config[:env] = env_loaded_from_env if env_loaded_from_env
end

Instance Attribute Details

#config_hashHash (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 used by the AppSignal gem. Combined Hash of the #system_config, #initial_config, #file_config, #env_config attributes.

Returns:

  • (Hash)

See Also:



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

attr_reader :root_path, :env, :config_hash, :system_config,
:initial_config, :file_config, :env_config, :override_config

#envObject (readonly)

Returns the value of attribute env.



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

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)


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

attr_reader :root_path, :env, :config_hash, :system_config,
:initial_config, :file_config, :env_config, :override_config

#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)


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

attr_reader :root_path, :env, :config_hash, :system_config,
:initial_config, :file_config, :env_config, :override_config

#initial_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)


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

attr_reader :root_path, :env, :config_hash, :system_config,
:initial_config, :file_config, :env_config, :override_config

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#override_configObject (readonly)

Returns the value of attribute override_config.



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

def override_config
  @override_config
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



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

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)


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

attr_reader :root_path, :env, :config_hash, :system_config,
:initial_config, :file_config, :env_config, :override_config

Class Method Details

.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.



290
291
292
293
294
295
296
# File 'lib/appsignal/config.rb', line 290

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

Instance Method Details

#[](key) ⇒ Object



298
299
300
# File 'lib/appsignal/config.rb', line 298

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.



308
309
310
# File 'lib/appsignal/config.rb', line 308

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

#active?Boolean

Returns:

  • (Boolean)


352
353
354
# File 'lib/appsignal/config.rb', line 352

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

#log_file_pathObject



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/appsignal/config.rb', line 322

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



312
313
314
315
316
317
318
319
320
# File 'lib/appsignal/config.rb', line 312

def log_level
  level = ::Logger::DEBUG if config_hash[:debug] || config_hash[:transaction_debug_mode]
  option = config_hash[:log_level]
  if option
    log_level_option = LOG_LEVEL_MAP[option]
    level = log_level_option if log_level_option
  end
  level.nil? ? Appsignal::Config::DEFAULT_LOG_LEVEL : level
end

#valid?Boolean

Returns:

  • (Boolean)


348
349
350
# File 'lib/appsignal/config.rb', line 348

def valid?
  @valid
end

#validateObject



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/appsignal/config.rb', line 401

def validate
  # 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

rubocop:disable Metrics/AbcSize



356
357
358
359
360
361
362
363
364
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
# File 'lib/appsignal/config.rb', line 356

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_DEBUG_LOGGING"]                = config_hash[:debug].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
  ENV["_APPSIGNAL_TRANSACTION_DEBUG_MODE"]       = config_hash[:transaction_debug_mode].to_s
  if config_hash[:working_directory_path]
    ENV["_APPSIGNAL_WORKING_DIRECTORY_PATH"] = config_hash[:working_directory_path]
  end
  if config_hash[:working_dir_path]
    ENV["_APPSIGNAL_WORKING_DIR_PATH"] = config_hash[:working_dir_path]
  end
  ENV["_APP_REVISION"] = config_hash[:revision].to_s
end