Skip to content

Commit 385c668

Browse files
authored
fix(grpc): Fix AttributeError when instrumenting with OTel (#4405)
OTel also wraps `grpc.aio.server`, but expects the `interceptors` arg to be a list, while Sentry [turns it into a tuple](https://github.com/getsentry/sentry-python/blob/94414cdcdce2a311355967ee7b43e7035cf954ce/sentry_sdk/integrations/grpc/__init__.py#L130). Non-tuple sequences are actually only supported [starting](grpc/grpc@9699580) in grpc 1.42.0. So for older versions we need to still use a tuple. Fixes #4389
1 parent 9582a48 commit 385c668

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

sentry_sdk/integrations/grpc/__init__.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from grpc.aio import Server as AsyncServer
77

88
from sentry_sdk.integrations import Integration
9+
from sentry_sdk.utils import parse_version
910

1011
from .client import ClientInterceptor
1112
from .server import ServerInterceptor
@@ -41,6 +42,8 @@ def __getitem__(self, _):
4142

4243
P = ParamSpec("P")
4344

45+
GRPC_VERSION = parse_version(grpc.__version__)
46+
4447

4548
def _wrap_channel_sync(func: Callable[P, Channel]) -> Callable[P, Channel]:
4649
"Wrapper for synchronous secure and insecure channel."
@@ -127,7 +130,21 @@ def patched_aio_server( # type: ignore
127130
**kwargs: P.kwargs,
128131
) -> Server:
129132
server_interceptor = AsyncServerInterceptor()
130-
interceptors = (server_interceptor, *(interceptors or []))
133+
interceptors = [
134+
server_interceptor,
135+
*(interceptors or []),
136+
] # type: Sequence[grpc.ServerInterceptor]
137+
138+
try:
139+
# We prefer interceptors as a list because of compatibility with
140+
# opentelemetry https://github.com/getsentry/sentry-python/issues/4389
141+
# However, prior to grpc 1.42.0, only tuples were accepted, so we
142+
# have no choice there.
143+
if GRPC_VERSION is not None and GRPC_VERSION < (1, 42, 0):
144+
interceptors = tuple(interceptors)
145+
except Exception:
146+
pass
147+
131148
return func(*args, interceptors=interceptors, **kwargs) # type: ignore
132149

133150
return patched_aio_server # type: ignore

0 commit comments

Comments
 (0)