Add wrapper to Sentry to not load it when not configured
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
35
lib/service/error_reporting/error_reporting.ex
Normal file
35
lib/service/error_reporting/error_reporting.ex
Normal file
@@ -0,0 +1,35 @@
|
||||
defmodule Mobilizon.Service.ErrorReporting do
|
||||
@moduledoc """
|
||||
Mpdule to load and configure error reporting adapters
|
||||
"""
|
||||
|
||||
@callback enabled? :: boolean()
|
||||
|
||||
@callback configure :: any()
|
||||
|
||||
@callback handle_event(list(atom()), map(), map(), any()) :: any()
|
||||
|
||||
@spec adapter :: module() | nil
|
||||
def adapter do
|
||||
adapter = Mobilizon.Config.get([__MODULE__, :adapter])
|
||||
if adapter && adapter.enabled?(), do: adapter, else: nil
|
||||
end
|
||||
|
||||
@spec handle_event(list(atom()), map(), map(), any()) :: any()
|
||||
def handle_event(event_name, event_measurements, event_metadata, handler_config) do
|
||||
adapter = adapter()
|
||||
|
||||
if adapter do
|
||||
adapter.handle_event(event_name, event_measurements, event_metadata, handler_config)
|
||||
end
|
||||
end
|
||||
|
||||
@spec configure :: any()
|
||||
def configure do
|
||||
adapter = adapter()
|
||||
|
||||
if adapter do
|
||||
adapter.configure()
|
||||
end
|
||||
end
|
||||
end
|
||||
45
lib/service/error_reporting/sentry.ex
Normal file
45
lib/service/error_reporting/sentry.ex
Normal file
@@ -0,0 +1,45 @@
|
||||
defmodule Mobilizon.Service.ErrorReporting.Sentry do
|
||||
@moduledoc """
|
||||
Sentry adapter for error reporting
|
||||
"""
|
||||
|
||||
alias Mobilizon.Service.ErrorReporting
|
||||
@behaviour ErrorReporting
|
||||
|
||||
@impl ErrorReporting
|
||||
def enabled? do
|
||||
!is_nil(Application.get_env(:sentry, :dsn))
|
||||
end
|
||||
|
||||
@impl ErrorReporting
|
||||
def configure do
|
||||
Logger.add_backend(Sentry.LoggerBackend)
|
||||
end
|
||||
|
||||
def capture_message(message, opts \\ []) when is_binary(message) do
|
||||
if enabled?() do
|
||||
Sentry.capture_message(message, opts)
|
||||
end
|
||||
end
|
||||
|
||||
def capture_exception(exception, opts \\ []) do
|
||||
if enabled?() do
|
||||
Sentry.capture_exception(exception, opts)
|
||||
end
|
||||
end
|
||||
|
||||
@impl ErrorReporting
|
||||
def handle_event([:oban, :job, :exception], measure, %{job: job} = meta, _) do
|
||||
extra =
|
||||
job
|
||||
|> Map.take([:id, :args, :meta, :queue, :worker])
|
||||
|> Map.merge(measure)
|
||||
|
||||
Sentry.capture_exception(meta.error, stacktrace: meta.stacktrace, extra: extra)
|
||||
end
|
||||
|
||||
@impl ErrorReporting
|
||||
def handle_event([:oban, :circuit, :trip], _measure, meta, _) do
|
||||
Sentry.capture_exception(meta.error, stacktrace: meta.stacktrace, extra: meta)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user