Skip to content

Commit

Permalink
feat: reports Tower.Event.metadata as ErrorTracker occurrence context
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Nov 27, 2024
1 parent 67770d7 commit b1a1711
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
45 changes: 21 additions & 24 deletions lib/tower_error_tracker/reporter.ex
Original file line number Diff line number Diff line change
@@ -1,35 +1,20 @@
defmodule TowerErrorTracker.Reporter do
@moduledoc false

def report_event(%Tower.Event{
kind: :error,
reason: exception,
stacktrace: stacktrace,
plug_conn: plug_conn
}) do
ErrorTracker.report(exception, stacktrace, context(plug_conn))
def report_event(%Tower.Event{kind: :error, reason: exception, stacktrace: stacktrace} = event) do
ErrorTracker.report(exception, stacktrace, context(event))

:ok
end

def report_event(%Tower.Event{
kind: :throw,
reason: value,
stacktrace: stacktrace,
plug_conn: plug_conn
}) do
ErrorTracker.report({:throw, inspect(value)}, stacktrace, context(plug_conn))
def report_event(%Tower.Event{kind: :throw, reason: value, stacktrace: stacktrace} = event) do
ErrorTracker.report({:throw, inspect(value)}, stacktrace, context(event))

:ok
end

def report_event(%Tower.Event{
kind: :exit,
reason: reason,
stacktrace: stacktrace,
plug_conn: plug_conn
}) do
ErrorTracker.report({:exit, Exception.format_exit(reason)}, stacktrace, context(plug_conn))
def report_event(%Tower.Event{kind: :exit, reason: reason, stacktrace: stacktrace} = event) do
ErrorTracker.report({:exit, Exception.format_exit(reason)}, stacktrace, context(event))

:ok
end
Expand All @@ -38,12 +23,24 @@ defmodule TowerErrorTracker.Reporter do
:ignore
end

defp context(%{metadata: metadata, plug_conn: plug_conn}) do
%{}
|> Map.merge(metadata_context(metadata))
|> Map.merge(request_context(plug_conn))
end

defp metadata_context(metadata) when is_map(metadata) and map_size(metadata) > 0 do
%{metadata: metadata}
end

defp metadata_context(_), do: %{}

if Code.ensure_loaded?(Plug.Conn) do
defp context(%Plug.Conn{} = conn) do
defp request_context(%Plug.Conn{} = conn) do
%{request: request_data(conn)}
end

defp context(_), do: %{}
defp request_context(_), do: %{}

defp request_data(%Plug.Conn{} = conn) do
%{
Expand All @@ -52,6 +49,6 @@ defmodule TowerErrorTracker.Reporter do
}
end
else
defp context(_), do: %{}
defp request_context(_), do: %{}
end
end
22 changes: 22 additions & 0 deletions test/tower_error_tracker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,28 @@ defmodule TowerErrorTrackerTest do
)
end

test "reports event metadata as context" do
Tower.report_exception(RuntimeError.exception("Oops!"), [], metadata: %{user_id: 123})

assert_eventually(
[
%{
kind: "Elixir.RuntimeError",
reason: "Oops!",
occurrences: [
%{
context: %{
"metadata" => %{
"user_id" => 123
}
}
}
]
}
] = TestApp.Repo.all(ErrorTracker.Error) |> TestApp.Repo.preload(:occurrences)
)
end

defp in_unlinked_process(fun) when is_function(fun, 0) do
{:ok, pid} = Task.Supervisor.start_link()

Expand Down

0 comments on commit b1a1711

Please sign in to comment.