1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-21 23:44:30 +00:00

Add wrapper to Sentry to not load it when not configured

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-06-27 13:15:24 +02:00
parent 3ed25bab81
commit 7ec6f158ec
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
13 changed files with 102 additions and 24 deletions

View file

@ -113,7 +113,7 @@ config :mobilizon, Mobilizon.Web.Email.Mailer,
# Configures Elixir's Logger
config :logger, :console,
backends: [:console, Sentry.LoggerBackend],
backends: [:console],
format: "$time $metadata[$level] $message\n",
metadata: [:request_id]

View file

@ -9,6 +9,7 @@ defmodule Mobilizon.Federation.ActivityPub.Fetcher do
alias Mobilizon.Federation.HTTPSignatures.Signature
alias Mobilizon.Federation.ActivityPub.{Relay, Transmogrifier}
alias Mobilizon.Federation.ActivityStream.Converter.Actor, as: ActorConverter
alias Mobilizon.Service.ErrorReporting.Sentry
alias Mobilizon.Service.HTTP.ActivityPub, as: ActivityPubClient
import Mobilizon.Federation.ActivityPub.Utils,

View file

@ -8,6 +8,7 @@ defmodule Mobilizon.Federation.ActivityPub.Refresher do
alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
alias Mobilizon.Federation.ActivityPub.{Fetcher, Relay, Transmogrifier, Utils}
alias Mobilizon.Service.ErrorReporting.Sentry
require Logger
@doc """

View file

@ -21,6 +21,7 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
alias Mobilizon.Federation.ActivityPub.Types.Ownable
alias Mobilizon.Federation.ActivityStream.{Converter, Convertible}
alias Mobilizon.Service.ErrorReporting.Sentry
alias Mobilizon.Service.Workers.Background
alias Mobilizon.Tombstone
alias Mobilizon.Web.Email.Participation

View file

@ -13,6 +13,7 @@ defmodule Mobilizon.Federation.HTTPSignatures.Signature do
alias Mobilizon.Actors.Actor
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
alias Mobilizon.Service.ErrorReporting.Sentry
require Logger

View file

@ -16,7 +16,7 @@ defmodule Mobilizon do
alias Mobilizon.{Config, Storage, Web}
alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Service.{ErrorPage, ErrorReporter}
alias Mobilizon.Service.{ErrorPage, ErrorReporting}
alias Mobilizon.Service.Export.{Feed, ICalendar}
@name Mix.Project.config()[:name]
@ -66,13 +66,14 @@ defmodule Mobilizon do
] ++
task_children(@env)
Logger.add_backend(Sentry.LoggerBackend)
ErrorReporting.configure()
:ok = Oban.Telemetry.attach_default_logger()
:telemetry.attach_many(
"oban-errors",
[[:oban, :job, :exception], [:oban, :circuit, :trip]],
&ErrorReporter.handle_event/4,
&ErrorReporting.handle_event/4,
%{}
)

View file

@ -16,6 +16,7 @@ defmodule Mobilizon.Actors do
alias Mobilizon.Events.FeedToken
alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Medias.File
alias Mobilizon.Service.ErrorReporting.Sentry
alias Mobilizon.Service.Workers
alias Mobilizon.Storage.{Page, Repo}
alias Mobilizon.Users

View file

@ -1,17 +0,0 @@
defmodule Mobilizon.Service.ErrorReporter do
@moduledoc """
Module to delegate all exceptions to Sentry
"""
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
def handle_event([:oban, :circuit, :trip], _measure, meta, _) do
Sentry.capture_exception(meta.error, stacktrace: meta.stacktrace, extra: meta)
end
end

View 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

View 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

View file

@ -6,6 +6,7 @@ defmodule Mobilizon.Web.Auth.Context do
import Plug.Conn
alias Mobilizon.Service.ErrorReporting.Sentry, as: SentryAdapter
alias Mobilizon.Users.User
def init(opts) do
@ -24,7 +25,10 @@ defmodule Mobilizon.Web.Auth.Context do
context =
case Guardian.Plug.current_resource(conn) do
%User{id: user_id, email: user_email} = user ->
Sentry.Context.set_user_context(%{id: user_id, name: user_email})
if SentryAdapter.enabled?() do
Sentry.Context.set_user_context(%{id: user_id, name: user_email})
end
Map.put(context, :current_user, user)
nil ->

View file

@ -3,6 +3,7 @@ defmodule Mobilizon.Web.Email.Mailer do
Mobilizon Mailer.
"""
use Bamboo.Mailer, otp_app: :mobilizon
alias Mobilizon.Service.ErrorReporting.Sentry
def send_email_later(email) do
Mobilizon.Web.Email.Mailer.deliver_later!(email)

View file

@ -2,7 +2,10 @@ defmodule Mobilizon.Web.Endpoint do
@moduledoc """
Endpoint for Mobilizon app
"""
if Application.fetch_env!(:mobilizon, :env) !== :test do
alias Mobilizon.Service.ErrorReporting.Sentry, as: SentryAdapter
if Application.fetch_env!(:mobilizon, :env) !== :test &&
SentryAdapter.enabled?() do
use Sentry.PlugCapture
end
@ -92,7 +95,8 @@ defmodule Mobilizon.Web.Endpoint do
String.replace_leading(url(), "http", "ws")
end
if Application.fetch_env!(:mobilizon, :env) !== :test do
if Application.fetch_env!(:mobilizon, :env) !== :test &&
SentryAdapter.enabled?() do
plug(Sentry.PlugContext)
end
end