Skip to content

Improve Error Tracking for complex errors#85

Open
martosaur wants to merge 5 commits intoPostHog:masterfrom
martosaur:am-multiple-exceptions-support
Open

Improve Error Tracking for complex errors#85
martosaur wants to merge 5 commits intoPostHog:masterfrom
martosaur:am-multiple-exceptions-support

Conversation

@martosaur
Copy link
Contributor

Error Tracking supports chained exceptions. This actually maps fairly well on OTP errors that are usually caused by some other underlying error. Let's take advantage of this!

In this PR:

  1. If we see a crash_reason in the logger metadata, we don't consider it the only important part, but rather use it as a sign of a chained error. We create a dedicated $exception_list entry for crash reason, and then add an additional one based on the rest of the log event. This also allows us to better support "handled" errors that add crash_reason, e.g. `Logger.error("Unexpected error", crash_reason: {exception, STACKTRACE}). Previously, the string "unexpected error" would be simply ignored. Now it will be the first error in chain.
  2. Add an enrich_metadata stage to logger handler that will extract some useful info from known OTP reports and put them into event properties.
  3. Drop some known internal metadata keys, since they aren't really useful.

Before

image

After

image

It would be even better with newlines of course...

@rafaeelaudibert
Copy link
Member

Hey @martosaur! Hopefully PostHog/posthog#48689 will help solve the newlines problem.

Copy link
Member

@rafaeelaudibert rafaeelaudibert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tysm! This is good, as always. Left some comments for possible future improvements.

Comment on lines +136 to +150
defp do_type(%{msg: {:report, %{label: {:gen_server, :terminate}}}}) do
"GenServer terminating"
end

defp do_type(%{msg: {:report, %{label: {Task.Supervisor, :terminating}}}}) do
"Task terminating"
end

defp do_type(%{msg: {:report, %{label: {:gen_statem, :terminate}}}}) do
":gen_statem terminating"
end

defp do_type(%{msg: {:report, %{label: {:proc_lib, :crash}}}}) do
"Process terminating"
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means we'll group all of this under the same issue. I assume this is not a problem because this is the underlying reason and we'll group under the actual error, but we might wanna change this in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this shouldn't be a problem, because by default fingerprinting uses not just type but also the latest application frame, which should be different 🤔


defp stacktrace(_event, _), do: %{}

defp do_stacktrace([_ | _] = stacktrace, in_app_modules) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like an extra indirection for no reason, could've inlined this under stacktrace/2 above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But stacktrace/2 has two heads at this point? We could use case, sure, but that kind of goes against the grain of all other functions in the module, esp. type + do_type. Or am I misunderstanding you comment?

@rafaeelaudibert
Copy link
Member

@martosaur tests are failing, mind checking?

@rafaeelaudibert rafaeelaudibert self-requested a review February 24, 2026 00:08
@martosaur
Copy link
Contributor Author

Oof, I forgot that this whole thing will only be working nicely with Elixir 1.18+, previous versions will behave as before. I'll have to add test variants for older versions, I'll do this over the weekend!

Copy link
Member

@martosaur all good! just remember to re-request my review once you've updated it. I've merged PostHog/posthog#48689 to hopefully make this more readable too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants