Skip to content

Commit 50ad148

Browse files
fdellekartantonpirkerszokeasaurusrex
authored
fix(grpc): Return proper metadata object instead of list in… (#3205)
* fix(grpc): Return propagate proper metadata object instead of list in client interceptor Fixes #2509 * fix(grpc): Transform metadata into Metadata object in case it's a tuple Up until version 1.65.0 of grpcio, the metadata was not guaranteed to arrive as the type specified in annotations but could be a tuple. To support versions before that we check and transform it here. * docs(grpc): Add comment about workaround --------- Co-authored-by: Anton Pirker <[email protected]> Co-authored-by: Daniel Szoke <[email protected]>
1 parent 3e43a91 commit 50ad148

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

sentry_sdk/integrations/grpc/aio/client.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
ClientCallDetails,
77
UnaryUnaryCall,
88
UnaryStreamCall,
9+
Metadata,
910
)
1011
from google.protobuf.message import Message
1112

@@ -19,23 +20,19 @@ class ClientInterceptor:
1920
def _update_client_call_details_metadata_from_scope(
2021
client_call_details: ClientCallDetails,
2122
) -> ClientCallDetails:
22-
metadata = (
23-
list(client_call_details.metadata) if client_call_details.metadata else []
24-
)
23+
if client_call_details.metadata is None:
24+
client_call_details = client_call_details._replace(metadata=Metadata())
25+
elif not isinstance(client_call_details.metadata, Metadata):
26+
# This is a workaround for a GRPC bug, which was fixed in grpcio v1.60.0
27+
# See https://github.com/grpc/grpc/issues/34298.
28+
client_call_details = client_call_details._replace(
29+
metadata=Metadata.from_tuple(client_call_details.metadata)
30+
)
2531
for (
2632
key,
2733
value,
2834
) in sentry_sdk.get_current_scope().iter_trace_propagation_headers():
29-
metadata.append((key, value))
30-
31-
client_call_details = ClientCallDetails(
32-
method=client_call_details.method,
33-
timeout=client_call_details.timeout,
34-
metadata=metadata,
35-
credentials=client_call_details.credentials,
36-
wait_for_ready=client_call_details.wait_for_ready,
37-
)
38-
35+
client_call_details.metadata.add(key, value)
3936
return client_call_details
4037

4138

0 commit comments

Comments
 (0)