-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set session from Logger metadata #29
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cb075aa
refactor Airbrake.Payload functions
5621f73
refactor Airbrake.Payload functions
469f0c1
rename a function
3fb38f8
extract config from config/dev.exs to config/runtime.exs
bc68e5d
extract Airbrake.Config
f11ec15
bump dialyxir to ~> 1.4
7134926
extract Airbrake.Payload.Builder with better tests
154c6c1
optionally add Logger metadata to session
fdad779
update README for new config option
b64271f
update CHANGELOG and bump to version 2.1.0
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much cleaner 👍 👍