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

Support non-exception error values from Oban jobs #807

Merged
merged 6 commits into from
Oct 12, 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
31 changes: 22 additions & 9 deletions lib/sentry/integrations/oban/error_reporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,40 @@ defmodule Sentry.Integrations.Oban.ErrorReporter do
:no_config
) :: :ok
def handle_event([:oban, :job, :exception], _measurements, %{job: job} = _metadata, :no_config) do
%{reason: exception, stacktrace: stacktrace} = job.unsaved_error
%{reason: reason, stacktrace: stacktrace} = job.unsaved_error

stacktrace =
case {apply(Oban.Worker, :from_string, [job.worker]), stacktrace} do
{{:ok, atom_worker}, []} -> [{atom_worker, :process, 1, []}]
_ -> stacktrace
end

_ =
Sentry.capture_exception(exception,
fingerprint_opts =
if is_exception(reason) do
[inspect(reason.__struct__), Exception.message(reason)]
else
[inspect(reason)]
end

opts =
[
stacktrace: stacktrace,
tags: %{oban_worker: job.worker, oban_queue: job.queue, oban_state: job.state},
fingerprint: [
inspect(exception.__struct__),
inspect(job.worker),
Exception.message(exception)
],
fingerprint: [inspect(job.worker)] ++ fingerprint_opts,
extra:
Map.take(job, [:args, :attempt, :id, :max_attempts, :meta, :queue, :tags, :worker]),
integration_meta: %{oban: %{job: job}}
)
]

_ =
if is_exception(reason) do
Sentry.capture_exception(reason, opts)
else
Sentry.capture_message(
"Oban job #{job.worker} errored out: %s",
opts ++ [interpolation_parameters: [inspect(reason)]]
)
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more clear to have reason instead of exception here since it may not be an exception after all :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True! Updated, thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wildly generic error message we got going on here 😄 I think it'd be really hard to debug as it's just a generic message plus there are no metadata about the job (which you have available).

This needs to be completely restructured to basically build shared options, and then use Sentry.capture_message or Sentry.capture_exception based on whether reason is an exception or not. Also, doesn't :interpolation_parameters require string parameters? If so, do inspect(reason) here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also let's have the message be something like

"Oban job #{job.worker} errored out: %s"

Btw the fingerprint option needs to be customized, if it's just reason use inspect(reason) insted of Exception.message/1


:ok
end
Expand Down
44 changes: 44 additions & 0 deletions test/sentry/integrations/oban/error_reporter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,49 @@ defmodule Sentry.Integrations.Oban.ErrorReporterTest do
assert event.tags.oban_worker == "Sentry.Integrations.Oban.ErrorReporterTest.MyWorker"
assert %{job: %Oban.Job{}} = event.integration_meta.oban
end

test "reports non-exception errors to Sentry" do
job =
%{"id" => "123", "entity" => "user", "type" => "delete"}
|> MyWorker.new()
|> Ecto.Changeset.apply_action!(:validate)
|> Map.replace!(:unsaved_error, %{
reason: :undef,
kind: :error,
stacktrace: []
})

Sentry.Test.start_collecting()

assert :ok =
ErrorReporter.handle_event(
[:oban, :job, :exception],
%{},
%{job: job},
:no_config
)

assert [event] = Sentry.Test.pop_sentry_reports()
assert %{job: %Oban.Job{}} = event.integration_meta.oban

assert event.message == %Sentry.Interfaces.Message{
formatted:
"Oban job Sentry.Integrations.Oban.ErrorReporterTest.MyWorker errored out: :undef",
message:
"Oban job Sentry.Integrations.Oban.ErrorReporterTest.MyWorker errored out: %s",
params: [":undef"]
}

assert [%Sentry.Interfaces.Thread{stacktrace: %{frames: [stacktrace]}}] = event.threads

assert stacktrace.module == MyWorker

assert stacktrace.function ==
"Sentry.Integrations.Oban.ErrorReporterTest.MyWorker.process/1"

assert event.tags.oban_queue == "default"
assert event.tags.oban_state == "available"
assert event.tags.oban_worker == "Sentry.Integrations.Oban.ErrorReporterTest.MyWorker"
end
end
end
Loading