Skip to content

Commit

Permalink
Make async gRPC less noisy (#2507)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyggen authored Nov 17, 2023
1 parent 0c9803a commit 9bf6c13
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion sentry_sdk/integrations/grpc/aio/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
try:
import grpc
from grpc import HandlerCallDetails, RpcMethodHandler
from grpc.aio import ServicerContext
from grpc.aio import AbortError, ServicerContext
except ImportError:
raise DidNotEnable("grpcio is not installed")

Expand Down Expand Up @@ -52,6 +52,8 @@ async def wrapped(request, context):
with hub.start_transaction(transaction=transaction):
try:
return await handler.unary_unary(request, context)
except AbortError:
raise
except Exception as exc:
event, hint = event_from_exception(
exc,
Expand Down
18 changes: 18 additions & 0 deletions tests/integrations/grpc/test_grpc_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ async def test_grpc_server_exception(capture_events, grpc_server):
assert event["exception"]["values"][0]["mechanism"]["type"] == "grpc"


@pytest.mark.asyncio
async def test_grpc_server_abort(capture_events, grpc_server):
events = capture_events()

async with grpc.aio.insecure_channel("localhost:{}".format(AIO_PORT)) as channel:
stub = gRPCTestServiceStub(channel)
try:
await stub.TestServe(gRPCTestMessage(text="abort"))
raise AssertionError()
except Exception:
pass

assert len(events) == 1


@pytest.mark.asyncio
async def test_grpc_client_starts_span(
grpc_server, sentry_init, capture_events_forksafe
Expand Down Expand Up @@ -218,6 +233,9 @@ async def TestServe(cls, request, context): # noqa: N802
if request.text == "exception":
raise cls.TestException()

if request.text == "abort":
await context.abort(grpc.StatusCode.ABORTED)

return gRPCTestMessage(text=request.text)

@classmethod
Expand Down

0 comments on commit 9bf6c13

Please sign in to comment.