Skip to content

Commit 9a98ceb

Browse files
authored
ref(grpc): Use new scopes API (#2886)
1 parent f801578 commit 9a98ceb

File tree

5 files changed

+54
-60
lines changed

5 files changed

+54
-60
lines changed

sentry_sdk/integrations/grpc/aio/client.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99
)
1010
from google.protobuf.message import Message
1111

12-
from sentry_sdk import Hub
12+
import sentry_sdk
1313
from sentry_sdk.consts import OP
14+
from sentry_sdk.scope import Scope
1415

1516

1617
class ClientInterceptor:
1718
@staticmethod
18-
def _update_client_call_details_metadata_from_hub(
19-
client_call_details: ClientCallDetails, hub: Hub
19+
def _update_client_call_details_metadata_from_scope(
20+
client_call_details: ClientCallDetails,
2021
) -> ClientCallDetails:
2122
metadata = (
2223
list(client_call_details.metadata) if client_call_details.metadata else []
2324
)
24-
for key, value in hub.iter_trace_propagation_headers():
25+
for key, value in Scope.get_current_scope().iter_trace_propagation_headers():
2526
metadata.append((key, value))
2627

2728
client_call_details = ClientCallDetails(
@@ -42,17 +43,16 @@ async def intercept_unary_unary(
4243
client_call_details: ClientCallDetails,
4344
request: Message,
4445
) -> Union[UnaryUnaryCall, Message]:
45-
hub = Hub.current
4646
method = client_call_details.method
4747

48-
with hub.start_span(
48+
with sentry_sdk.start_span(
4949
op=OP.GRPC_CLIENT, description="unary unary call to %s" % method.decode()
5050
) as span:
5151
span.set_data("type", "unary unary")
5252
span.set_data("method", method)
5353

54-
client_call_details = self._update_client_call_details_metadata_from_hub(
55-
client_call_details, hub
54+
client_call_details = self._update_client_call_details_metadata_from_scope(
55+
client_call_details
5656
)
5757

5858
response = await continuation(client_call_details, request)
@@ -71,17 +71,16 @@ async def intercept_unary_stream(
7171
client_call_details: ClientCallDetails,
7272
request: Message,
7373
) -> Union[AsyncIterable[Any], UnaryStreamCall]:
74-
hub = Hub.current
7574
method = client_call_details.method
7675

77-
with hub.start_span(
76+
with sentry_sdk.start_span(
7877
op=OP.GRPC_CLIENT, description="unary stream call to %s" % method.decode()
7978
) as span:
8079
span.set_data("type", "unary stream")
8180
span.set_data("method", method)
8281

83-
client_call_details = self._update_client_call_details_metadata_from_hub(
84-
client_call_details, hub
82+
client_call_details = self._update_client_call_details_metadata_from_scope(
83+
client_call_details
8584
)
8685

8786
response = await continuation(client_call_details, request)

sentry_sdk/integrations/grpc/aio/server.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from sentry_sdk import Hub
2-
from sentry_sdk._types import MYPY
1+
import sentry_sdk
2+
from sentry_sdk._types import TYPE_CHECKING
33
from sentry_sdk.consts import OP
44
from sentry_sdk.integrations import DidNotEnable
55
from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_CUSTOM
66
from sentry_sdk.utils import event_from_exception
77

8-
if MYPY:
8+
if TYPE_CHECKING:
99
from collections.abc import Awaitable, Callable
1010
from typing import Any
1111

@@ -39,8 +39,6 @@ async def wrapped(request, context):
3939
if not name:
4040
return await handler(request, context)
4141

42-
hub = Hub.current
43-
4442
# What if the headers are empty?
4543
transaction = Transaction.continue_from_headers(
4644
dict(context.invocation_metadata()),
@@ -49,7 +47,7 @@ async def wrapped(request, context):
4947
source=TRANSACTION_SOURCE_CUSTOM,
5048
)
5149

52-
with hub.start_transaction(transaction=transaction):
50+
with sentry_sdk.start_transaction(transaction=transaction):
5351
try:
5452
return await handler.unary_unary(request, context)
5553
except AbortError:
@@ -59,7 +57,7 @@ async def wrapped(request, context):
5957
exc,
6058
mechanism={"type": "grpc", "handled": False},
6159
)
62-
hub.capture_event(event, hint=hint)
60+
sentry_sdk.capture_event(event, hint=hint)
6361
raise
6462

6563
elif not handler.request_streaming and handler.response_streaming:

sentry_sdk/integrations/grpc/client.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from sentry_sdk import Hub
2-
from sentry_sdk._types import MYPY
1+
import sentry_sdk
2+
from sentry_sdk._types import TYPE_CHECKING
33
from sentry_sdk.consts import OP
44
from sentry_sdk.integrations import DidNotEnable
5+
from sentry_sdk.scope import Scope
56

6-
if MYPY:
7+
if TYPE_CHECKING:
78
from typing import Any, Callable, Iterator, Iterable, Union
89

910
try:
@@ -23,17 +24,16 @@ class ClientInterceptor(
2324

2425
def intercept_unary_unary(self, continuation, client_call_details, request):
2526
# type: (ClientInterceptor, Callable[[ClientCallDetails, Message], _UnaryOutcome], ClientCallDetails, Message) -> _UnaryOutcome
26-
hub = Hub.current
2727
method = client_call_details.method
2828

29-
with hub.start_span(
29+
with sentry_sdk.start_span(
3030
op=OP.GRPC_CLIENT, description="unary unary call to %s" % method
3131
) as span:
3232
span.set_data("type", "unary unary")
3333
span.set_data("method", method)
3434

35-
client_call_details = self._update_client_call_details_metadata_from_hub(
36-
client_call_details, hub
35+
client_call_details = self._update_client_call_details_metadata_from_scope(
36+
client_call_details
3737
)
3838

3939
response = continuation(client_call_details, request)
@@ -43,17 +43,16 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
4343

4444
def intercept_unary_stream(self, continuation, client_call_details, request):
4545
# type: (ClientInterceptor, Callable[[ClientCallDetails, Message], Union[Iterable[Any], UnaryStreamCall]], ClientCallDetails, Message) -> Union[Iterator[Message], Call]
46-
hub = Hub.current
4746
method = client_call_details.method
4847

49-
with hub.start_span(
48+
with sentry_sdk.start_span(
5049
op=OP.GRPC_CLIENT, description="unary stream call to %s" % method
5150
) as span:
5251
span.set_data("type", "unary stream")
5352
span.set_data("method", method)
5453

55-
client_call_details = self._update_client_call_details_metadata_from_hub(
56-
client_call_details, hub
54+
client_call_details = self._update_client_call_details_metadata_from_scope(
55+
client_call_details
5756
)
5857

5958
response = continuation(
@@ -65,12 +64,12 @@ def intercept_unary_stream(self, continuation, client_call_details, request):
6564
return response
6665

6766
@staticmethod
68-
def _update_client_call_details_metadata_from_hub(client_call_details, hub):
69-
# type: (ClientCallDetails, Hub) -> ClientCallDetails
67+
def _update_client_call_details_metadata_from_scope(client_call_details):
68+
# type: (ClientCallDetails) -> ClientCallDetails
7069
metadata = (
7170
list(client_call_details.metadata) if client_call_details.metadata else []
7271
)
73-
for key, value in hub.iter_trace_propagation_headers():
72+
for key, value in Scope.get_current_scope().iter_trace_propagation_headers():
7473
metadata.append((key, value))
7574

7675
client_call_details = grpc._interceptor._ClientCallDetails(

sentry_sdk/integrations/grpc/server.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from sentry_sdk import Hub
2-
from sentry_sdk._types import MYPY
1+
import sentry_sdk
2+
from sentry_sdk._types import TYPE_CHECKING
33
from sentry_sdk.consts import OP
44
from sentry_sdk.integrations import DidNotEnable
55
from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_CUSTOM
66

7-
if MYPY:
7+
if TYPE_CHECKING:
88
from typing import Callable, Optional
99
from google.protobuf.message import Message
1010

@@ -30,27 +30,26 @@ def intercept_service(self, continuation, handler_call_details):
3030

3131
def behavior(request, context):
3232
# type: (Message, ServicerContext) -> Message
33-
hub = Hub(Hub.current)
34-
35-
name = self._find_method_name(context)
36-
37-
if name:
38-
metadata = dict(context.invocation_metadata())
39-
40-
transaction = Transaction.continue_from_headers(
41-
metadata,
42-
op=OP.GRPC_SERVER,
43-
name=name,
44-
source=TRANSACTION_SOURCE_CUSTOM,
45-
)
46-
47-
with hub.start_transaction(transaction=transaction):
48-
try:
49-
return handler.unary_unary(request, context)
50-
except BaseException as e:
51-
raise e
52-
else:
53-
return handler.unary_unary(request, context)
33+
with sentry_sdk.isolation_scope():
34+
name = self._find_method_name(context)
35+
36+
if name:
37+
metadata = dict(context.invocation_metadata())
38+
39+
transaction = Transaction.continue_from_headers(
40+
metadata,
41+
op=OP.GRPC_SERVER,
42+
name=name,
43+
source=TRANSACTION_SOURCE_CUSTOM,
44+
)
45+
46+
with sentry_sdk.start_transaction(transaction=transaction):
47+
try:
48+
return handler.unary_unary(request, context)
49+
except BaseException as e:
50+
raise e
51+
else:
52+
return handler.unary_unary(request, context)
5453

5554
return grpc.unary_unary_rpc_method_handler(
5655
behavior,

tests/integrations/grpc/test_grpc.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import grpc
77
import pytest
88

9-
from sentry_sdk import Hub, start_transaction
9+
from sentry_sdk import start_span, start_transaction
1010
from sentry_sdk.consts import OP
1111
from sentry_sdk.integrations.grpc import GRPCIntegration
1212
from tests.integrations.grpc.grpc_test_service_pb2 import gRPCTestMessage
@@ -310,8 +310,7 @@ class TestService(gRPCTestServiceServicer):
310310

311311
@staticmethod
312312
def TestServe(request, context): # noqa: N802
313-
hub = Hub.current
314-
with hub.start_span(op="test", description="test"):
313+
with start_span(op="test", description="test"):
315314
pass
316315

317316
return gRPCTestMessage(text=request.text)

0 commit comments

Comments
 (0)