Class: Appsignal::Config

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

Constant Summary collapse

DEFAULT_CONFIG =
{
  :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,
  :endpoint                       => "https://push.appsignal.com",
  :files_world_accessible         => true,
  :filter_parameters              => [],
  :filter_session_data            => [],
  :ignore_actions                 => [],
  :ignore_errors                  => [],
  :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_URI 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_APP_NAME"                       => :name,
  "APPSIGNAL_CA_FILE_PATH"                   => :ca_file_path,
  "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_FILES_WORLD_ACCESSIBLE"         => :files_world_accessible,
  "APPSIGNAL_FILTER_PARAMETERS"              => :filter_parameters,
  "APPSIGNAL_FILTER_SESSION_DATA"            => :filter_session_data,
  "APPSIGNAL_HOSTNAME"                       => :hostname,
  "APPSIGNAL_HTTP_PROXY"                     => :http_proxy,
  "APPSIGNAL_IGNORE_ACTIONS"                 => :ignore_actions,
  "APPSIGNAL_IGNORE_ERRORS"                  => :ignore_errors,
  "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_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_APP_NAME
  APPSIGNAL_CA_FILE_PATH
  APPSIGNAL_HOSTNAME
  APPSIGNAL_HTTP_PROXY
  APPSIGNAL_LOG
  APPSIGNAL_LOG_LEVEL
  APPSIGNAL_LOG_PATH
  APPSIGNAL_LOGGING_ENDPOINT
  APPSIGNAL_PUSH_API_ENDPOINT
  APPSIGNAL_PUSH_API_KEY
  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_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_PARAMETERS
  APPSIGNAL_FILTER_SESSION_DATA
  APPSIGNAL_IGNORE_ACTIONS
  APPSIGNAL_IGNORE_ERRORS
  APPSIGNAL_IGNORE_NAMESPACES
  APPSIGNAL_REQUEST_HEADERS
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::DeprecationMessage

#deprecation_message, message

Constructor Details

#initialize(root_path, env, initial_config = {}, logger = Appsignal.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.logger)

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

  • config_file (String) (defaults to: nil)

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

See Also:



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/appsignal/config.rb', line 221

def initialize(root_path, env, initial_config = {}, logger = Appsignal.logger, config_file = nil)
  @root_path      = root_path
  @config_file    = config_file
  @logger         = logger
  @valid          = false
  @config_hash    = Hash[DEFAULT_CONFIG]
  env_loaded_from_initial = env.to_s
  @env =
    if ENV.key?("APPSIGNAL_APP_ENV".freeze)
      env_loaded_from_env = ENV["APPSIGNAL_APP_ENV".freeze]
    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:



185
186
# File 'lib/appsignal/config.rb', line 185

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.



185
186
187
# File 'lib/appsignal/config.rb', line 185

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)


185
186
# File 'lib/appsignal/config.rb', line 185

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)


185
186
# File 'lib/appsignal/config.rb', line 185

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)


185
186
# File 'lib/appsignal/config.rb', line 185

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

#loggerObject

Returns the value of attribute logger.



187
188
189
# File 'lib/appsignal/config.rb', line 187

def logger
  @logger
end

#override_configObject (readonly)

Returns the value of attribute override_config.



185
186
187
# File 'lib/appsignal/config.rb', line 185

def override_config
  @override_config
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



185
186
187
# File 'lib/appsignal/config.rb', line 185

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)


185
186
# File 'lib/appsignal/config.rb', line 185

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.



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

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

Instance Method Details

#[](key) ⇒ Object



269
270
271
# File 'lib/appsignal/config.rb', line 269

def [](key)
  config_hash[key]
end

#[]=(key, value) ⇒ Object



273
274
275
# File 'lib/appsignal/config.rb', line 273

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

#active?Boolean

Returns:

  • (Boolean)


312
313
314
# File 'lib/appsignal/config.rb', line 312

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

#log_file_pathObject



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/appsignal/config.rb', line 289

def log_file_path
  path = config_hash[:log_path] || root_path && File.join(root_path, "log")
  if path && File.writable?(path)
    return File.join(File.realpath(path), "appsignal.log")
  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."
    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."
  end
end

#log_levelObject



277
278
279
280
281
282
283
284
285
286
287
# File 'lib/appsignal/config.rb', line 277

def log_level
  if config_hash[:debug] || config_hash[:transaction_debug_mode]
    level = ::Logger::DEBUG
  end
  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)


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

def valid?
  @valid
end

#validateObject



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/appsignal/config.rb', line 352

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



316
317
318
319
320
321
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
347
348
349
350
# File 'lib/appsignal/config.rb', line 316

def write_to_environment # rubocop:disable Metrics/AbcSize
  ENV["_APPSIGNAL_ACTIVE"]                       = active?.to_s
  ENV["_APPSIGNAL_AGENT_PATH"]                   = File.expand_path("../../../ext", __FILE__).to_s
  ENV["_APPSIGNAL_APP_NAME"]                     = config_hash[:name]
  ENV["_APPSIGNAL_APP_PATH"]                     = root_path.to_s
  ENV["_APPSIGNAL_CA_FILE_PATH"]                 = config_hash[:ca_file_path].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_ENVIRONMENT"]                  = 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_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_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_TRANSACTION_DEBUG_MODE"]       = config_hash[:transaction_debug_mode].to_s
  ENV["_APPSIGNAL_WORKING_DIRECTORY_PATH"]       = config_hash[:working_directory_path] if config_hash[:working_directory_path]
  ENV["_APPSIGNAL_WORKING_DIR_PATH"]             = config_hash[:working_dir_path] if config_hash[:working_dir_path]
  ENV["_APP_REVISION"]                           = config_hash[:revision].to_s
end