diff --git a/sentry_sdk/integrations/grpc/__init__.py b/sentry_sdk/integrations/grpc/__init__.py index bc4025b8b3..b6641163a9 100644 --- a/sentry_sdk/integrations/grpc/__init__.py +++ b/sentry_sdk/integrations/grpc/__init__.py @@ -1,25 +1,30 @@ from functools import wraps -import grpc -from grpc import Channel, Server, intercept_channel -from grpc.aio import Channel as AsyncChannel -from grpc.aio import Server as AsyncServer - from sentry_sdk.integrations import Integration from sentry_sdk.utils import parse_version +from sentry_sdk.integrations import DidNotEnable from .client import ClientInterceptor from .server import ServerInterceptor -from .aio.server import ServerInterceptor as AsyncServerInterceptor -from .aio.client import ( - SentryUnaryUnaryClientInterceptor as AsyncUnaryUnaryClientInterceptor, -) -from .aio.client import ( - SentryUnaryStreamClientInterceptor as AsyncUnaryStreamClientIntercetor, -) from typing import TYPE_CHECKING, Any, Optional, Sequence +try: + import grpc + from grpc import Channel, Server, intercept_channel + from grpc.aio import Channel as AsyncChannel + from grpc.aio import Server as AsyncServer + + from .aio.server import ServerInterceptor as AsyncServerInterceptor + from .aio.client import ( + SentryUnaryUnaryClientInterceptor as AsyncUnaryUnaryClientInterceptor, + ) + from .aio.client import ( + SentryUnaryStreamClientInterceptor as AsyncUnaryStreamClientIntercetor, + ) +except ImportError: + raise DidNotEnable("grpcio is not installed.") + # Hack to get new Python features working in older versions # without introducing a hard dependency on `typing_extensions` # from: https://stackoverflow.com/a/71944042/300572 diff --git a/sentry_sdk/integrations/grpc/aio/client.py b/sentry_sdk/integrations/grpc/aio/client.py index 9ff27d1824..2edad83aff 100644 --- a/sentry_sdk/integrations/grpc/aio/client.py +++ b/sentry_sdk/integrations/grpc/aio/client.py @@ -1,19 +1,23 @@ from typing import Callable, Union, AsyncIterable, Any -from grpc.aio import ( - UnaryUnaryClientInterceptor, - UnaryStreamClientInterceptor, - ClientCallDetails, - UnaryUnaryCall, - UnaryStreamCall, - Metadata, -) -from google.protobuf.message import Message - import sentry_sdk from sentry_sdk.consts import OP +from sentry_sdk.integrations import DidNotEnable from sentry_sdk.integrations.grpc.consts import SPAN_ORIGIN +try: + from grpc.aio import ( + UnaryUnaryClientInterceptor, + UnaryStreamClientInterceptor, + ClientCallDetails, + UnaryUnaryCall, + UnaryStreamCall, + Metadata, + ) + from google.protobuf.message import Message +except ImportError: + raise DidNotEnable("grpcio is not installed") + class ClientInterceptor: @staticmethod diff --git a/tests/test_shadowed_module.py b/tests/test_shadowed_module.py index 34bb7917f4..685a5dceaf 100644 --- a/tests/test_shadowed_module.py +++ b/tests/test_shadowed_module.py @@ -30,7 +30,6 @@ def pytest_generate_tests(metafunc): submodule_names - { "clickhouse_driver", - "grpc", "litellm", "opentelemetry", "pure_eval",