Skip to content

Commit

Permalink
Config cleanup (#55)
Browse files Browse the repository at this point in the history
* Clear the ENV in the setup in appsignal_config_test

* Test default and valid configurations

* Split up the application environment config tests

* Remove Heroku DYNO check, add test for :running_in_container

* Clean up minimum configuration tests

* Add tests to make sure the System env gets set from the application env

* Set APPSIGNAL_FILTER_PARAMETERS from application env

* Set APPSIGNAL_SEND_PARAMS from application env

*  Add tests to make sure the application env gets set from the System env

*  Set :ignore_actions from system env

* Set :ignore_errors from system env

*  Set :log from system env

*  Set :log_path from system env

* Remove duplicate config tests

* Clean up the setup block in the config tests

* Add a test to make sure the system environment overwrites the application environment configuration

* :running_in_container gets set automatically on Heroku

* Clear APPSIGNAL_FILTER_PARAMETERS in filter_test.exs

Since 4fb068, APPSIGNAL_FILTER_PARAMETERS gets set from
Appsignal.write_to_environment/1. This commit clears it in
filter_test.exs, so it doesn't get set into the application config in
the next test.
  • Loading branch information
jeffkreeftmeijer authored and Arjan Scherpenisse committed Jan 10, 2017
1 parent 7f070aa commit 78ab765
Show file tree
Hide file tree
Showing 3 changed files with 345 additions and 58 deletions.
24 changes: 15 additions & 9 deletions lib/appsignal/config.ex
Expand Up @@ -3,13 +3,14 @@ defmodule Appsignal.Config do

@default_config %{
debug: false,
ignore_errors: [],
ignore_actions: [],
env: :dev,
send_params: true,
endpoint: "https://push.appsignal.com",
enable_host_metrics: false,
endpoint: "https://push.appsignal.com",
env: :dev,
filter_parameters: nil,
ignore_actions: [],
ignore_errors: [],
running_in_container: false,
send_params: true,
skip_session_data: false
}

Expand All @@ -20,10 +21,12 @@ defmodule Appsignal.Config do
"""
@spec initialize() :: :ok | {:error, :invalid_config}
def initialize() do
system_config = %{running_in_container: System.get_env("DYNO") != nil}
app_config = Application.get_env(:appsignal, :config, []) |> coerce_map
env_config = load_from_environment()

config = @default_config
|> Map.merge(system_config)
|> Map.merge(app_config)
|> Map.merge(env_config)

Expand Down Expand Up @@ -63,6 +66,7 @@ defmodule Appsignal.Config do
"APPSIGNAL_FRONTEND_ERROR_CATCHING_PATH" => :frontend_error_catching_path,
"APPSIGNAL_FILTER_PARAMETERS" => :filter_parameters,
"APPSIGNAL_DEBUG" => :debug,
"APPSIGNAL_LOG" => :log,
"APPSIGNAL_LOG_PATH" => :log_path,
"APPSIGNAL_IGNORE_ERRORS" => :ignore_errors,
"APPSIGNAL_IGNORE_ACTIONS" => :ignore_actions,
Expand All @@ -73,10 +77,10 @@ defmodule Appsignal.Config do
"APPSIGNAL_SKIP_SESSION_DATA" => :skip_session_data
}

@string_keys ~w(APPSIGNAL_PUSH_API_KEY APPSIGNAL_PUSH_API_ENDPOINT APPSIGNAL_FRONTEND_ERROR_CATCHING_PATH APPSIGNAL_HTTP_PROXY APPSIGNAL_LOG_PATH APPSIGNAL_WORKING_DIR_PATH APP_REVISION)
@string_keys ~w(APPSIGNAL_PUSH_API_KEY APPSIGNAL_PUSH_API_ENDPOINT APPSIGNAL_FRONTEND_ERROR_CATCHING_PATH APPSIGNAL_HTTP_PROXY APPSIGNAL_LOG APPSIGNAL_LOG_PATH APPSIGNAL_WORKING_DIR_PATH APP_REVISION)
@bool_keys ~w(APPSIGNAL_ACTIVE APPSIGNAL_DEBUG APPSIGNAL_INSTRUMENT_NET_HTTP APPSIGNAL_ENABLE_FRONTEND_ERROR_CATCHING APPSIGNAL_ENABLE_ALLOCATION_TRACKING APPSIGNAL_ENABLE_GC_INSTRUMENTATION APPSIGNAL_RUNNING_IN_CONTAINER APPSIGNAL_ENABLE_HOST_METRICS APPSIGNAL_SKIP_SESSION_DATA)
@atom_keys ~w(APPSIGNAL_APP_NAME APPSIGNAL_ENVIRONMENT)
@string_list_keys ~w(APPSIGNAL_FILTER_PARAMETERS)
@string_list_keys ~w(APPSIGNAL_FILTER_PARAMETERS APPSIGNAL_IGNORE_ACTIONS APPSIGNAL_IGNORE_ERRORS)

defp load_environment(config, list, converter) do
list |> Enum.reduce(
Expand All @@ -93,8 +97,6 @@ defmodule Appsignal.Config do

defp load_from_environment() do
%{}
# Heroku is a container based system
|> Map.put(:running_in_container, System.get_env("DYNO") != nil)
|> load_environment(@string_keys, &(&1))
|> load_environment(@bool_keys, &(true?(&1)))
|> load_environment(@atom_keys, &(String.to_atom(&1)))
Expand Down Expand Up @@ -138,8 +140,12 @@ defmodule Appsignal.Config do
unless empty?(config[:http_proxy]) do
System.put_env("APPSIGNAL_HTTP_PROXY", config[:http_proxy])
end
unless empty?(config[:filter_parameters]) do
System.put_env("APPSIGNAL_FILTER_PARAMETERS", config[:filter_parameters] |> Enum.join(","))
end
System.put_env("APPSIGNAL_IGNORE_ACTIONS", config[:ignore_actions] |> Enum.join(","))
System.put_env("APPSIGNAL_IGNORE_ERRORS", config[:ignore_errors] |> Enum.join(","))
System.put_env("APPSIGNAL_SEND_PARAMS", Atom.to_string(config[:send_params]))
System.put_env("APPSIGNAL_RUNNING_IN_CONTAINER", Atom.to_string(config[:running_in_container]))
unless empty?(config[:working_dir_path]) do
System.put_env("APPSIGNAL_WORKING_DIR_PATH", config[:working_dir_path])
Expand Down

0 comments on commit 78ab765

Please sign in to comment.