Skip to content

Fix reporting errors from Oban #70

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

Merged
merged 4 commits into from
Aug 28, 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
3 changes: 2 additions & 1 deletion lib/error_tracker/integrations/oban.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ defmodule ErrorTracker.Integrations.Oban do

def handle_event([:oban, :job, :exception], _measurements, metadata, :no_config) do
%{reason: exception, stacktrace: stacktrace} = metadata
state = Map.get(metadata, :state, :failure)

ErrorTracker.report(exception, stacktrace)
ErrorTracker.report(exception, stacktrace, %{state: state})
end
end
23 changes: 21 additions & 2 deletions lib/error_tracker/schemas/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,21 @@ defmodule ErrorTracker.Error do
@doc false
def new(kind, reason, stacktrace = %ErrorTracker.Stacktrace{}) do
source = ErrorTracker.Stacktrace.source(stacktrace)
source_line = if source.file, do: "#{source.file}:#{source.line}", else: "nofile"

{source_line, source_function} =
if source do
source_line = if source.line, do: "#{source.file}:#{source.line}", else: "nofile"
source_function = "#{source.module}.#{source.function}/#{source.arity}"

{source_line, source_function}
else
{"-", "-"}
end

params = [
kind: to_string(kind),
source_line: source_line,
source_function: "#{source.module}.#{source.function}/#{source.arity}"
source_function: source_function
]

fingerprint = :crypto.hash(:sha256, params |> Keyword.values() |> Enum.join())
Expand All @@ -46,4 +55,14 @@ defmodule ErrorTracker.Error do
|> Ecto.Changeset.put_change(:last_occurrence_at, DateTime.utc_now())
|> Ecto.Changeset.apply_action(:new)
end

@doc """
Returns if the Error has information of the source or not.

Errors usually have information about in which line and function occurred, but
in some cases (like an Oban job ending with `{:error, any()}`) we cannot get
that information and no source is stored.
"""
def has_source_info?(%__MODULE__{source_function: "-", source_line: "-"}), do: false
def has_source_info?(%__MODULE__{}), do: true
end
2 changes: 1 addition & 1 deletion lib/error_tracker/web/live/dashboard.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<p class="whitespace-nowrap text-ellipsis w-full overflow-hidden">
(<%= sanitize_module(error.kind) %>) <%= error.reason %>
</p>
<p class="font-normal text-gray-400">
<p :if={ErrorTracker.Error.has_source_info?(error)} class="font-normal text-gray-400">
<%= sanitize_module(error.source_function) %>
<br />
<%= error.source_line %>
Expand Down
4 changes: 2 additions & 2 deletions lib/error_tracker/web/live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
<pre class="overflow-auto p-4 rounded-lg bg-gray-300/10 border border-gray-900"><%= @occurrence.reason %></pre>
</.section>

<.section title="Source">
<.section :if={ErrorTracker.Error.has_source_info?(@error)} title="Source">
<pre class="overflow-auto text-sm p-4 rounded-lg bg-gray-300/10 border border-gray-900">
<%= sanitize_module(@error.source_function) %>
<%= @error.source_line %></pre>
</.section>

<.section title="Stacktrace">
<.section :if={@occurrence.stacktrace.lines != []} title="Stacktrace">
<div class="p-4 bg-gray-300/10 border border-gray-900 rounded-lg">
<div class="w-full mb-4">
<label class="flex justify-end">
Expand Down