Skip to content
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
5 changes: 5 additions & 0 deletions .sampo/changesets/somber-knight-otso.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
hex/posthog: patch
---

Improve error tracking grouping by always using arity to format stacktrace frames
3 changes: 2 additions & 1 deletion lib/posthog/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,12 @@ defmodule PostHog.Handler do
in_app = module in in_app_modules
filename = Keyword.get(location, :file, []) |> IO.chardata_to_string()
lineno = Keyword.get(location, :line)
arity = with args when is_list(args) <- arity_or_args, do: length(args)

%{
platform: "custom",
lang: "elixir",
function: Exception.format_mfa(module, function, arity_or_args),
function: Exception.format_mfa(module, function, arity),
filename: filename,
lineno: lineno,
module: inspect(module),
Expand Down
22 changes: 20 additions & 2 deletions test/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ defmodule PostHog.IntegrationTest do
|> PostHog.Registry.via(PostHog.Sender, 1)
|> GenServer.whereis()

send(sender_pid, :batch_time_reached)
:sys.get_status(sender_pid)
case :sys.get_state(sender_pid) do
%{timer_ref: ref} ->
send(sender_pid, {:timeout, ref, :batch_time_reached})
:sys.get_status(sender_pid)

_ ->
:ok
end
end

:logger.add_handler(:posthog, PostHog.Handler, %{config: config})
Expand Down Expand Up @@ -73,6 +79,18 @@ defmodule PostHog.IntegrationTest do
wait.()
end

test "anonymous function", %{wait_fun: wait} do
{:ok, pid} =
Task.start(fn ->
Enum.map([:foo], fn x when is_binary(x) -> :ok end)
end)

ref = Process.monitor(pid)
assert_receive({:DOWN, ^ref, _, _, _})

wait.()
end

test "exports metadata", %{wait_fun: wait} do
LoggerHandlerKit.Act.metadata_serialization(:all)
Logger.error("Error with metadata")
Expand Down
72 changes: 70 additions & 2 deletions test/posthog/handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1639,7 +1639,7 @@ defmodule PostHog.HandlerTest do
frames: [
%{
filename: "",
function: ":erlang.system_time(:foo)",
function: ":erlang.system_time/1",
in_app: false,
lineno: nil,
module: ":erlang",
Expand Down Expand Up @@ -1668,7 +1668,7 @@ defmodule PostHog.HandlerTest do
frames: [
%{
filename: "",
function: ":erlang.system_time(:foo)",
function: ":erlang.system_time/1",
in_app: false,
lineno: nil,
module: ":erlang",
Expand All @@ -1690,6 +1690,74 @@ defmodule PostHog.HandlerTest do
end
end

test "anonymous function in stacktrace", %{
handler_ref: ref,
config: %{supervisor_name: supervisor_name}
} do
{:ok, _pid} = Task.start(fn -> Enum.map([:foo], fn :bar -> :ok end) end)
LoggerHandlerKit.Assert.assert_logged(ref)

assert [event] = all_captured(supervisor_name)

if pre_19?() do
assert %{
event: "$exception",
properties: %{
"$exception_list": [
%{
type: "FunctionClauseError",
value:
"no function clause matching in anonymous fn/1 in PostHog.HandlerTest.\"test anonymous function in stacktrace\"/1",
stacktrace: %{
type: "raw",
frames: [
%{
function:
"anonymous fn/1 in PostHog.HandlerTest.\"test anonymous function in stacktrace\"/1",
platform: "custom",
lang: "elixir"
}
| _
]
},
mechanism: %{type: "generic", handled: false}
}
]
}
} = event
else
assert %{
event: "$exception",
properties: %{
"$exception_list": [
%{
type: "FunctionClauseError",
value:
"no function clause matching in anonymous fn/1 in PostHog.HandlerTest.\"test anonymous function in stacktrace\"/1",
stacktrace: %{
type: "raw",
frames: [
%{
function:
"anonymous fn/1 in PostHog.HandlerTest.\"test anonymous function in stacktrace\"/1",
platform: "custom",
lang: "elixir"
}
| _
]
},
mechanism: %{type: "generic", handled: false}
},
%{
mechanism: %{handled: true, type: "generic"},
type: "Task terminating"
}
]
}
} = event
end
end

@tag config: [in_app_otp_apps: [:logger_handler_kit]]
test "marks in_app frames as such", %{
handler_ref: ref,
Expand Down