Skip to content

Commit

Permalink
fix(grpc): Return proper metadata object instead of list in… (#3205)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Dec 5, 2024
1 parent 3e43a91 commit 50ad148
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions sentry_sdk/integrations/grpc/aio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
ClientCallDetails,
UnaryUnaryCall,
UnaryStreamCall,
Metadata,
)
from google.protobuf.message import Message

Expand All @@ -19,23 +20,19 @@ class ClientInterceptor:
def _update_client_call_details_metadata_from_scope(
client_call_details: ClientCallDetails,
) -> ClientCallDetails:
metadata = (
list(client_call_details.metadata) if client_call_details.metadata else []
)
if client_call_details.metadata is None:
client_call_details = client_call_details._replace(metadata=Metadata())
elif not isinstance(client_call_details.metadata, Metadata):
# This is a workaround for a GRPC bug, which was fixed in grpcio v1.60.0
# See https://github.com/grpc/grpc/issues/34298.
client_call_details = client_call_details._replace(
metadata=Metadata.from_tuple(client_call_details.metadata)
)
for (
key,
value,
) in sentry_sdk.get_current_scope().iter_trace_propagation_headers():
metadata.append((key, value))

client_call_details = ClientCallDetails(
method=client_call_details.method,
timeout=client_call_details.timeout,
metadata=metadata,
credentials=client_call_details.credentials,
wait_for_ready=client_call_details.wait_for_ready,
)

client_call_details.metadata.add(key, value)
return client_call_details


Expand Down

0 comments on commit 50ad148

Please sign in to comment.