1010import stomp
1111import sys
1212import tempfile
13+ import time
1314import traceback
1415from pathlib import Path
1516
4849)
4950from giskard .utils import threaded
5051
52+ from giskard .utils .analytics_collector import analytics
53+
54+
5155logger = logging .getLogger (__name__ )
5256
5357
@@ -69,6 +73,17 @@ def dispatch_action(callback, ml_worker, action, req):
6973 rep_id = req ["id" ] if "id" in req .keys () else None
7074 # Parse the param
7175 params = req ["param" ] if "param" in req .keys () else {}
76+
77+ # Track usage frequency to optimize action param parsing
78+ analytics .track (
79+ "mlworker:websocket:action:type" ,
80+ {
81+ "name" : action .name ,
82+ "worker" : ml_worker .ml_worker_id ,
83+ "language" : "PYTHON" ,
84+ },
85+ )
86+ start = time .process_time ()
7287 try :
7388 params = parse_action_param (action , params )
7489 # Call the function and get the response
@@ -78,6 +93,19 @@ def dispatch_action(callback, ml_worker, action, req):
7893 error_str = str (e ), error_type = type (e ).__name__ , detail = traceback .format_exc ()
7994 )
8095 logger .warning (e )
96+ finally :
97+ analytics .track (
98+ "mlworker:websocket:action" ,
99+ {
100+ "name" : action .name ,
101+ "worker" : ml_worker .ml_worker_id ,
102+ "language" : "PYTHON" ,
103+ "type" : "ERROR" if isinstance (info , websocket .ErrorReply ) else "SUCCESS" ,
104+ "action_time" : time .process_time () - start ,
105+ "error" : info .error_str if isinstance (info , websocket .ErrorReply ) else "" ,
106+ "error_type" : info .error_type if isinstance (info , websocket .ErrorReply ) else "" ,
107+ },
108+ )
81109
82110 if rep_id :
83111 # Reply if there is an ID
@@ -102,6 +130,20 @@ def dispatch_action(callback, ml_worker, action, req):
102130 ),
103131 )
104132
133+ analytics .track (
134+ "mlworker:websocket:action:reply" ,
135+ {
136+ "name" : action .name ,
137+ "worker" : ml_worker .ml_worker_id ,
138+ "language" : "PYTHON" ,
139+ "action_time" : time .process_time () - start ,
140+ "is_error" : isinstance (info , websocket .ErrorReply ),
141+ "frag_len" : FRAG_LEN ,
142+ "frag_count" : frag_count ,
143+ "reply_len" : len (payload ),
144+ },
145+ )
146+
105147 # Post-processing of stopWorker
106148 if action == MLWorkerAction .stopWorker and ml_worker .ws_stopping is True :
107149 ml_worker .ws_conn .disconnect ()
0 commit comments