Skip to content

Commit

Permalink
feat: automatically Tower.attach
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Oct 4, 2024
1 parent 1464380 commit ded7e08
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 44 deletions.
35 changes: 12 additions & 23 deletions lib/tower.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,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 @@ -207,13 +199,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 Expand Up @@ -278,11 +263,13 @@ defmodule Tower do
Note that `Tower.attach/0` is not a precondition for `Tower` `handle_*` functions to work
properly and inform reporters. They are independent.
"""
@spec attach() :: :ok
@spec attach() :: :ok | {:error, reason :: term()}
def attach do
:ok = Tower.LoggerHandler.attach()
:ok = Tower.BanditExceptionHandler.attach()
:ok = Tower.ObanExceptionHandler.attach()
with :ok <- Tower.LoggerHandler.attach(),
:ok <- Tower.BanditExceptionHandler.attach(),
:ok <- Tower.ObanExceptionHandler.attach() do
:ok
end
end

@doc """
Expand All @@ -292,11 +279,13 @@ defmodule Tower do
You can still manually call `Tower` `handle_*` functions and reporters will be informed about
those events.
"""
@spec detach() :: :ok
@spec detach() :: :ok | {:error, reason :: term()}
def detach do
:ok = Tower.LoggerHandler.detach()
:ok = Tower.BanditExceptionHandler.detach()
:ok = Tower.ObanExceptionHandler.detach()
with :ok <- Tower.LoggerHandler.detach(),
:ok <- Tower.BanditExceptionHandler.detach(),
:ok <- Tower.ObanExceptionHandler.detach() do
:ok
end
end

@doc """
Expand Down
25 changes: 17 additions & 8 deletions lib/tower/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ defmodule Tower.Application do

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

@impl true
def stop(_state) do
Tower.detach()
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 @@ -13,8 +13,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 @@ -23,8 +21,6 @@ defmodule Tower.EphemeralReporter do
:error
iex> event.reason
%ArithmeticError{message: "bad argument in arithmetic expression"}
iex> Tower.detach()
:ok
"""
@behaviour Tower.Reporter

Expand Down
3 changes: 0 additions & 3 deletions test/tower/tower_oban_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ defmodule TowerObanTest do
Ecto.Migrator.up(TestApp.Repo, 0, TestApp.Repo.Migrations.AddOban)
end)

Tower.attach()

on_exit(fn ->
Tower.detach()
Tower.EphemeralReporter.reset()
end)
end
Expand Down
3 changes: 0 additions & 3 deletions test/tower/tower_plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ defmodule TowerPlugTest do
import ExUnit.CaptureLog, only: [capture_log: 1]

setup do
Tower.attach()

on_exit(fn ->
Tower.detach()
Tower.EphemeralReporter.reset()
end)
end
Expand Down
3 changes: 0 additions & 3 deletions test/tower_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ defmodule TowerTest do
import ExUnit.CaptureLog, only: [capture_log: 1]

setup do
Tower.attach()

on_exit(fn ->
Tower.detach()
Tower.EphemeralReporter.reset()
end)
end
Expand Down

0 comments on commit ded7e08

Please sign in to comment.