Skip to content

Commit d028d65

Browse files
author
Robin
committed
fix(grpc): add isolation_scope to async server interceptor
1 parent cd656f0 commit d028d65

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

sentry_sdk/integrations/grpc/aio/server.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,32 @@ async def intercept_service(
4343
handler_factory = grpc.unary_unary_rpc_method_handler
4444

4545
async def wrapped(request: "Any", context: "ServicerContext") -> "Any":
46-
name = self._find_method_name(context)
47-
if not name:
48-
return await handler(request, context)
49-
50-
# What if the headers are empty?
51-
transaction = sentry_sdk.continue_trace(
52-
dict(context.invocation_metadata()),
53-
op=OP.GRPC_SERVER,
54-
name=name,
55-
source=TransactionSource.CUSTOM,
56-
origin=SPAN_ORIGIN,
57-
)
58-
59-
with sentry_sdk.start_transaction(transaction=transaction):
60-
try:
61-
return await handler.unary_unary(request, context)
62-
except AbortError:
63-
raise
64-
except Exception as exc:
65-
event, hint = event_from_exception(
66-
exc,
67-
mechanism={"type": "grpc", "handled": False},
68-
)
69-
sentry_sdk.capture_event(event, hint=hint)
70-
raise
46+
with sentry_sdk.isolation_scope():
47+
name = self._find_method_name(context)
48+
if not name:
49+
return await handler(request, context)
50+
51+
# What if the headers are empty?
52+
transaction = sentry_sdk.continue_trace(
53+
dict(context.invocation_metadata()),
54+
op=OP.GRPC_SERVER,
55+
name=name,
56+
source=TransactionSource.CUSTOM,
57+
origin=SPAN_ORIGIN,
58+
)
59+
60+
with sentry_sdk.start_transaction(transaction=transaction):
61+
try:
62+
return await handler.unary_unary(request, context)
63+
except AbortError:
64+
raise
65+
except Exception as exc:
66+
event, hint = event_from_exception(
67+
exc,
68+
mechanism={"type": "grpc", "handled": False},
69+
)
70+
sentry_sdk.capture_event(event, hint=hint)
71+
raise
7172

7273
elif not handler.request_streaming and handler.response_streaming:
7374
handler_factory = grpc.unary_stream_rpc_method_handler

0 commit comments

Comments
 (0)