Skip to content

Commit

Permalink
feat: auto attach
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Sep 2, 2024
1 parent 9ed3c45 commit 9d60036
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 39 deletions.
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 @@ defmodule TowerTest do

setup do
start_reporter()
Tower.attach()

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

test "starts with 0 exceptions" do
Expand Down

0 comments on commit 9d60036

Please sign in to comment.