-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from CityBaseInc/set-session-from-logger-metadata
Set session from Logger metadata
- Loading branch information
Showing
15 changed files
with
527 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
import Config | ||
|
||
# These settings can be used on the iex console. | ||
# More are set in `config/runtime.exs`. | ||
config :airbrake_client, | ||
api_key: {:system, "AIRBRAKE_API_KEY"}, | ||
project_id: {:system, "AIRBRAKE_PROJECT_ID"}, | ||
host: {:system, "AIRBRAKE_HOST", "https://api.airbrake.io"}, | ||
session: :include_logger_metadata, | ||
private: [http_adapter: HTTPoison] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Config | ||
|
||
case config_env() do | ||
:dev -> | ||
config :airbrake_client, | ||
api_key: System.get_env("AIRBRAKE_API_KEY"), | ||
project_id: System.get_env("AIRBRAKE_PROJECT_ID"), | ||
host: System.get_env("AIRBRAKE_HOST", "https://api.airbrake.io") | ||
|
||
:test -> | ||
nil | ||
|
||
:prod -> | ||
nil | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
defmodule Airbrake.Config do | ||
@moduledoc false | ||
|
||
defmodule Behaviour do | ||
@moduledoc false | ||
|
||
@callback get(atom()) :: any() | ||
|
||
@callback get(atom(), any()) :: any() | ||
|
||
@callback env :: String.t() | ||
|
||
@callback hostname :: String.t() | ||
end | ||
|
||
@behaviour Airbrake.Config.Behaviour | ||
|
||
# Gets a value from the `:airbrake_client` config. | ||
@impl Airbrake.Config.Behaviour | ||
def get(key, default \\ nil) do | ||
:airbrake_client | ||
|> Application.get_env(key, default) | ||
|> resolve() | ||
end | ||
|
||
# Returns the name of the environment. | ||
@impl Airbrake.Config.Behaviour | ||
def env do | ||
case Application.get_env(:airbrake_client, :environment) do | ||
nil -> hostname() | ||
{:system, var} -> System.get_env(var, hostname()) | ||
atom_env when is_atom(atom_env) -> to_string(atom_env) | ||
str_env when is_binary(str_env) -> str_env | ||
fun_env when is_function(fun_env) -> fun_env.() | ||
end | ||
end | ||
|
||
# Returns a hostname. | ||
@impl Airbrake.Config.Behaviour | ||
def hostname do | ||
System.get_env("HOST") || to_string(elem(:inet.gethostname(), 1)) | ||
end | ||
|
||
defp resolve({:system, key, default}), do: System.get_env(key) || default | ||
defp resolve({:system, key}), do: System.get_env(key) | ||
defp resolve(value), do: value | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
defmodule Airbrake.Payload.Builder do | ||
@moduledoc false | ||
|
||
alias Airbrake.Payload.Backtrace | ||
alias Airbrake.Utils | ||
|
||
def build_error(exception, stacktrace) do | ||
%{ | ||
type: exception[:type], | ||
message: exception[:message], | ||
backtrace: Backtrace.from_stacktrace(stacktrace) | ||
} | ||
end | ||
|
||
def build(:context, opts) do | ||
config = get_config(opts) | ||
|
||
Map.merge( | ||
%{environment: config.env(), hostname: config.hostname()}, | ||
opts |> Keyword.get(:context, %{}) |> Enum.into(%{}) | ||
) | ||
end | ||
|
||
def build(:environment, opts) do | ||
environment = | ||
Keyword.get_lazy(opts, :environment, fn -> | ||
Keyword.get(opts, :env) | ||
end) | ||
|
||
case environment do | ||
nil -> nil | ||
env -> env |> Enum.into(%{}) |> filter_environment(opts) | ||
end | ||
end | ||
|
||
def build(:params, opts) do | ||
case Keyword.get(opts, :params) do | ||
nil -> nil | ||
params -> params |> Enum.into(%{}) |> filter_parameters(opts) | ||
end | ||
end | ||
|
||
def build(:session, opts) do | ||
config = get_config(opts) | ||
|
||
logger_metadata = | ||
if config.get(:session) == :include_logger_metadata, | ||
do: Keyword.get(opts, :logger_metadata, []), | ||
else: [] | ||
|
||
opts_session = opts |> Keyword.get(:session, %{}) |> Enum.into(%{}) | ||
full_session = logger_metadata |> Enum.into(%{}) |> Map.merge(opts_session) | ||
|
||
if full_session == %{}, | ||
do: nil, | ||
else: full_session | ||
end | ||
|
||
def filter_parameters(params, opts) do | ||
filter_parameters = get_config(opts).get(:filter_parameters, []) | ||
|
||
Utils.filter(params, filter_parameters) | ||
end | ||
|
||
def filter_environment(nil) do | ||
nil | ||
end | ||
|
||
def filter_environment(environment, opts) do | ||
filter_headers = get_config(opts).get(:filter_headers, []) | ||
|
||
cond do | ||
Map.has_key?(environment, "headers") -> | ||
Map.update!(environment, "headers", &Utils.filter(&1, filter_headers)) | ||
|
||
Map.has_key?(environment, :headers) -> | ||
Map.update!(environment, :headers, &Utils.filter(&1, filter_headers)) | ||
|
||
true -> | ||
environment | ||
end | ||
end | ||
|
||
defp get_config(opts), | ||
do: Keyword.get(opts, :config, Airbrake.Config) | ||
end |
Oops, something went wrong.