Skip to content

Commit 01a51d3

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

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

mypy.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,5 @@ ignore_missing_imports = True
6767
ignore_missing_imports = True
6868
[mypy-arq.*]
6969
ignore_missing_imports = True
70+
[mypy-grpc.*]
71+
ignore_missing_imports = True

sentry_sdk/integrations/grpc/client.py

Lines changed: 9 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, Union
88

99
try:
1010
import grpc
11+
from grpc import ClientCallDetails, Call
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, Message], _UnaryOutcome], 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, Message], Iterator], ClientCallDetails, Message) -> Union[Iterator[Message], Call]
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)
@@ -55,6 +60,7 @@ def intercept_unary_stream(self, continuation, client_call_details, request):
5560

5661
@staticmethod
5762
def _update_client_call_details_metadata_from_hub(client_call_details, hub):
63+
# type: (ClientCallDetails, Hub) -> ClientCallDetails
5864
metadata = (
5965
list(client_call_details.metadata) if client_call_details.metadata else []
6066
)

sentry_sdk/integrations/grpc/server.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,31 @@
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
9+
from google.protobuf.message import Message
910

1011
try:
1112
import grpc
13+
from grpc import ServicerContext, HandlerCallDetails, RpcMethodHandler
1214
except ImportError:
1315
raise DidNotEnable("grpcio is not installed")
1416

1517

1618
class ServerInterceptor(grpc.ServerInterceptor):
1719
def __init__(self, find_name=None):
18-
# type: (ServerInterceptor, Callable | None) -> ServerInterceptor
20+
# type: (ServerInterceptor, Optional[Callable[[ServicerContext], str]]) -> None
1921
if find_name:
2022
self._find_method_name = find_name
2123
super(ServerInterceptor, self).__init__()
2224

2325
def intercept_service(self, continuation, handler_call_details):
26+
# type: (ServerInterceptor, Callable[[HandlerCallDetails], RpcMethodHandler], HandlerCallDetails) -> RpcMethodHandler
2427
handler = continuation(handler_call_details)
2528
if not handler or not handler.unary_unary:
2629
return handler
2730

2831
def behavior(request, context):
32+
# type: (Message, ServicerContext) -> Message
2933
hub = Hub(Hub.current)
3034

3135
name = self._find_method_name(context)
@@ -56,4 +60,5 @@ def behavior(request, context):
5660

5761
@staticmethod
5862
def _find_name(context):
63+
# type: (ServicerContext) -> str
5964
return context._rpc_event.call_details.method.decode()

sentry_sdk/integrations/socket.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
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 socket import AddressFamily, SocketKind
10+
from typing import Tuple, Optional, Union, List
11+
712
__all__ = ["SocketIntegration"]
813

914

@@ -25,17 +30,17 @@ def _patch_create_connection():
2530
real_create_connection = socket.create_connection
2631

2732
def create_connection(
28-
address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None
33+
address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None
2934
):
30-
# type: (tuple[str, None | int], float | None, tuple[bytearray | bytes | str, int] | None) -> socket
35+
# type: (Tuple[Optional[str], int], Optional[float], Optional[Tuple[Union[bytearray, bytes, str], int]])-> socket.socket
3136
hub = Hub.current
3237
if hub.get_integration(SocketIntegration) is None:
3338
return real_create_connection(
3439
address=address, timeout=timeout, source_address=source_address
3540
)
3641

3742
with hub.start_span(
38-
op=OP.SOCKET_CONNECTION, description="%s:%s" % (address[0], address[1])
43+
op=OP.SOCKET_CONNECTION, description="%s:%s" % (address[0], address[1])
3944
) as span:
4045
span.set_data("address", address)
4146
span.set_data("timeout", timeout)
@@ -52,17 +57,18 @@ def _patch_getaddrinfo():
5257
# type: () -> None
5358
real_getaddrinfo = socket.getaddrinfo
5459

55-
def getaddrinfo(host, port, *args, **kwargs):
60+
def getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
61+
# type: (Union[bytes, str, None], Union[str, int, None], int, int, int, int) -> List[Tuple[AddressFamily, SocketKind, int, str, Union[Tuple[str, int], Tuple[str, int, int, int]]]]
5662
hub = Hub.current
5763
if hub.get_integration(SocketIntegration) is None:
58-
return real_getaddrinfo(host, port, *args, **kwargs)
64+
return real_getaddrinfo(host, port, family, type, proto, flags)
5965

6066
with hub.start_span(
61-
op=OP.SOCKET_DNS, description="%s:%s" % (host, port)
67+
op=OP.SOCKET_DNS, description="%s:%s" % (host, port)
6268
) as span:
6369
span.set_data("host", host)
6470
span.set_data("port", port)
6571

66-
return real_getaddrinfo(host, port, *args, **kwargs)
72+
return real_getaddrinfo(host, port, family, type, proto, flags)
6773

6874
socket.getaddrinfo = getaddrinfo

0 commit comments

Comments
 (0)