|
1 | | -from .server import ServerInterceptor # noqa: F401 |
2 | | -from .client import ClientInterceptor # noqa: F401 |
| 1 | +from functools import wraps |
| 2 | + |
| 3 | +import grpc |
| 4 | +from grpc import Channel, Server, intercept_channel |
| 5 | +from grpc.aio import Channel as AsyncChannel |
| 6 | +from grpc.aio import Server as AsyncServer |
| 7 | + |
| 8 | +from sentry_sdk.integrations import Integration |
| 9 | +from sentry_sdk._types import TYPE_CHECKING |
| 10 | + |
| 11 | +from .client import ClientInterceptor |
| 12 | +from .server import ServerInterceptor |
| 13 | +from .aio.server import ServerInterceptor as AsyncServerInterceptor |
| 14 | +from .aio.client import ( |
| 15 | + SentryUnaryUnaryClientInterceptor as AsyncUnaryUnaryClientInterceptor, |
| 16 | +) |
| 17 | +from .aio.client import ( |
| 18 | + SentryUnaryStreamClientInterceptor as AsyncUnaryStreamClientIntercetor, |
| 19 | +) |
| 20 | + |
| 21 | +from typing import Any, Optional, Sequence |
| 22 | + |
| 23 | +# Hack to get new Python features working in older versions |
| 24 | +# without introducing a hard dependency on `typing_extensions` |
| 25 | +# from: https://stackoverflow.com/a/71944042/300572 |
| 26 | +if TYPE_CHECKING: |
| 27 | + from typing import ParamSpec, Callable |
| 28 | +else: |
| 29 | + # Fake ParamSpec |
| 30 | + class ParamSpec: |
| 31 | + def __init__(self, _): |
| 32 | + self.args = None |
| 33 | + self.kwargs = None |
| 34 | + |
| 35 | + # Callable[anything] will return None |
| 36 | + class _Callable: |
| 37 | + def __getitem__(self, _): |
| 38 | + return None |
| 39 | + |
| 40 | + # Make instances |
| 41 | + Callable = _Callable() |
| 42 | + |
| 43 | +P = ParamSpec("P") |
| 44 | + |
| 45 | + |
| 46 | +def _wrap_channel_sync(func: Callable[P, Channel]) -> Callable[P, Channel]: |
| 47 | + "Wrapper for synchronous secure and insecure channel." |
| 48 | + |
| 49 | + @wraps(func) |
| 50 | + def patched_channel(*args: Any, **kwargs: Any) -> Channel: |
| 51 | + channel = func(*args, **kwargs) |
| 52 | + if not ClientInterceptor._is_intercepted: |
| 53 | + ClientInterceptor._is_intercepted = True |
| 54 | + return intercept_channel(channel, ClientInterceptor()) |
| 55 | + else: |
| 56 | + return channel |
| 57 | + |
| 58 | + return patched_channel |
| 59 | + |
| 60 | + |
| 61 | +def _wrap_intercept_channel(func: Callable[P, Channel]) -> Callable[P, Channel]: |
| 62 | + @wraps(func) |
| 63 | + def patched_intercept_channel( |
| 64 | + channel: Channel, *interceptors: grpc.ServerInterceptor |
| 65 | + ) -> Channel: |
| 66 | + if ClientInterceptor._is_intercepted: |
| 67 | + interceptors = tuple( |
| 68 | + [ |
| 69 | + interceptor |
| 70 | + for interceptor in interceptors |
| 71 | + if not isinstance(interceptor, ClientInterceptor) |
| 72 | + ] |
| 73 | + ) |
| 74 | + else: |
| 75 | + interceptors = interceptors |
| 76 | + return intercept_channel(channel, *interceptors) |
| 77 | + |
| 78 | + return patched_intercept_channel # type: ignore |
| 79 | + |
| 80 | + |
| 81 | +def _wrap_channel_async(func: Callable[P, AsyncChannel]) -> Callable[P, AsyncChannel]: |
| 82 | + "Wrapper for asynchronous secure and insecure channel." |
| 83 | + |
| 84 | + @wraps(func) |
| 85 | + def patched_channel( |
| 86 | + *args: P.args, |
| 87 | + interceptors: Optional[Sequence[grpc.aio.ClientInterceptor]] = None, |
| 88 | + **kwargs: P.kwargs, |
| 89 | + ) -> Channel: |
| 90 | + sentry_interceptors = [ |
| 91 | + AsyncUnaryUnaryClientInterceptor(), |
| 92 | + AsyncUnaryStreamClientIntercetor(), |
| 93 | + ] |
| 94 | + interceptors = [*sentry_interceptors, *(interceptors or [])] |
| 95 | + return func(*args, interceptors=interceptors, **kwargs) # type: ignore |
| 96 | + |
| 97 | + return patched_channel # type: ignore |
| 98 | + |
| 99 | + |
| 100 | +def _wrap_sync_server(func: Callable[P, Server]) -> Callable[P, Server]: |
| 101 | + """Wrapper for synchronous server.""" |
| 102 | + |
| 103 | + @wraps(func) |
| 104 | + def patched_server( |
| 105 | + *args: P.args, |
| 106 | + interceptors: Optional[Sequence[grpc.ServerInterceptor]] = None, |
| 107 | + **kwargs: P.kwargs, |
| 108 | + ) -> Server: |
| 109 | + interceptors = [ |
| 110 | + interceptor |
| 111 | + for interceptor in interceptors or [] |
| 112 | + if not isinstance(interceptor, ServerInterceptor) |
| 113 | + ] |
| 114 | + server_interceptor = ServerInterceptor() |
| 115 | + interceptors = [server_interceptor, *(interceptors or [])] |
| 116 | + return func(*args, interceptors=interceptors, **kwargs) # type: ignore |
| 117 | + |
| 118 | + return patched_server # type: ignore |
| 119 | + |
| 120 | + |
| 121 | +def _wrap_async_server(func: Callable[P, AsyncServer]) -> Callable[P, AsyncServer]: |
| 122 | + """Wrapper for asynchronous server.""" |
| 123 | + |
| 124 | + @wraps(func) |
| 125 | + def patched_aio_server( |
| 126 | + *args: P.args, |
| 127 | + interceptors: Optional[Sequence[grpc.ServerInterceptor]] = None, |
| 128 | + **kwargs: P.kwargs, |
| 129 | + ) -> Server: |
| 130 | + server_interceptor = AsyncServerInterceptor() |
| 131 | + interceptors = [server_interceptor, *(interceptors or [])] |
| 132 | + return func(*args, interceptors=interceptors, **kwargs) # type: ignore |
| 133 | + |
| 134 | + return patched_aio_server # type: ignore |
| 135 | + |
| 136 | + |
| 137 | +class GRPCIntegration(Integration): |
| 138 | + identifier = "grpc" |
| 139 | + |
| 140 | + @staticmethod |
| 141 | + def setup_once() -> None: |
| 142 | + import grpc |
| 143 | + |
| 144 | + grpc.insecure_channel = _wrap_channel_sync(grpc.insecure_channel) |
| 145 | + grpc.secure_channel = _wrap_channel_sync(grpc.secure_channel) |
| 146 | + grpc.intercept_channel = _wrap_intercept_channel(grpc.intercept_channel) |
| 147 | + |
| 148 | + grpc.aio.insecure_channel = _wrap_channel_async(grpc.aio.insecure_channel) |
| 149 | + grpc.aio.secure_channel = _wrap_channel_async(grpc.aio.secure_channel) |
| 150 | + |
| 151 | + grpc.server = _wrap_sync_server(grpc.server) |
| 152 | + grpc.aio.server = _wrap_async_server(grpc.aio.server) |
0 commit comments