Skip to content

Flag to globally disable error tracking #82

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

Merged
merged 4 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion guides/Getting Started.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ ErrorTracker needs a few configuration options to work. This configuration shoul
```elixir
config :error_tracker,
repo: MyApp.Repo,
otp_app: :my_app
otp_app: :my_app,
enabled: true
```

The `:repo` option specifies the repository that will be used by ErrorTracker. You can use your regular application repository or a different one if you prefer to keep errors in a different database.

The `:otp_app` option specifies your application name. When an error occurs, ErrorTracker will use this information to understand which parts of the stack trace belong to your application and which parts belong to third-party dependencies. This allows you to filter in-app vs third-party frames when viewing errors.

The `:enabled` option (defaults to `true` if not present) allows to disable the ErrorTracker on certain environments. This is useful to avoid filling your dev database with errors, for example.

## Setting up the database

Since ErrorTracker stores errors in the database you must create a database migration to add the required tables:
Expand Down
16 changes: 13 additions & 3 deletions lib/error_tracker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ defmodule ErrorTracker do
@doc """
Report an exception to be stored.

Returns the occurrence stored or `:noop` if the ErrorTracker is disabled by
configuration the exception has not been stored.

Aside from the exception, it is expected to receive the stack trace and,
optionally, a context map which will be merged with the current process
context.
Expand Down Expand Up @@ -111,9 +114,12 @@ defmodule ErrorTracker do

context = Map.merge(get_context(), given_context)

{_error, occurrence} = upsert_error!(error, stacktrace, context, reason)

occurrence
if enabled?() do
{_error, occurrence} = upsert_error!(error, stacktrace, context, reason)
occurrence
else
:noop
end
end

@doc """
Expand Down Expand Up @@ -185,6 +191,10 @@ defmodule ErrorTracker do
Process.get(:error_tracker_context, %{})
end

defp enabled? do
!!Application.get_env(:error_tracker, :enabled, true)
end

defp normalize_exception(%struct{} = ex, _stacktrace) when is_exception(ex) do
{to_string(struct), Exception.message(ex)}
end
Expand Down
27 changes: 27 additions & 0 deletions test/error_tracker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ defmodule ErrorTrackerTest do
@relative_file_path Path.relative_to(__ENV__.file, File.cwd!())

describe inspect(&ErrorTracker.report/3) do
setup context do
if Map.has_key?(context, :enabled) do
Application.put_env(:error_tracker, :enabled, context[:enabled])
# Ensure that the application env is restored after each test
on_exit(fn -> Application.delete_env(:error_tracker, :enabled) end)
end

[]
end

test "reports exceptions" do
%Occurrence{error: error = %Error{}} =
report_error(fn -> raise "This is a test" end)
Expand Down Expand Up @@ -72,6 +82,23 @@ defmodule ErrorTrackerTest do

assert %Occurrence{} = report_error(fn -> raise "test" end, invalid_context)
end

test "without enabled flag it works as expected" do
# Ensure no value is set
Application.delete_env(:error_tracker, :enabled)

assert %Occurrence{} = report_error(fn -> raise "Sample error" end)
end

@tag enabled: true
test "with enabled flag to true it works as expected" do
assert %Occurrence{} = report_error(fn -> raise "Sample error" end)
end

@tag enabled: false
test "with enabled flag to false it does not store the exception" do
assert report_error(fn -> raise "Sample error" end) == :noop
end
end

describe inspect(&ErrorTracker.resolve/1) do
Expand Down
5 changes: 4 additions & 1 deletion test/support/case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ defmodule ErrorTracker.Test.Case do
ErrorTracker.report({kind, reason}, __STACKTRACE__, context)
end

repo().preload(occurrence, :error)
case occurrence do
%ErrorTracker.Occurrence{} -> repo().preload(occurrence, :error)
other -> other
end
end

@doc """
Expand Down