Class: Chronos::Configuration

Inherits:
Object
  • Object
show all
Includes:
Internal::ConfigurationValidation
Defined in:
lib/chronos/configuration.rb

Overview

Mutable configuration used only while the Chronos agent is being set up.

Examples:

config = Chronos::Configuration.new
config.project_id = 'project-id'
config.project_key = 'project-key'
config.host = 'https://chronos.example.com'
snapshot = config.snapshot

Constant Summary collapse

DEFAULT_BLOCKLIST_KEYS =
%w(
  password password_confirmation passwd secret api_key apikey authorization
  token access_token refresh_token private_key client_secret cookie set-cookie
  session credit_card card_number cvv cpf cnpj
).freeze
CONTEXT_STORE_METHODS =
[:get, :set, :clear, :with_context].freeze
ATTRIBUTES =
[
  :project_id, :project_key, :host, :environment, :app_version,
  :service_name, :root_directory, :logger, :timeout, :open_timeout,
  :queue_size, :workers, :enabled, :error_notifications,
  :ignored_environments, :proxy, :ssl_verify, :user_agent,
  :max_payload_size, :gzip, :blocklist_keys, :allowlist_keys,
  :filters, :hash_keys, :anonymize_ip, :max_retries,
  :retry_base_interval, :retry_max_interval, :retry_jitter,
  :backlog_size, :circuit_failure_threshold, :circuit_reset_timeout,
  :remote_configuration, :remote_config_max_bytes, :sampling_rate,
  :enabled_event_types, :max_remote_send_interval, :context_store,
  :breadcrumb_capacity, :breadcrumb_max_bytes, :rails_enabled,
  :rails_capture_in_console, :rails_capture_in_test, :rails_capture_user_agent
].freeze
Snapshot =

Immutable configuration shared by all runtime components.

Examples:

snapshot.enabled_for_environment? #=> true
Class.new do
  attr_reader(*Configuration::ATTRIBUTES)

  def initialize(values)
    Configuration::ATTRIBUTES.each do |attribute|
      value = values[attribute]
      deep_freeze(value)
      instance_variable_set("@#{attribute}", value)
    end
    freeze
  end

  def enabled_for_environment?
    enabled && !ignored_environments.map(&:to_s).include?(environment.to_s)
  end

  private # rubocop:disable Layout/AccessModifierIndentation

  def deep_freeze(value)
    return value if value.respond_to?(:call) || context_store?(value)

    case value
    when Hash
      value.each do |key, child|
        deep_freeze(key)
        deep_freeze(child)
      end
    when Array
      value.each { |child| deep_freeze(child) }
    end
    value.freeze
  end

  def context_store?(value)
    Configuration::CONTEXT_STORE_METHODS.all? { |method_name| value.respond_to?(method_name) }
  rescue StandardError
    false
  end
end

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



48
49
50
51
52
53
# File 'lib/chronos/configuration.rb', line 48

def initialize
  initialize_core_defaults
  initialize_privacy_defaults
  initialize_resilience_defaults
  initialize_rails_defaults
end

Instance Method Details

#snapshotObject

Raises:



55
56
57
58
59
60
# File 'lib/chronos/configuration.rb', line 55

def snapshot
  errors = validation_errors
  raise ConfigurationError, errors.join(", ") unless errors.empty?

  Snapshot.new(to_hash)
end

#valid?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/chronos/configuration.rb', line 62

def valid?
  validation_errors.empty?
end

#validation_errorsObject

rubocop:disable Metrics/AbcSize



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chronos/configuration.rb', line 66

def validation_errors # rubocop:disable Metrics/AbcSize
  errors = []
  if enabled
    errors << "project_id is required" if blank?(project_id)
    errors << "project_key is required" if blank?(project_key)
    errors.concat(host_errors)
  end
  errors << "timeout must be greater than zero" unless positive_number?(timeout)
  errors << "open_timeout must be greater than zero" unless positive_number?(open_timeout)
  errors << "queue_size must be a positive integer" unless positive_integer?(queue_size)
  errors << "workers must be a positive integer" unless positive_integer?(workers)
  errors << "max_payload_size must be a positive integer" unless positive_integer?(max_payload_size)
  errors.concat(resilience_errors)
  errors.concat(privacy_errors)
  errors.concat(context_errors)
  errors
end