Skip to content

Commit

Permalink
chore: changes due to code review
Browse files Browse the repository at this point in the history
  • Loading branch information
polvalente committed Oct 8, 2024
1 parent 8cc6194 commit 71b2995
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/grpc/client/interceptors/logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,40 @@ 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()

log_result(result, level, grpc_type, stream, start, stop)
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 ",
stream.service_name,
".",
to_string(elem(stream.rpc, 0)),
" -> ",
inspect(error)
inspect(error),
" (",
formatted_diff(diff),
")"
]
end)
end
Expand All @@ -80,7 +87,7 @@ defmodule GRPC.Client.Interceptors.Logger do
" -> ",
inspect(status),
" (",
GRPC.Server.Interceptors.Logger.formatted_diff(diff),
formatted_diff(diff),
")"
]
end)
Expand All @@ -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

0 comments on commit 71b2995

Please sign in to comment.