Skip to content

Commit f28a1b8

Browse files
authored
Log Oban errors (and still report them to Sentry) (#4657)
* log oban errors in CE * update changelog * not just ce * add comment
1 parent 5ad743c commit f28a1b8

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
77

88
- Add ability to review and revoke particular logged in user sessions
99
- Add ability to change password from user settings screen
10+
- Add error logs for background jobs plausible/analytics#4657
1011

1112
### Removed
1213

lib/oban_error_reporter.ex

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule ObanErrorReporter do
2+
use Plausible
23
require Logger
34

45
def handle_event(name, measurements, metadata, _) do
@@ -21,20 +22,17 @@ defmodule ObanErrorReporter do
2122
|> Map.merge(measure)
2223

2324
on_job_exception(job)
24-
25-
Sentry.capture_exception(meta.reason, stacktrace: meta.stacktrace, extra: extra)
25+
capture_error(meta, extra)
2626
end
2727

2828
defp handle_event([:oban, :notifier, :exception], _timing, meta) do
2929
extra = Map.take(meta, ~w(channel payload)a)
30-
31-
Sentry.capture_exception(meta.reason, stacktrace: meta.stacktrace, extra: extra)
30+
capture_error(meta, extra)
3231
end
3332

3433
defp handle_event([:oban, :plugin, :exception], _timing, meta) do
3534
extra = Map.take(meta, ~w(plugin)a)
36-
37-
Sentry.capture_exception(meta.reason, stacktrace: meta.stacktrace, extra: extra)
35+
capture_error(meta, extra)
3836
end
3937

4038
defp on_job_exception(%Oban.Job{
@@ -65,4 +63,16 @@ defmodule ObanErrorReporter do
6563
end
6664

6765
defp on_job_exception(_job), do: :ignore
66+
67+
# Logs the error and sends it to Sentry
68+
defp capture_error(meta, extra) do
69+
Logger.error(
70+
# this message is ignored by Sentry
71+
"Background job (#{inspect(extra)}) failed:\n\n " <>
72+
Exception.format(:error, meta.reason, meta.stacktrace),
73+
# Sentry report is built entirely from crash_reason
74+
crash_reason: {meta.reason, meta.stacktrace},
75+
sentry: %{extra: extra}
76+
)
77+
end
6878
end

0 commit comments

Comments
 (0)