11from typing import Callable , Union , AsyncIterable , Any
22
33import sentry_sdk
4- from sentry_sdk .consts import OP
4+ from sentry_sdk .consts import OP , SPANDATA
55from sentry_sdk .integrations import DidNotEnable
66from sentry_sdk .integrations .grpc .consts import SPAN_ORIGIN
7+ from sentry_sdk .tracing_utils import has_span_streaming_enabled
78
89try :
910 from grpc .aio import (
@@ -49,23 +50,47 @@ async def intercept_unary_unary(
4950 ) -> "Union[UnaryUnaryCall, Message]" :
5051 method = client_call_details .method
5152
52- with sentry_sdk .start_span (
53- op = OP .GRPC_CLIENT ,
54- name = "unary unary call to %s" % method .decode (),
55- origin = SPAN_ORIGIN ,
56- ) as span :
57- span .set_data ("type" , "unary unary" )
58- span .set_data ("method" , method )
59-
60- client_call_details = self ._update_client_call_details_metadata_from_scope (
61- client_call_details
62- )
63-
64- response = await continuation (client_call_details , request )
65- status_code = await response .code ()
66- span .set_data ("code" , status_code .name )
67-
68- return response
53+ span_streaming = has_span_streaming_enabled (sentry_sdk .get_client ().options )
54+ if span_streaming :
55+ with sentry_sdk .traces .start_span (
56+ name = "unary unary call to %s" % method .decode (),
57+ attributes = {
58+ "sentry.op" : OP .GRPC_CLIENT ,
59+ "sentry.origin" : SPAN_ORIGIN ,
60+ SPANDATA .RPC_METHOD : method .decode (),
61+ },
62+ ) as span :
63+ client_call_details = (
64+ self ._update_client_call_details_metadata_from_scope (
65+ client_call_details
66+ )
67+ )
68+
69+ response = await continuation (client_call_details , request )
70+ status_code = await response .code ()
71+ span .set_attribute (SPANDATA .RPC_RESPONSE_STATUS_CODE , status_code .name )
72+
73+ return response
74+ else :
75+ with sentry_sdk .start_span (
76+ op = OP .GRPC_CLIENT ,
77+ name = "unary unary call to %s" % method .decode (),
78+ origin = SPAN_ORIGIN ,
79+ ) as span :
80+ span .set_data ("type" , "unary unary" )
81+ span .set_data ("method" , method )
82+
83+ client_call_details = (
84+ self ._update_client_call_details_metadata_from_scope (
85+ client_call_details
86+ )
87+ )
88+
89+ response = await continuation (client_call_details , request )
90+ status_code = await response .code ()
91+ span .set_data ("code" , status_code .name )
92+
93+ return response
6994
7095
7196class SentryUnaryStreamClientInterceptor (
@@ -80,20 +105,42 @@ async def intercept_unary_stream(
80105 ) -> "Union[AsyncIterable[Any], UnaryStreamCall]" :
81106 method = client_call_details .method
82107
83- with sentry_sdk .start_span (
84- op = OP .GRPC_CLIENT ,
85- name = "unary stream call to %s" % method .decode (),
86- origin = SPAN_ORIGIN ,
87- ) as span :
88- span .set_data ("type" , "unary stream" )
89- span .set_data ("method" , method )
90-
91- client_call_details = self ._update_client_call_details_metadata_from_scope (
92- client_call_details
93- )
94-
95- response = await continuation (client_call_details , request )
96- # status_code = await response.code()
97- # span.set_data("code", status_code)
98-
99- return response
108+ span_streaming = has_span_streaming_enabled (sentry_sdk .get_client ().options )
109+ if span_streaming :
110+ with sentry_sdk .traces .start_span (
111+ name = "unary stream call to %s" % method .decode (),
112+ attributes = {
113+ "sentry.op" : OP .GRPC_CLIENT ,
114+ "sentry.origin" : SPAN_ORIGIN ,
115+ SPANDATA .RPC_METHOD : method .decode (),
116+ },
117+ ) as span :
118+ client_call_details = (
119+ self ._update_client_call_details_metadata_from_scope (
120+ client_call_details
121+ )
122+ )
123+
124+ response = await continuation (client_call_details , request )
125+
126+ return response
127+ else :
128+ with sentry_sdk .start_span (
129+ op = OP .GRPC_CLIENT ,
130+ name = "unary stream call to %s" % method .decode (),
131+ origin = SPAN_ORIGIN ,
132+ ) as span :
133+ span .set_data ("type" , "unary stream" )
134+ span .set_data ("method" , method )
135+
136+ client_call_details = (
137+ self ._update_client_call_details_metadata_from_scope (
138+ client_call_details
139+ )
140+ )
141+
142+ response = await continuation (client_call_details , request )
143+ # status_code = await response.code()
144+ # span.set_data("code", status_code)
145+
146+ return response
0 commit comments