From 71b29956d4ab78460aa1c4e14b7967e236f21483 Mon Sep 17 00:00:00 2001 From: Paulo Valente <16843419+polvalente@users.noreply.github.com> Date: Tue, 8 Oct 2024 00:46:03 -0300 Subject: [PATCH] chore: changes due to code review --- lib/grpc/client/interceptors/logger.ex | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/grpc/client/interceptors/logger.ex b/lib/grpc/client/interceptors/logger.ex index ba5c163a..48e52314 100644 --- a/lib/grpc/client/interceptors/logger.ex +++ b/lib/grpc/client/interceptors/logger.ex @@ -33,8 +33,9 @@ defmodule GRPC.Client.Interceptors.Logger do level = Keyword.fetch!(opts, :level) if Logger.compare_levels(level, Logger.level()) != :lt do + start = System.monotonic_time() + try do - start = System.monotonic_time() result = next.(stream, req) stop = System.monotonic_time() @@ -42,16 +43,19 @@ defmodule GRPC.Client.Interceptors.Logger do result rescue error in GRPC.RPCError -> - log_error(error, stream) + stop = System.monotonic_time() + log_error(error, stream, start, stop) - raise error + reraise error, __STACKTRACE__ end else next.(stream, req) end end - defp log_error(error, stream) do + defp log_error(error, stream, start, stop) do + diff = System.convert_time_unit(stop - start, :native, :microsecond) + Logger.log(:error, fn -> [ "Call ", @@ -59,7 +63,10 @@ defmodule GRPC.Client.Interceptors.Logger do ".", to_string(elem(stream.rpc, 0)), " -> ", - inspect(error) + inspect(error), + " (", + formatted_diff(diff), + ")" ] end) end @@ -80,7 +87,7 @@ defmodule GRPC.Client.Interceptors.Logger do " -> ", inspect(status), " (", - GRPC.Server.Interceptors.Logger.formatted_diff(diff), + formatted_diff(diff), ")" ] end) @@ -91,4 +98,7 @@ defmodule GRPC.Client.Interceptors.Logger do end) end end + + def formatted_diff(diff) when diff > 1000, do: [diff |> div(1000) |> Integer.to_string(), "ms"] + def formatted_diff(diff), do: [Integer.to_string(diff), "µs"] end