Skip to content
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

feat: auto attach #59

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
15 changes: 0 additions & 15 deletions lib/tower.ex
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,6 @@ defmodule Tower do
- [`TowerSlack`](https://hexdocs.pm/tower_slack) ([`tower_slack`](https://hex.pm/packages/tower_slack))
- and properly set the `config :tower, :reporters, [...]` configuration key

## Enabling automated exception handling

Tower.attach()

## Disabling automated exception handling

Tower.detach()

## Manual handling

If either, for whatever reason when using automated exception handling, an exception condition is
Expand Down Expand Up @@ -199,13 +191,6 @@ defmodule Tower do

# in some config/*.exs
config :tower, reporters: [MyApp.ErrorReporter]

# config/application.ex
Tower.attach()

`Tower.attach/0` will be responsible for registering the necessary handlers in your application
so that any uncaught exception, uncaught throw or abnormal process exit is handled by Tower and
passed along to the reporter.
"""

defmodule ReportEventError do
Expand Down
25 changes: 18 additions & 7 deletions lib/tower/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,23 @@ defmodule Tower.Application do

@impl true
def start(_type, _args) do
Supervisor.start_link(
[
{Task.Supervisor, name: Tower.TaskSupervisor}
],
strategy: :one_for_one,
name: Tower.Supervisor
)
with {:ok, pid} <-
Supervisor.start_link(
[
{Task.Supervisor, name: Tower.TaskSupervisor}
],
strategy: :one_for_one,
name: Tower.Supervisor
),
:ok = Tower.attach() do
{:ok, pid}
end
end

@impl true
def stop(_arg) do
Tower.detach()

:ok
end
end
4 changes: 0 additions & 4 deletions lib/tower/ephemeral_reporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ defmodule Tower.EphemeralReporter do
iex> Tower.EphemeralReporter.events()
[]
iex> Application.put_env(:tower, :reporters, [Tower.EphemeralReporter])
iex> Tower.attach()
:ok
iex> spawn(fn -> 1 / 0 end)
iex> Process.sleep(200)
:ok
Expand All @@ -20,8 +18,6 @@ defmodule Tower.EphemeralReporter do
:error
iex> event.reason
%ArithmeticError{message: "bad argument in arithmetic expression"}
iex> Tower.detach()
:ok
iex> Tower.EphemeralReporter.stop(pid)
:ok
"""
Expand Down
6 changes: 1 addition & 5 deletions test/tower/tower_oban_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ defmodule TowerObanTest do

Ecto.Migrator.up(TestApp.Repo, 0, TestApp.Repo.Migrations.AddOban)

Tower.attach()

on_exit(fn ->
Tower.detach()
end)
:ok
end

@tag capture_log: true
Expand Down
5 changes: 1 addition & 4 deletions test/tower/tower_plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ defmodule TowerPlugTest do

setup do
start_link_supervised!(Tower.EphemeralReporter)
Tower.attach()

on_exit(fn ->
Tower.detach()
end)
:ok
end

test "reports arithmetic error during plug dispatch with Plug.Cowboy" do
Expand Down
5 changes: 1 addition & 4 deletions test/tower_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@

setup do
start_reporter()
Tower.attach()

on_exit(fn ->
Tower.detach()
end)
:ok
end

test "starts with 0 exceptions" do
Expand All @@ -22,7 +19,7 @@
test "reports arithmetic error" do
capture_log(fn ->
in_unlinked_process(fn ->
1 / 0

Check warning on line 22 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the call to //2 will fail with ArithmeticError

Check warning on line 22 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError

Check warning on line 22 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the call to //2 will fail with ArithmeticError

Check warning on line 22 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError
end)
end)

Expand Down Expand Up @@ -311,7 +308,7 @@
test "reports Exception manually" do
in_unlinked_process(fn ->
try do
1 / 0

Check warning on line 311 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the call to //2 will fail with ArithmeticError

Check warning on line 311 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError

Check warning on line 311 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the call to //2 will fail with ArithmeticError

Check warning on line 311 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError
catch
kind, reason ->
Tower.handle_caught(kind, reason, __STACKTRACE__)
Expand All @@ -337,7 +334,7 @@
test "reports Exception manually (shorthand)" do
in_unlinked_process(fn ->
try do
1 / 0

Check warning on line 337 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the call to //2 will fail with ArithmeticError

Check warning on line 337 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError

Check warning on line 337 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the call to //2 will fail with ArithmeticError

Check warning on line 337 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError
rescue
e ->
Tower.handle_exception(e, __STACKTRACE__)
Expand Down Expand Up @@ -515,7 +512,7 @@

capture_log(fn ->
in_unlinked_process(fn ->
1 / 0

Check warning on line 515 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the call to //2 will fail with ArithmeticError

Check warning on line 515 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError

Check warning on line 515 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the call to //2 will fail with ArithmeticError

Check warning on line 515 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError
end)
end)

Expand Down
Loading