From ded7e08bb49eaabba9dd4191c153a3387ac57dd7 Mon Sep 17 00:00:00 2001 From: Gonzalo <456459+grzuy@users.noreply.github.com> Date: Mon, 2 Sep 2024 16:01:41 -0300 Subject: [PATCH] feat: automatically Tower.attach --- lib/tower.ex | 35 +++++++++++---------------------- lib/tower/application.ex | 25 +++++++++++++++-------- lib/tower/ephemeral_reporter.ex | 4 ---- test/tower/tower_oban_test.exs | 3 --- test/tower/tower_plug_test.exs | 3 --- test/tower_test.exs | 3 --- 6 files changed, 29 insertions(+), 44 deletions(-) diff --git a/lib/tower.ex b/lib/tower.ex index a7bdd67..8a93e8f 100644 --- a/lib/tower.ex +++ b/lib/tower.ex @@ -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 @@ -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 @@ -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 """ @@ -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 """ diff --git a/lib/tower/application.ex b/lib/tower/application.ex index 67514ea..bb1c80c 100644 --- a/lib/tower/application.ex +++ b/lib/tower/application.ex @@ -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 diff --git a/lib/tower/ephemeral_reporter.ex b/lib/tower/ephemeral_reporter.ex index cb2fa8a..cfab013 100644 --- a/lib/tower/ephemeral_reporter.ex +++ b/lib/tower/ephemeral_reporter.ex @@ -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 @@ -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 diff --git a/test/tower/tower_oban_test.exs b/test/tower/tower_oban_test.exs index c47cae7..88c93c1 100644 --- a/test/tower/tower_oban_test.exs +++ b/test/tower/tower_oban_test.exs @@ -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 diff --git a/test/tower/tower_plug_test.exs b/test/tower/tower_plug_test.exs index 9427214..6fe18f1 100644 --- a/test/tower/tower_plug_test.exs +++ b/test/tower/tower_plug_test.exs @@ -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 diff --git a/test/tower_test.exs b/test/tower_test.exs index 66f9e16..3bbb71e 100644 --- a/test/tower_test.exs +++ b/test/tower_test.exs @@ -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