2626
2727CLIENT_ATTRIBUTES_WARNING_LOG_MSG = "Exception occurred in PyZeebe instrumentation: Failed to add client attributes."
2828
29+
2930# Adds client method params as txn or span attributes
3031def _add_client_input_attributes (method_name , txn , args , kwargs ):
3132 try :
3233 if method_name in ("run_process" , "run_process_with_result" ):
3334 bpmn_id = kwargs .get ("bpmn_process_id" , args [0 ] if args else None )
3435 if bpmn_id :
3536 txn ._add_agent_attribute ("zeebe.client.bpmnProcessId" , bpmn_id )
36- #add_attr("zeebe.client.bpmnProcessId", bpmn_id)
37+ # add_attr("zeebe.client.bpmnProcessId", bpmn_id)
3738 elif method_name == "publish_message" :
3839 msg_name = kwargs .get ("name" , args [0 ] if args else None )
3940 if msg_name :
4041 txn ._add_agent_attribute ("zeebe.client.messageName" , msg_name )
41- #add_attr("zeebe.client.messageName", msg_name)
42+ # add_attr("zeebe.client.messageName", msg_name)
4243 correlation_key = kwargs .get ("correlation_key" , args [1 ] if args and len (args ) > 1 else None )
4344 if correlation_key :
4445 txn ._add_agent_attribute ("zeebe.client.correlationKey" , correlation_key )
45- #add_attr("zeebe.client.correlationKey", correlation_key)
46+ # add_attr("zeebe.client.correlationKey", correlation_key)
4647 message_id = kwargs .get ("message_id" )
4748 if message_id and len (args ) > 4 :
4849 message_id = args [4 ]
4950 if message_id :
5051 txn ._add_agent_attribute ("zeebe.client.messageId" , message_id )
51- #add_attr("zeebe.client.messageId", message_id)
52+ # add_attr("zeebe.client.messageId", message_id)
5253 elif method_name == "deploy_resource" :
5354 resources = list (args )
5455 if len (resources ) == 1 and isinstance (resources [0 ], (list , tuple )):
5556 resources = list (resources [0 ])
5657 if resources :
5758 txn ._add_agent_attribute ("zeebe.client.resourceCount" , len (resources ))
58- #add_attr("zeebe.client.resourceCount", len(resources))
59+ # add_attr("zeebe.client.resourceCount", len(resources))
5960 if len (resources ) == 1 :
6061 try :
6162 txn ._add_agent_attribute ("zeebe.client.resourceFile" , str (resources [0 ]))
62- #add_attr("zeebe.client.resourceFile", str(resources[0]))
63+ # add_attr("zeebe.client.resourceFile", str(resources[0]))
6364 except Exception :
6465 txn ._add_agent_attribute ("zeebe.client.resourceFile" , str (resources [0 ]))
65- #add_attr("zeebe.client.resourceFile", repr(resources[0]))
66+ # add_attr("zeebe.client.resourceFile", repr(resources[0]))
6667 except Exception :
6768 _logger .warning (CLIENT_ATTRIBUTES_WARNING_LOG_MSG , exc_info = True )
6869
70+
6971# Async wrapper that instruments router/worker annotations`
7072async def _nr_wrapper_execute_one_job (wrapped , instance , args , kwargs ):
7173 job = args [0 ] if args else kwargs .get ("job" )
7274 process_id = getattr (job , "bpmn_process_id" , None ) or "UnknownProcess"
7375 task_type = getattr (job , "type" , None ) or "UnknownType"
7476 txn_name = f"{ process_id } /{ task_type } "
7577
76- with BackgroundTask (application_instance (), txn_name , group = "ZeebeTask" ):
78+ with BackgroundTask (application_instance (), txn_name , group = "ZeebeTask" ):
7779 if job is not None :
7880 if hasattr (job , "key" ):
7981 add_custom_attribute ("zeebe.job.key" , job .key )
@@ -89,6 +91,7 @@ async def _nr_wrapper_execute_one_job(wrapped, instance, args, kwargs):
8991 result = await wrapped (* args , ** kwargs )
9092 return result
9193
94+
9295# Async wrapper that instruments a ZeebeClient method.
9396def _nr_client_wrapper (method_name ):
9497 async def wrapper (wrapped , instance , args , kwargs ):
@@ -108,34 +111,23 @@ async def wrapper(wrapped, instance, args, kwargs):
108111 result = await wrapped (* args , ** kwargs )
109112 finally :
110113 created_txn .__exit__ (None , None , None )
111-
114+
112115 return result
113116
114117 return wrapper
115118
119+
116120# Instrument JobExecutor.execute_one_job to create a background transaction per job (invoked from @router.task or @worker.task annotations)
117121def instrument_pyzeebe_worker_job_executor (module ):
118122 if hasattr (module , "JobExecutor" ):
119- wrap_function_wrapper (
120- module ,
121- "JobExecutor.execute_one_job" ,
122- _nr_wrapper_execute_one_job
123- )
123+ wrap_function_wrapper (module , "JobExecutor.execute_one_job" , _nr_wrapper_execute_one_job )
124+
124125
125126# Instrument ZeebeClient methods to trace client calls.
126127def instrument_pyzeebe_client_client (module ):
127- target_methods = (
128- "run_process" ,
129- "run_process_with_result" ,
130- "deploy_resource" ,
131- "publish_message" ,
132- )
128+ target_methods = ("run_process" , "run_process_with_result" , "deploy_resource" , "publish_message" )
133129
134130 for method_name in target_methods :
135131 if hasattr (module , "ZeebeClient" ):
136132 if hasattr (module .ZeebeClient , method_name ):
137- wrap_function_wrapper (
138- module ,
139- f"ZeebeClient.{ method_name } " ,
140- _nr_client_wrapper (method_name )
141- )
133+ wrap_function_wrapper (module , f"ZeebeClient.{ method_name } " , _nr_client_wrapper (method_name ))
0 commit comments