Skip to content

Commit 7f992e3

Browse files
feat(tracing) Add type hints for grpc and socket integrations
1 parent 4a09bf9 commit 7f992e3

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

sentry_sdk/integrations/grpc/client.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
from sentry_sdk.integrations import DidNotEnable
55

66
if MYPY:
7-
pass
7+
from typing import Callable, Iterator
88

99
try:
1010
import grpc
11+
from grpc import ClientCallDetails
12+
from grpc._interceptor import _UnaryOutcome
13+
from google.protobuf.message import Message
1114
except ImportError:
1215
raise DidNotEnable("grpcio is not installed")
1316

@@ -16,11 +19,12 @@ class ClientInterceptor(
1619
grpc.UnaryUnaryClientInterceptor, grpc.UnaryStreamClientInterceptor
1720
):
1821
def intercept_unary_unary(self, continuation, client_call_details, request):
22+
# type: (ClientInterceptor, Callable[ClientCallDetails], ClientCallDetails, Message) -> _UnaryOutcome
1923
hub = Hub.current
2024
method = client_call_details.method
2125

2226
with hub.start_span(
23-
op=OP.GRPC_CLIENT, description="unary unary call to %s" % method
27+
op=OP.GRPC_CLIENT, description="unary unary call to %s" % method
2428
) as span:
2529
span.set_data("type", "unary unary")
2630
span.set_data("method", method)
@@ -35,11 +39,12 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
3539
return response
3640

3741
def intercept_unary_stream(self, continuation, client_call_details, request):
42+
# type: (ClientInterceptor, Callable[ClientCallDetails], ClientCallDetails, Message) -> Iterator
3843
hub = Hub.current
3944
method = client_call_details.method
4045

4146
with hub.start_span(
42-
op=OP.GRPC_CLIENT, description="unary stream call to %s" % method
47+
op=OP.GRPC_CLIENT, description="unary stream call to %s" % method
4348
) as span:
4449
span.set_data("type", "unary stream")
4550
span.set_data("method", method)

sentry_sdk/integrations/grpc/server.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@
55
from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_CUSTOM
66

77
if MYPY:
8-
from typing import Callable
8+
from typing import Callable, Optional
99

1010
try:
1111
import grpc
12+
from grpc import ServicerContext, HandlerCallDetails
1213
except ImportError:
1314
raise DidNotEnable("grpcio is not installed")
1415

1516

1617
class ServerInterceptor(grpc.ServerInterceptor):
1718
def __init__(self, find_name=None):
18-
# type: (ServerInterceptor, Callable | None) -> ServerInterceptor
19+
# type: (ServerInterceptor, Optional[Callable[ServicerContext]]) -> None
1920
if find_name:
2021
self._find_method_name = find_name
2122
super(ServerInterceptor, self).__init__()
2223

2324
def intercept_service(self, continuation, handler_call_details):
25+
# type: (ServerInterceptor, Callable[HandlerCallDetails], HandlerCallDetails) -> RpcMethodHandler
2426
handler = continuation(handler_call_details)
2527
if not handler or not handler.unary_unary:
2628
return handler
@@ -56,4 +58,5 @@ def behavior(request, context):
5658

5759
@staticmethod
5860
def _find_name(context):
61+
# type: (ServicerContext) -> str
5962
return context._rpc_event.call_details.method.decode()

sentry_sdk/integrations/socket.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import socket
22

33
from sentry_sdk import Hub
4+
from sentry_sdk._types import MYPY
45
from sentry_sdk.consts import OP
56
from sentry_sdk.integrations import Integration
67

8+
if MYPY:
9+
from typing import Tuple, Optional, Union, Dict, List
10+
711
__all__ = ["SocketIntegration"]
812

913

@@ -27,7 +31,7 @@ def _patch_create_connection():
2731
def create_connection(
2832
address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None
2933
):
30-
# type: (tuple[str, None | int], float | None, tuple[bytearray | bytes | str, int] | None) -> socket
34+
# type: (Tuple[Optional[str], int], Optional[float], Optional[Tuple[Union[bytearray, bytes, str], int]])-> socket.socket
3135
hub = Hub.current
3236
if hub.get_integration(SocketIntegration) is None:
3337
return real_create_connection(
@@ -53,6 +57,7 @@ def _patch_getaddrinfo():
5357
real_getaddrinfo = socket.getaddrinfo
5458

5559
def getaddrinfo(host, port, *args, **kwargs):
60+
# type: (str, int, Optional[Tuple], Optional[Dict]) -> List
5661
hub = Hub.current
5762
if hub.get_integration(SocketIntegration) is None:
5863
return real_getaddrinfo(host, port, *args, **kwargs)

0 commit comments

Comments
 (0)