Skip to content

Commit

Permalink
Add :request_headers warning to Config.initialize()
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffkreeftmeijer committed Apr 25, 2018
1 parent 9ff1da1 commit 9d3dd4d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
43 changes: 43 additions & 0 deletions lib/appsignal/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ defmodule Appsignal.Config do
log: "file"
}

@suggested_request_headers [
~w(accept accept-charset accept-encoding accept-language cache-control),
~w(connection content-length path-info range referer request-method),
~w(request-uri server-name server-port server-protocol user-agent)
]

@doc """
Initializes the AppSignal config. Looks at the config default, the
Elixir-provided configuration and the various `APPSIGNAL_*`
Expand All @@ -36,13 +42,32 @@ defmodule Appsignal.Config do
|> Map.merge(app_config)
|> Map.merge(env_config)


# Config is valid when we have a push api key
config =
config
|> Map.put(:valid, !empty?(config[:push_api_key]))

Application.put_env(:appsignal, :config, config)

if config[:valid] && !config[:request_headers] && !test?() do
require Logger

Logger.warn("""
The :request_headers config was not set in the AppSignal configuration, falling back to the default list. Please explicitly list response headers to send to AppSignal in config/appsignal.exs:
request_headers: ~w(
#{multiline_suggested_request_headers()}
)
Or set the APPSIGNAL_REQUEST_HEADERS environment variable:
$ export APPSIGNAL_REQUEST_HEADERS="#{single_line_suggested_request_headers()}"
Please check https://github.com/appsignal/appsignal-elixir/pull/336 for more information on this change.
""")
end

case config[:valid] do
true ->
:ok
Expand Down Expand Up @@ -232,6 +257,24 @@ defmodule Appsignal.Config do
|> Enum.into(%{})
end

def single_line_suggested_request_headers do
@suggested_request_headers
|> List.flatten
|> Enum.join(",")
end

def multiline_suggested_request_headers do
Enum.map_join(@suggested_request_headers, "\n", fn(row) ->
" #{Enum.join(row, " ")}"
end)
end

defp test? do
Mix.env
|> Atom.to_string()
|> String.starts_with?("test")
end

# When you use Appsignal.Config you get a handy config macro which
# can be used to read the application config.
defmacro __using__(_) do
Expand Down
26 changes: 4 additions & 22 deletions lib/mix/tasks/appsignal.install.ex
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
defmodule Mix.Tasks.Appsignal.Install do
use Mix.Task
alias Appsignal.Utils.PushApiKeyValidator
alias Appsignal.{Utils.PushApiKeyValidator, Config}

@demo Application.get_env(:appsignal, :appsignal_demo, Appsignal.Demo)

@request_headers [
~w(accept accept-charset accept-encoding accept-language cache-control),
~w(connection content-length path-info range referer request-method),
~w(request-uri server-name server-port server-protocol user-agent)
]

def run([]) do
header()
IO.puts "We're missing an AppSignal Push API key and cannot continue."
Expand All @@ -22,7 +16,7 @@ defmodule Mix.Tasks.Appsignal.Install do
def run([push_api_key]) do
config = %{active: true, push_api_key: push_api_key}
Application.put_env(:appsignal, :config, config)
Appsignal.Config.initialize
Config.initialize

header()
validate_push_api_key()
Expand Down Expand Up @@ -103,7 +97,7 @@ defmodule Mix.Tasks.Appsignal.Install do
IO.puts ~s( export APPSIGNAL_APP_NAME="#{config[:name]}")
IO.puts ~s( export APPSIGNAL_APP_ENV="production")
IO.puts ~s( export APPSIGNAL_PUSH_API_KEY="#{config[:push_api_key]}")
IO.puts ~s( export APPSIGNAL_REQUEST_HEADERS="#{single_line_request_headers()}")
IO.puts ~s( export APPSIGNAL_REQUEST_HEADERS="#{Config.single_line_suggested_request_headers()}")
end

defp write_config_file(config) do
Expand Down Expand Up @@ -159,25 +153,13 @@ defmodule Mix.Tasks.Appsignal.Install do
end
end

defp single_line_request_headers do
@request_headers
|> List.flatten
|> Enum.join(",")
end

defp multiline_request_headers do
Enum.map_join(@request_headers, "\n", fn(row) ->
" #{Enum.join(row, " ")}"
end)
end

# Contents for the config/appsignal.exs file.
defp appsignal_config_file_contents(config) do
options = [
~s( name: "#{config[:name]}",),
~s( push_api_key: "#{config[:push_api_key]}",),
~s{ request_headers: ~w(},
multiline_request_headers(),
Config.multiline_suggested_request_headers(),
~s{ ),},
~s( env: Mix.env)
]
Expand Down

0 comments on commit 9d3dd4d

Please sign in to comment.