Skip to content

Commit

Permalink
Merge pull request #1320 from appsignal/refactor-config-option-types
Browse files Browse the repository at this point in the history
Refactor config option types
  • Loading branch information
tombruijn authored Oct 23, 2024
2 parents bce4e97 + 7cdaf7f commit c615c09
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 64 deletions.
123 changes: 63 additions & 60 deletions lib/appsignal/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,64 +120,67 @@ def self.determine_root_path
}.freeze

# @api private
ENV_STRING_KEYS = {
"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
STRING_OPTIONS = {
:activejob_report_errors => "APPSIGNAL_ACTIVEJOB_REPORT_ERRORS",
:name => "APPSIGNAL_APP_NAME",
:bind_address => "APPSIGNAL_BIND_ADDRESS",
:ca_file_path => "APPSIGNAL_CA_FILE_PATH",
:hostname => "APPSIGNAL_HOSTNAME",
:host_role => "APPSIGNAL_HOST_ROLE",
:http_proxy => "APPSIGNAL_HTTP_PROXY",
:log => "APPSIGNAL_LOG",
:log_level => "APPSIGNAL_LOG_LEVEL",
:log_path => "APPSIGNAL_LOG_PATH",
:logging_endpoint => "APPSIGNAL_LOGGING_ENDPOINT",
:endpoint => "APPSIGNAL_PUSH_API_ENDPOINT",
:push_api_key => "APPSIGNAL_PUSH_API_KEY",
:sidekiq_report_errors => "APPSIGNAL_SIDEKIQ_REPORT_ERRORS",
:statsd_port => "APPSIGNAL_STATSD_PORT",
:working_directory_path => "APPSIGNAL_WORKING_DIRECTORY_PATH",
:revision => "APP_REVISION"
}.freeze

# @api private
ENV_BOOLEAN_KEYS = {
"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
BOOLEAN_OPTIONS = {
:active => "APPSIGNAL_ACTIVE",
:enable_allocation_tracking => "APPSIGNAL_ENABLE_ALLOCATION_TRACKING",
:enable_at_exit_reporter => "APPSIGNAL_ENABLE_AT_EXIT_REPORTER",
:enable_host_metrics => "APPSIGNAL_ENABLE_HOST_METRICS",
:enable_minutely_probes => "APPSIGNAL_ENABLE_MINUTELY_PROBES",
:enable_statsd => "APPSIGNAL_ENABLE_STATSD",
:enable_nginx_metrics => "APPSIGNAL_ENABLE_NGINX_METRICS",
:enable_gvl_global_timer => "APPSIGNAL_ENABLE_GVL_GLOBAL_TIMER",
:enable_gvl_waiting_threads => "APPSIGNAL_ENABLE_GVL_WAITING_THREADS",
:enable_rails_error_reporter => "APPSIGNAL_ENABLE_RAILS_ERROR_REPORTER",
:enable_rake_performance_instrumentation =>
"APPSIGNAL_ENABLE_RAKE_PERFORMANCE_INSTRUMENTATION",
:files_world_accessible => "APPSIGNAL_FILES_WORLD_ACCESSIBLE",
:instrument_http_rb => "APPSIGNAL_INSTRUMENT_HTTP_RB",
:instrument_net_http => "APPSIGNAL_INSTRUMENT_NET_HTTP",
:instrument_redis => "APPSIGNAL_INSTRUMENT_REDIS",
:instrument_sequel => "APPSIGNAL_INSTRUMENT_SEQUEL",
:running_in_container => "APPSIGNAL_RUNNING_IN_CONTAINER",
:send_environment_metadata => "APPSIGNAL_SEND_ENVIRONMENT_METADATA",
:send_params => "APPSIGNAL_SEND_PARAMS",
:send_session_data => "APPSIGNAL_SEND_SESSION_DATA"
}.freeze

# @api private
ENV_ARRAY_KEYS = {
"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
ARRAY_OPTIONS = {
:dns_servers => "APPSIGNAL_DNS_SERVERS",
:filter_metadata => "APPSIGNAL_FILTER_METADATA",
:filter_parameters => "APPSIGNAL_FILTER_PARAMETERS",
:filter_session_data => "APPSIGNAL_FILTER_SESSION_DATA",
:ignore_actions => "APPSIGNAL_IGNORE_ACTIONS",
:ignore_errors => "APPSIGNAL_IGNORE_ERRORS",
:ignore_logs => "APPSIGNAL_IGNORE_LOGS",
:ignore_namespaces => "APPSIGNAL_IGNORE_NAMESPACES",
:request_headers => "APPSIGNAL_REQUEST_HEADERS"
}.freeze

# @api private
ENV_FLOAT_KEYS = {
"APPSIGNAL_CPU_COUNT" => :cpu_count
FLOAT_OPTIONS = {
:cpu_count => "APPSIGNAL_CPU_COUNT"
}.freeze

# @api private
Expand Down Expand Up @@ -470,31 +473,31 @@ def load_from_environment
config = {}

# Configuration with string type
ENV_STRING_KEYS.each do |env_key, option|
STRING_OPTIONS.each do |option, env_key|
env_var = ENV.fetch(env_key, nil)
next unless env_var

config[option] = env_var
end

# Configuration with boolean type
ENV_BOOLEAN_KEYS.each do |env_key, option|
BOOLEAN_OPTIONS.each do |option, env_key|
env_var = ENV.fetch(env_key, nil)
next unless env_var

config[option] = env_var.casecmp("true").zero?
end

# Configuration with array of strings type
ENV_ARRAY_KEYS.each do |env_key, option|
ARRAY_OPTIONS.each do |option, env_key|
env_var = ENV.fetch(env_key, nil)
next unless env_var

config[option] = env_var.split(",")
end

# Configuration with float type
ENV_FLOAT_KEYS.each do |env_key, option|
FLOAT_OPTIONS.each do |option, env_key|
env_var = ENV.fetch(env_key, nil)
next unless env_var

Expand Down Expand Up @@ -550,7 +553,7 @@ def env
@config.env
end

Appsignal::Config::ENV_STRING_KEYS.each_value do |option|
Appsignal::Config::STRING_OPTIONS.each_key do |option|
define_method(option) do
fetch_option(option)
end
Expand All @@ -560,7 +563,7 @@ def env
end
end

Appsignal::Config::ENV_BOOLEAN_KEYS.each_value do |option|
Appsignal::Config::BOOLEAN_OPTIONS.each_key do |option|
define_method(option) do
fetch_option(option)
end
Expand All @@ -570,7 +573,7 @@ def env
end
end

Appsignal::Config::ENV_ARRAY_KEYS.each_value do |option|
Appsignal::Config::ARRAY_OPTIONS.each_key do |option|
define_method(option) do
fetch_option(option)
end
Expand All @@ -580,7 +583,7 @@ def env
end
end

Appsignal::Config::ENV_FLOAT_KEYS.each_value do |option|
Appsignal::Config::FLOAT_OPTIONS.each_key do |option|
define_method(option) do
fetch_option(option)
end
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/appsignal/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ def on_load
it "reads all string env keys" do
config

Appsignal::Config::ENV_STRING_KEYS.each do |env_key, option|
Appsignal::Config::STRING_OPTIONS.each do |option, env_key|
ENV.fetch(env_key) { raise "Config env var '#{env_key}' is not set for this test" }
expect(config[option]).to eq(ENV.fetch(env_key, nil))
end
Expand All @@ -627,7 +627,7 @@ def on_load
it "reads all boolean env keys" do
config

Appsignal::Config::ENV_BOOLEAN_KEYS.each do |env_key, option|
Appsignal::Config::BOOLEAN_OPTIONS.each do |option, env_key|
ENV.fetch(env_key) { raise "Config env var '#{env_key}' is not set for this test" }
expect(config[option]).to eq(ENV.fetch(env_key, nil) == "true")
end
Expand All @@ -636,7 +636,7 @@ def on_load
it "reads all array env keys" do
config

Appsignal::Config::ENV_ARRAY_KEYS.each do |env_key, option|
Appsignal::Config::ARRAY_OPTIONS.each do |option, env_key|
ENV.fetch(env_key) { raise "Config env var '#{env_key}' is not set for this test" }
expect(config[option]).to eq(ENV.fetch(env_key, nil).split(","))
end
Expand All @@ -645,7 +645,7 @@ def on_load
it "reads all float env keys" do
config

Appsignal::Config::ENV_FLOAT_KEYS.each do |env_key, option|
Appsignal::Config::FLOAT_OPTIONS.each do |option, env_key|
ENV.fetch(env_key) { raise "Config env var '#{env_key}' is not set for this test" }
expect(config[option]).to eq(ENV.fetch(env_key, nil).to_f)
end
Expand Down

0 comments on commit c615c09

Please sign in to comment.