Skip to content

Commit 75b253d

Browse files
Log server timeouts captured by Cowboy.Handler (#439)
Co-authored-by: Adriano Santos <[email protected]>
1 parent 9df8fcc commit 75b253d

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

lib/grpc/server/adapters/cowboy/handler.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,10 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
464464
error = %RPCError{status: GRPC.Status.deadline_exceeded(), message: "Deadline expired"}
465465
req = send_error(req, error, state, :timeout)
466466

467+
[req: req]
468+
|> ReportException.new(error)
469+
|> log_error()
470+
467471
{:stop, req, state}
468472
end
469473

@@ -694,7 +698,7 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
694698
{:wait, ref}
695699
end
696700

697-
defp log_error(%ReportException{kind: kind} = exception, stacktrace) do
701+
defp log_error(%ReportException{kind: kind} = exception, stacktrace \\ []) do
698702
crash_reason = GRPC.Logger.crash_reason(kind, exception, stacktrace)
699703

700704
kind

test/grpc/integration/server_test.exs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,20 @@ defmodule GRPC.Integration.ServerTest do
277277
end
278278

279279
test "return deadline error for slow server" do
280-
run_server([TimeoutServer], fn port ->
281-
{:ok, channel} = GRPC.Stub.connect("localhost:#{port}")
282-
rect = %Routeguide.Rectangle{}
283-
error = %GRPC.RPCError{message: "Deadline expired", status: 4}
280+
logs =
281+
ExUnit.CaptureLog.capture_log(fn ->
282+
run_server([TimeoutServer], fn port ->
283+
{:ok, channel} = GRPC.Stub.connect("localhost:#{port}")
284+
rect = %Routeguide.Rectangle{}
285+
error = %GRPC.RPCError{message: "Deadline expired", status: 4}
284286

285-
assert {:error, ^error} =
286-
channel |> Routeguide.RouteGuide.Stub.list_features(rect, timeout: 500)
287-
end)
287+
assert {:error, ^error} =
288+
channel |> Routeguide.RouteGuide.Stub.list_features(rect, timeout: 500)
289+
end)
290+
end)
291+
292+
assert logs =~
293+
"Exception raised while handling /routeguide.RouteGuide/ListFeatures:\n** (GRPC.RPCError) Deadline expired"
288294
end
289295

290296
test "return normally for a little slow server" do

0 commit comments

Comments
 (0)