From 36ca70223e5cb7d9eae4d47e9eb96e8adaa096c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Tue, 27 Aug 2024 20:41:10 +0200 Subject: [PATCH 1/4] Fix Oban reporting and all state field. There was an error when an Oban worker returned an `{:error, reason}` tuple because Oban does not return an stacktrace (makes sense, as it is not an exception by itself). Our code was not prepared for that use case. I modified it to handle it and circunvent the needed for source file and source function information. As an extra feature I have also added the reporting of the `state` of the job (which is mostly ever `failure` for us). --- lib/error_tracker/integrations/oban.ex | 3 ++- lib/error_tracker/schemas/error.ex | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/error_tracker/integrations/oban.ex b/lib/error_tracker/integrations/oban.ex index 77acd5a..be637d5 100644 --- a/lib/error_tracker/integrations/oban.ex +++ b/lib/error_tracker/integrations/oban.ex @@ -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 diff --git a/lib/error_tracker/schemas/error.ex b/lib/error_tracker/schemas/error.ex index 26265d4..aa13a87 100644 --- a/lib/error_tracker/schemas/error.ex +++ b/lib/error_tracker/schemas/error.ex @@ -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()) From ed69910e9dd9e52565731067387b17d341be7cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Tue, 27 Aug 2024 20:43:42 +0200 Subject: [PATCH 2/4] Avoid showing empty stacktrace and source info on error show --- lib/error_tracker/web/live/show.ex | 3 +++ lib/error_tracker/web/live/show.html.heex | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/error_tracker/web/live/show.ex b/lib/error_tracker/web/live/show.ex index 0982219..fd8cefd 100644 --- a/lib/error_tracker/web/live/show.ex +++ b/lib/error_tracker/web/live/show.ex @@ -121,4 +121,7 @@ defmodule ErrorTracker.Web.Live.Show do |> limit(^num_results) |> Repo.all() end + + defp error_has_source_info?(%Error{source_function: "-", source_line: "-"}), do: false + defp error_has_source_info?(%Error{}), do: true end diff --git a/lib/error_tracker/web/live/show.html.heex b/lib/error_tracker/web/live/show.html.heex index 795b7fb..970ef41 100644 --- a/lib/error_tracker/web/live/show.html.heex +++ b/lib/error_tracker/web/live/show.html.heex @@ -19,13 +19,13 @@
<%= @occurrence.reason %>
- <.section title="Source"> + <.section :if={error_has_source_info?(@error)} title="Source">
         <%= sanitize_module(@error.source_function) %>
         <%= @error.source_line %>
- <.section title="Stacktrace"> + <.section :if={@occurrence.stacktrace.lines != []} title="Stacktrace">