Log Oban errors (and still report them to Sentry) (#4657)

* log oban errors in CE

* update changelog

* not just ce

* add comment
This commit is contained in:
ruslandoga
2024-10-08 17:26:35 +07:00
committed by GitHub
parent 5ad743c8d3
commit f28a1b8086
2 changed files with 17 additions and 6 deletions

View File

@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
- Add ability to review and revoke particular logged in user sessions
- Add ability to change password from user settings screen
- Add error logs for background jobs plausible/analytics#4657
### Removed

View File

@ -1,4 +1,5 @@
defmodule ObanErrorReporter do
use Plausible
require Logger
def handle_event(name, measurements, metadata, _) do
@ -21,20 +22,17 @@ defmodule ObanErrorReporter do
|> Map.merge(measure)
on_job_exception(job)
Sentry.capture_exception(meta.reason, stacktrace: meta.stacktrace, extra: extra)
capture_error(meta, extra)
end
defp handle_event([:oban, :notifier, :exception], _timing, meta) do
extra = Map.take(meta, ~w(channel payload)a)
Sentry.capture_exception(meta.reason, stacktrace: meta.stacktrace, extra: extra)
capture_error(meta, extra)
end
defp handle_event([:oban, :plugin, :exception], _timing, meta) do
extra = Map.take(meta, ~w(plugin)a)
Sentry.capture_exception(meta.reason, stacktrace: meta.stacktrace, extra: extra)
capture_error(meta, extra)
end
defp on_job_exception(%Oban.Job{
@ -65,4 +63,16 @@ defmodule ObanErrorReporter do
end
defp on_job_exception(_job), do: :ignore
# Logs the error and sends it to Sentry
defp capture_error(meta, extra) do
Logger.error(
# this message is ignored by Sentry
"Background job (#{inspect(extra)}) failed:\n\n " <>
Exception.format(:error, meta.reason, meta.stacktrace),
# Sentry report is built entirely from crash_reason
crash_reason: {meta.reason, meta.stacktrace},
sentry: %{extra: extra}
)
end
end