1616import time
1717
1818from newrelic .api .external_trace import ExternalTrace
19- from newrelic .api .web_transaction import WebTransactionWrapper
20- from newrelic .api .transaction import current_transaction
2119from newrelic .api .time_trace import notice_error
22- from newrelic .common .object_wrapper import wrap_function_wrapper
20+ from newrelic .api .transaction import current_transaction
21+ from newrelic .api .web_transaction import WebTransactionWrapper
2322from newrelic .common .object_names import callable_name
23+ from newrelic .common .object_wrapper import wrap_function_wrapper
2424
2525
2626def _get_uri_method (instance , * args , ** kwargs ):
27- target = instance ._channel .target ().decode (' utf-8' )
28- method = instance ._method .decode (' utf-8' ).lstrip ('/' )
29- uri = ' grpc://%s/%s' % (target , method )
27+ target = instance ._channel .target ().decode (" utf-8" ). lstrip ( "dns:///" )
28+ method = instance ._method .decode (" utf-8" ).lstrip ("/" )
29+ uri = " grpc://%s/%s" % (target , method )
3030 return (uri , method )
3131
3232
33- def _prepare_request (
34- transaction , guid , request ,
35- timeout = None , metadata = None , * args , ** kwargs ):
33+ def _prepare_request (transaction , guid , request , timeout = None , metadata = None , * args , ** kwargs ):
3634 metadata = metadata and list (metadata ) or []
3735 dt_metadata = transaction ._create_distributed_trace_data_with_guid (guid )
38- metadata .extend (
39- transaction ._generate_distributed_trace_headers (dt_metadata )
40- )
36+ metadata .extend (transaction ._generate_distributed_trace_headers (dt_metadata ))
4137 args = (request , timeout , metadata ) + args
4238 return args , kwargs
4339
4440
45- def _prepare_request_stream (
46- transaction , guid , request_iterator , * args , ** kwargs ):
47- return _prepare_request (
48- transaction , guid , request_iterator , * args , ** kwargs )
41+ def _prepare_request_stream (transaction , guid , request_iterator , * args , ** kwargs ):
42+ return _prepare_request (transaction , guid , request_iterator , * args , ** kwargs )
4943
5044
5145def wrap_call (module , object_path , prepare ):
@@ -56,7 +50,7 @@ def _call_wrapper(wrapped, instance, args, kwargs):
5650 return wrapped (* args , ** kwargs )
5751
5852 uri , method = _get_uri_method (instance )
59- with ExternalTrace (' gRPC' , uri , method , source = wrapped ):
53+ with ExternalTrace (" gRPC" , uri , method , source = wrapped ):
6054 args , kwargs = prepare (transaction , None , * args , ** kwargs )
6155 return wrapped (* args , ** kwargs )
6256
@@ -70,13 +64,13 @@ def _future_wrapper(wrapped, instance, args, kwargs):
7064 if transaction is None :
7165 return wrapped (* args , ** kwargs )
7266
73- guid = ' %016x' % random .getrandbits (64 )
67+ guid = " %016x" % random .getrandbits (64 )
7468 uri , method = _get_uri_method (instance )
7569
7670 args , kwargs = prepare (transaction , guid , * args , ** kwargs )
7771 future = wrapped (* args , ** kwargs )
7872 future ._nr_guid = guid
79- future ._nr_args = {"library" : ' gRPC' , "url" : uri , "method" : method , "source" : wrapped }
73+ future ._nr_args = {"library" : " gRPC" , "url" : uri , "method" : method , "source" : wrapped }
8074 future ._nr_start_time = time .time ()
8175
8276 # In non-streaming responses, result is typically called instead of
@@ -89,16 +83,16 @@ def _future_wrapper(wrapped, instance, args, kwargs):
8983
9084
9185def wrap_next (_wrapped , _instance , _args , _kwargs ):
92- _nr_args = getattr (_instance , ' _nr_args' , None )
86+ _nr_args = getattr (_instance , " _nr_args" , None )
9387 if not _nr_args :
9488 return _wrapped (* _args , ** _kwargs )
9589
9690 try :
9791 return _wrapped (* _args , ** _kwargs )
9892 except Exception :
99- delattr (_instance , ' _nr_args' )
100- _nr_start_time = getattr (_instance , ' _nr_start_time' , 0.0 )
101- _nr_guid = getattr (_instance , ' _nr_guid' , None )
93+ delattr (_instance , " _nr_args" )
94+ _nr_start_time = getattr (_instance , " _nr_start_time" , 0.0 )
95+ _nr_guid = getattr (_instance , " _nr_guid" , None )
10296
10397 with ExternalTrace (** _nr_args ) as t :
10498 t .start_time = _nr_start_time or t .start_time
@@ -107,12 +101,12 @@ def wrap_next(_wrapped, _instance, _args, _kwargs):
107101
108102
109103def wrap_result (_wrapped , _instance , _args , _kwargs ):
110- _nr_args = getattr (_instance , ' _nr_args' , None )
104+ _nr_args = getattr (_instance , " _nr_args" , None )
111105 if not _nr_args :
112106 return _wrapped (* _args , ** _kwargs )
113- delattr (_instance , ' _nr_args' )
114- _nr_start_time = getattr (_instance , ' _nr_start_time' , 0.0 )
115- _nr_guid = getattr (_instance , ' _nr_guid' , None )
107+ delattr (_instance , " _nr_args" )
108+ _nr_start_time = getattr (_instance , " _nr_start_time" , 0.0 )
109+ _nr_guid = getattr (_instance , " _nr_guid" , None )
116110
117111 try :
118112 result = _wrapped (* _args , ** _kwargs )
@@ -136,31 +130,22 @@ def grpc_web_transaction(wrapped, instance, args, kwargs):
136130 rpc_event , behavior = _bind_transaction_args (* args , ** kwargs )
137131 behavior_name = callable_name (behavior )
138132
139- call_details = (
140- getattr (rpc_event , 'call_details' , None ) or
141- getattr (rpc_event , 'request_call_details' , None ))
133+ call_details = getattr (rpc_event , "call_details" , None ) or getattr (rpc_event , "request_call_details" , None )
142134
143- metadata = (
144- getattr (rpc_event , 'invocation_metadata' , None ) or
145- getattr (rpc_event , 'request_metadata' , None ))
135+ metadata = getattr (rpc_event , "invocation_metadata" , None ) or getattr (rpc_event , "request_metadata" , None )
146136
147137 host = port = None
148138 if call_details :
149139 try :
150- host , port = call_details .host .split (b':' , 1 )
140+ host , port = call_details .host .split (b":" , 1 )
151141 except Exception :
152142 pass
153143
154144 request_path = call_details .method
155145
156146 return WebTransactionWrapper (
157- wrapped ,
158- name = behavior_name ,
159- request_path = request_path ,
160- host = host ,
161- port = port ,
162- headers = metadata ,
163- source = behavior )(* args , ** kwargs )
147+ wrapped , name = behavior_name , request_path = request_path , host = host , port = port , headers = metadata , source = behavior
148+ )(* args , ** kwargs )
164149
165150
166151def _trailing_metadata (state , * args , ** kwargs ):
@@ -185,44 +170,26 @@ def _nr_wrap_abort(wrapped, instance, args, kwargs):
185170
186171
187172def instrument_grpc__channel (module ):
188- wrap_call (module , '_UnaryUnaryMultiCallable.__call__' ,
189- _prepare_request )
190- wrap_call (module , '_UnaryUnaryMultiCallable.with_call' ,
191- _prepare_request )
192- wrap_future (module , '_UnaryUnaryMultiCallable.future' ,
193- _prepare_request )
194- wrap_future (module , '_UnaryStreamMultiCallable.__call__' ,
195- _prepare_request )
196- wrap_call (module , '_StreamUnaryMultiCallable.__call__' ,
197- _prepare_request_stream )
198- wrap_call (module , '_StreamUnaryMultiCallable.with_call' ,
199- _prepare_request_stream )
200- wrap_future (module , '_StreamUnaryMultiCallable.future' ,
201- _prepare_request_stream )
202- wrap_future (module , '_StreamStreamMultiCallable.__call__' ,
203- _prepare_request_stream )
204- if hasattr (module , '_MultiThreadedRendezvous' ):
205- wrap_function_wrapper (module , '_MultiThreadedRendezvous.result' ,
206- wrap_result )
207- wrap_function_wrapper (module , '_MultiThreadedRendezvous._next' ,
208- wrap_next )
173+ wrap_call (module , "_UnaryUnaryMultiCallable.__call__" , _prepare_request )
174+ wrap_call (module , "_UnaryUnaryMultiCallable.with_call" , _prepare_request )
175+ wrap_future (module , "_UnaryUnaryMultiCallable.future" , _prepare_request )
176+ wrap_future (module , "_UnaryStreamMultiCallable.__call__" , _prepare_request )
177+ wrap_call (module , "_StreamUnaryMultiCallable.__call__" , _prepare_request_stream )
178+ wrap_call (module , "_StreamUnaryMultiCallable.with_call" , _prepare_request_stream )
179+ wrap_future (module , "_StreamUnaryMultiCallable.future" , _prepare_request_stream )
180+ wrap_future (module , "_StreamStreamMultiCallable.__call__" , _prepare_request_stream )
181+ if hasattr (module , "_MultiThreadedRendezvous" ):
182+ wrap_function_wrapper (module , "_MultiThreadedRendezvous.result" , wrap_result )
183+ wrap_function_wrapper (module , "_MultiThreadedRendezvous._next" , wrap_next )
209184 else :
210- wrap_function_wrapper (module , '_Rendezvous.result' ,
211- wrap_result )
212- wrap_function_wrapper (module , '_Rendezvous._next' ,
213- wrap_next )
214- wrap_function_wrapper (module , '_Rendezvous.cancel' ,
215- wrap_result )
185+ wrap_function_wrapper (module , "_Rendezvous.result" , wrap_result )
186+ wrap_function_wrapper (module , "_Rendezvous._next" , wrap_next )
187+ wrap_function_wrapper (module , "_Rendezvous.cancel" , wrap_result )
216188
217189
218190def instrument_grpc_server (module ):
219- wrap_function_wrapper (module , '_unary_response_in_pool' ,
220- grpc_web_transaction )
221- wrap_function_wrapper (module , '_stream_response_in_pool' ,
222- grpc_web_transaction )
223- wrap_function_wrapper (module , '_completion_code' ,
224- _nr_wrap_status_code )
225- wrap_function_wrapper (module , '_abortion_code' ,
226- _nr_wrap_status_code )
227- wrap_function_wrapper (module , '_abort' ,
228- _nr_wrap_abort )
191+ wrap_function_wrapper (module , "_unary_response_in_pool" , grpc_web_transaction )
192+ wrap_function_wrapper (module , "_stream_response_in_pool" , grpc_web_transaction )
193+ wrap_function_wrapper (module , "_completion_code" , _nr_wrap_status_code )
194+ wrap_function_wrapper (module , "_abortion_code" , _nr_wrap_status_code )
195+ wrap_function_wrapper (module , "_abort" , _nr_wrap_abort )
0 commit comments