Skip to content

Commit a64b739

Browse files
committed
Normalize anonymous function stack frames
1 parent 32d6d3e commit a64b739

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

lib/posthog/handler.ex

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ defmodule PostHog.Handler do
215215
%{
216216
platform: "custom",
217217
lang: "elixir",
218-
function: Exception.format_mfa(module, function, arity_or_args),
218+
function: format_function(module, function, arity_or_args),
219219
filename: filename,
220220
lineno: lineno,
221221
module: inspect(module),
@@ -231,6 +231,16 @@ defmodule PostHog.Handler do
231231
}
232232
end
233233

234+
defp format_function(module, function, arity_or_args) do
235+
module
236+
|> Exception.format_mfa(function, arity_or_args)
237+
|> normalize_anonymous_function()
238+
end
239+
240+
defp normalize_anonymous_function(function) do
241+
String.replace(function, ~r/anonymous fn\(\d+\) in /, "anonymous fn in ")
242+
end
243+
234244
defp maybe_add_source_context(frame, filename, lineno, config) do
235245
with true <- config.enable_source_code_context,
236246
%{} = source_map <- Sources.get_source_map_for_file(config.supervisor_name, filename) do

test/posthog/handler_test.exs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,31 @@ defmodule PostHog.HandlerTest do
119119
end
120120
end
121121

122+
test "normalizes volatile anonymous function indexes in stack frames", %{
123+
handler_ref: ref,
124+
config: %{supervisor_name: supervisor_name}
125+
} do
126+
stacktrace = [
127+
{__MODULE__, :"anonymous fn(250) in PostHog.HandlerTest.example/1", 1,
128+
[file: ~c"test/posthog/handler_test.exs", line: 1]}
129+
]
130+
131+
Logger.error("Hello World", crash_reason: {%RuntimeError{message: "boom"}, stacktrace})
132+
LoggerHandlerKit.Assert.assert_logged(ref)
133+
134+
assert [event] = all_captured(supervisor_name)
135+
assert %{properties: %{"$exception_list": exception_list}} = event
136+
137+
assert exception_list
138+
|> Enum.flat_map(fn exception ->
139+
get_in(exception, [:stacktrace, :frames]) || []
140+
end)
141+
|> Enum.any?(fn frame ->
142+
frame.function ==
143+
~s(PostHog.HandlerTest."anonymous fn in PostHog.HandlerTest.example/1"/1)
144+
end)
145+
end
146+
122147
test "string message", %{handler_ref: ref, config: %{supervisor_name: supervisor_name}} do
123148
LoggerHandlerKit.Act.string_message()
124149
LoggerHandlerKit.Assert.assert_logged(ref)

0 commit comments

Comments
 (0)