1919from newrelic .api .background_task import BackgroundTask
2020from newrelic .api .message_trace import MessageTrace
2121from newrelic .api .transaction import current_transaction
22+ from newrelic .common .async_proxy import TransactionContext , async_proxy
2223from newrelic .common .object_wrapper import FunctionWrapper , wrap_object
23- from newrelic .common .async_proxy import async_proxy , TransactionContext
2424
2525
2626class MessageTransaction (BackgroundTask ):
27-
28- def __init__ (self , library , destination_type ,
29- destination_name , application , routing_key = None ,
30- exchange_type = None , headers = None , queue_name = None , reply_to = None ,
31- correlation_id = None , source = None ):
32-
33- name , group = self .get_transaction_name (library , destination_type ,
34- destination_name )
35-
36- super (MessageTransaction , self ).__init__ (application , name ,
37- group = group , source = source )
27+ def __init__ (
28+ self ,
29+ library ,
30+ destination_type ,
31+ destination_name ,
32+ application ,
33+ routing_key = None ,
34+ exchange_type = None ,
35+ headers = None ,
36+ queue_name = None ,
37+ reply_to = None ,
38+ correlation_id = None ,
39+ transport_type = "AMQP" ,
40+ source = None ,
41+ ):
42+
43+ name , group = self .get_transaction_name (library , destination_type , destination_name )
44+
45+ super (MessageTransaction , self ).__init__ (application , name , group = group , source = source )
3846
3947 self .headers = headers
4048
4149 if headers is not None and self .settings is not None :
4250 if self .settings .distributed_tracing .enabled :
43- self .accept_distributed_trace_headers (
44- headers , transport_type = 'AMQP' )
51+ self .accept_distributed_trace_headers (headers , transport_type = transport_type )
4552 elif self .settings .cross_application_tracer .enabled :
4653 self ._process_incoming_cat_headers (
47- headers .pop (MessageTrace .cat_id_key , None ),
48- headers .pop (MessageTrace .cat_transaction_key , None )
54+ headers .pop (MessageTrace .cat_id_key , None ), headers .pop (MessageTrace .cat_transaction_key , None )
4955 )
5056
5157 self .routing_key = routing_key
@@ -56,37 +62,45 @@ def __init__(self, library, destination_type,
5662
5763 @staticmethod
5864 def get_transaction_name (library , destination_type , destination_name ):
59- group = ' Message/%s/%s' % (library , destination_type )
60- name = ' Named/%s' % destination_name
65+ group = " Message/%s/%s" % (library , destination_type )
66+ name = " Named/%s" % destination_name
6167 return name , group
6268
6369 def _update_agent_attributes (self ):
6470 ms_attrs = self ._agent_attributes
6571
6672 if self .exchange_type is not None :
67- ms_attrs [' message.exchangeType' ] = self .exchange_type
73+ ms_attrs [" message.exchangeType" ] = self .exchange_type
6874 if self .queue_name is not None :
69- ms_attrs [' message.queueName' ] = self .queue_name
75+ ms_attrs [" message.queueName" ] = self .queue_name
7076 if self .reply_to is not None :
71- ms_attrs [' message.replyTo' ] = self .reply_to
77+ ms_attrs [" message.replyTo" ] = self .reply_to
7278 if self .correlation_id is not None :
73- ms_attrs [' message.correlationId' ] = self .correlation_id
79+ ms_attrs [" message.correlationId" ] = self .correlation_id
7480 if self .headers :
7581 for k , v in self .headers .items ():
76- new_key = ' message.headers.%s' % k
82+ new_key = " message.headers.%s" % k
7783 new_val = str (v )
7884 ms_attrs [new_key ] = new_val
7985 if self .routing_key is not None :
80- ms_attrs [' message.routingKey' ] = self .routing_key
86+ ms_attrs [" message.routingKey" ] = self .routing_key
8187
8288 super (MessageTransaction , self )._update_agent_attributes ()
8389
8490
85- def MessageTransactionWrapper (wrapped , library , destination_type ,
86- destination_name , application = None , routing_key = None ,
87- exchange_type = None , headers = None , queue_name = None , reply_to = None ,
88- correlation_id = None ):
89-
91+ def MessageTransactionWrapper (
92+ wrapped ,
93+ library ,
94+ destination_type ,
95+ destination_name ,
96+ application = None ,
97+ routing_key = None ,
98+ exchange_type = None ,
99+ headers = None ,
100+ queue_name = None ,
101+ reply_to = None ,
102+ correlation_id = None ,
103+ ):
90104 def wrapper (wrapped , instance , args , kwargs ):
91105 if callable (library ):
92106 if instance is not None :
@@ -173,9 +187,8 @@ def create_transaction(transaction):
173187 if not transaction .background_task :
174188 transaction .background_task = True
175189 transaction .set_transaction_name (
176- * MessageTransaction .get_transaction_name (
177- _library , _destination_type ,
178- _destination_name ))
190+ * MessageTransaction .get_transaction_name (_library , _destination_type , _destination_name )
191+ )
179192
180193 return None
181194
@@ -233,22 +246,61 @@ def create_transaction(transaction):
233246 return FunctionWrapper (wrapped , wrapper )
234247
235248
236- def message_transaction (library , destination_type , destination_name ,
237- application = None , routing_key = None , exchange_type = None , headers = None ,
238- queue_name = None , reply_to = None , correlation_id = None ):
239- return functools .partial (MessageTransactionWrapper ,
240- library = library , destination_type = destination_type ,
241- destination_name = destination_name , application = application ,
242- routing_key = routing_key , exchange_type = exchange_type ,
243- headers = headers , queue_name = queue_name , reply_to = reply_to ,
244- correlation_id = correlation_id )
245-
246-
247- def wrap_message_transaction (module , object_path , library , destination_type ,
248- destination_name , application = None , routing_key = None ,
249- exchange_type = None , headers = None , queue_name = None , reply_to = None ,
250- correlation_id = None ):
251- wrap_object (module , object_path , MessageTransactionWrapper ,
252- (library , destination_type , destination_name , application ,
253- routing_key , exchange_type , headers , queue_name , reply_to ,
254- correlation_id ))
249+ def message_transaction (
250+ library ,
251+ destination_type ,
252+ destination_name ,
253+ application = None ,
254+ routing_key = None ,
255+ exchange_type = None ,
256+ headers = None ,
257+ queue_name = None ,
258+ reply_to = None ,
259+ correlation_id = None ,
260+ ):
261+ return functools .partial (
262+ MessageTransactionWrapper ,
263+ library = library ,
264+ destination_type = destination_type ,
265+ destination_name = destination_name ,
266+ application = application ,
267+ routing_key = routing_key ,
268+ exchange_type = exchange_type ,
269+ headers = headers ,
270+ queue_name = queue_name ,
271+ reply_to = reply_to ,
272+ correlation_id = correlation_id ,
273+ )
274+
275+
276+ def wrap_message_transaction (
277+ module ,
278+ object_path ,
279+ library ,
280+ destination_type ,
281+ destination_name ,
282+ application = None ,
283+ routing_key = None ,
284+ exchange_type = None ,
285+ headers = None ,
286+ queue_name = None ,
287+ reply_to = None ,
288+ correlation_id = None ,
289+ ):
290+ wrap_object (
291+ module ,
292+ object_path ,
293+ MessageTransactionWrapper ,
294+ (
295+ library ,
296+ destination_type ,
297+ destination_name ,
298+ application ,
299+ routing_key ,
300+ exchange_type ,
301+ headers ,
302+ queue_name ,
303+ reply_to ,
304+ correlation_id ,
305+ ),
306+ )
0 commit comments