Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 16 additions & 6 deletions lib/oban_error_reporter.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule ObanErrorReporter do
use Plausible
require Logger

def handle_event(name, measurements, metadata, _) do
Expand All @@ -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{
Expand Down Expand Up @@ -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