3737
3838from .forward_controller import ForwardControllersPool
3939import reachy2_monitoring as rm
40+ import prometheus_client as pc
4041
4142class TracedCommands :
4243 def __init__ (self , traceparent = "" ) -> None :
4344 self .traceparent = traceparent
4445 self .commands = defaultdict (dict )
46+ self .timestamp = 0
4547
4648 def clear (self ):
4749 self .traceparent = ""
4850 self .commands .clear ()
51+ self .timestamp = 0
4952
5053
5154
@@ -55,6 +58,9 @@ def __init__(self, node_name, controllers_file):
5558 self .logger = self .get_logger ()
5659 self .tracer = rm .tracer (node_name )
5760 self .on_dynamic_joint_commands_counter = 0
61+ pc .start_http_server (10002 )
62+ self .sum_joint_states = pc .Summary ("dynamic_state_router_on_dynamic_joint_states_time" ,
63+ "Time spent during on_dynamic_joint_states callback" )
5864
5965 self .freq , self .forward_controllers = ForwardControllersPool .parse (
6066 self .logger , controllers_file
@@ -189,9 +195,9 @@ def on_dynamic_joint_commands(self, command: DynamicJointState):
189195 })
190196 with self .pub_lock :
191197 rm .travel_span ("wait_for_pub_lock" ,
192- start_time = start_wait ,
193- tracer = self .tracer ,
194- )
198+ start_time = start_wait ,
199+ tracer = self .tracer ,
200+ )
195201 self .requested_commands .traceparent = rm .traceparent ()
196202 for name , iv in zip (command .joint_names , command .interface_values ):
197203 if name not in self .joint_state :
@@ -207,12 +213,17 @@ def on_dynamic_joint_commands(self, command: DynamicJointState):
207213 )
208214 continue
209215 self .requested_commands .commands [name ][k ] = v
216+ self .requested_commands .timestamp = time .time_ns ()
210217 self .joint_command_request_pub .set ()
211218 self .on_dynamic_joint_commands_counter -= 1
212219
213220 def publish_command_loop (self ):
214221 while rclpy .ok ():
215222 self .joint_command_request_pub .wait ()
223+ rm .travel_span ("wait_for_command_request_pub" ,
224+ start_time = self .requested_commands .timestamp ,
225+ tracer = self .tracer ,
226+ context = rm .ctx_from_traceparent (self .requested_commands .traceparent ))
216227
217228 with self .pub_lock :
218229 self .handle_commands (self .requested_commands )
@@ -335,26 +346,27 @@ def handle_regular_commands(self, commands):
335346 # Subscription: DynamicJointState cb
336347 def on_dynamic_joint_states (self , state : DynamicJointState ):
337348 """Retreive the joint state from /dynamic_joint_states."""
338- if not self .joint_state_ready .is_set ():
339- for uid , name in enumerate (state .joint_names ):
340- self .joint_state [name ] = {}
341- self .joint_state [name ]["name" ] = name
342- self .joint_state [name ]["uid" ] = uid
343-
344- self .joint_command [name ] = {}
345-
346- for uid , (name , kv ) in enumerate (
347- zip (state .joint_names , state .interface_values )
348- ):
349- for k , v in zip (kv .interface_names , kv .values ):
350- self .joint_state [name ][k ] = v
351-
352- if not self .joint_state_ready .is_set ():
353- for name , state in self .joint_state .items ():
354- if "position" in state :
355- state ["target_position" ] = state ["position" ]
356-
357- self .joint_state_ready .set ()
349+ with self .sum_joint_states .time ():
350+ if not self .joint_state_ready .is_set ():
351+ for uid , name in enumerate (state .joint_names ):
352+ self .joint_state [name ] = {}
353+ self .joint_state [name ]["name" ] = name
354+ self .joint_state [name ]["uid" ] = uid
355+
356+ self .joint_command [name ] = {}
357+
358+ for uid , (name , kv ) in enumerate (
359+ zip (state .joint_names , state .interface_values )
360+ ):
361+ for k , v in zip (kv .interface_names , kv .values ):
362+ self .joint_state [name ][k ] = v
363+
364+ if not self .joint_state_ready .is_set ():
365+ for name , state in self .joint_state .items ():
366+ if "position" in state :
367+ state ["target_position" ] = state ["position" ]
368+
369+ self .joint_state_ready .set ()
358370
359371 def on_forward_position_controller_update (
360372 self , msg : Float64MultiArray , controller_name : str
0 commit comments