Skip to content

Commit

Permalink
Log Oban errors (and still report them to Sentry) (#4657)
Browse files Browse the repository at this point in the history
* log oban errors in CE

* update changelog

* not just ce

* add comment
  • Loading branch information
ruslandoga authored Oct 8, 2024
1 parent 5ad743c commit f28a1b8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
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

0 comments on commit f28a1b8

Please sign in to comment.