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 1 commit
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
28 changes: 16 additions & 12 deletions lib/sentry/integrations/oban/error_reporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,22 @@ defmodule Sentry.Integrations.Oban.ErrorReporter do
end

_ =
Sentry.capture_exception(exception,
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)
],
extra:
Map.take(job, [:args, :attempt, :id, :max_attempts, :meta, :queue, :tags, :worker]),
integration_meta: %{oban: %{job: job}}
)
if is_struct(exception) do

Choose a reason for hiding this comment

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

You may want more specifically is_exception here, rather than just is_struct, because I think Exception.message/1 below won't work on just any general struct.

This may not really matter in practice, though, because I think the errors will probably never end up being a struct other than an Exception.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Probably not but good to use the right tool for the job. Thanks and updated!

Sentry.capture_exception(exception,
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)
],
extra:
Map.take(job, [:args, :attempt, :id, :max_attempts, :meta, :queue, :tags, :worker]),
integration_meta: %{oban: %{job: job}}
)
else
Sentry.capture_message("Error with %s", interpolation_parameters: [exception])
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
30 changes: 30 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,35 @@ 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 event.message == %Sentry.Interfaces.Message{
message: "Error with %s",
params: [:undef],
formatted: "Error with undef"
}
end
end
end
Loading