Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Task/GenServer exits correctly in Google formatter #133

Merged
merged 1 commit into from
Sep 12, 2024
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
6 changes: 5 additions & 1 deletion lib/logger_json/formatters/google_cloud.ex
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,15 @@ defmodule LoggerJSON.Formatters.GoogleCloud do
format_reported_error_event(message, ruby_stacktrace, service_context, meta)
end

def format_crash_reason(binary, {error, reason}, service_context, meta) do
def format_crash_reason(binary, {error, reason}, service_context, meta) when is_atom(error) or is_binary(error) do
stacktrace = "** (#{error}) #{inspect(reason)}"
format_reported_error_event(binary, stacktrace, service_context, meta)
end

def format_crash_reason(binary, {error, reason}, service_context, meta) do
format_crash_reason(binary, {inspect(error), reason}, service_context, meta)
end

defp format_reported_error_event(message, stacktrace, service_context, meta) do
%{
"@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent",
Expand Down
45 changes: 45 additions & 0 deletions test/logger_json/formatters/google_cloud_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,51 @@ defmodule LoggerJSON.Formatters.GoogleCloudTest do
} = log_entry
end

test "logs Task/GenServer termination" do
test_pid = self()

logs =
capture_log(fn ->
{:ok, _} = Supervisor.start_link([{CrashingGenServer, :ok}], strategy: :one_for_one)

{:ok, _} =
Task.start(fn ->
try do
GenServer.call(CrashingGenServer, :boom)
catch
_ -> nil
after
send(test_pid, :done)
end
end)

# Wait for task to finish
receive do
:done -> nil
end

# Let logs flush
Process.sleep(100)
end)

refute logs =~ "FORMATTER CRASH"

[_, log_entry] =
logs
|> String.trim()
|> String.split("\n")
|> Enum.map(&decode_or_print_error/1)

assert %{
"@type" => "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent",
"message" => message,
"stack_trace" => "** ({{%RuntimeError{message: \"boom\"}" <> _,
"serviceContext" => %{"service" => "nonode@nohost"}
} = log_entry

assert message =~ ~r/Task #PID<\d+.\d+.\d+> started from #{inspect(test_pid)} terminating/
end

test "logs process exits" do
Logger.metadata(crash_reason: {{:EXIT, self()}, :sad_failure})

Expand Down
15 changes: 15 additions & 0 deletions test/support/crashing_gen_server.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule CrashingGenServer do
use GenServer

def start_link(_) do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
end

def init(state) do
{:ok, state}
end

def handle_call(:boom, _, _) do
raise "boom"
end
end
Loading