Skip to content

Commit ebfc290

Browse files
refactor: log tool execution duration
1 parent 25b45d7 commit ebfc290

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

apps/common/utils/tool_code.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import resource
1313
import getpass
1414
import random
15+
import time
1516
import uuid_utils.compat as uuid
17+
from contextlib import contextmanager
1618
from common.utils.logger import maxkb_logger
1719
from django.utils.translation import gettext_lazy as _
1820
from maxkb.const import BASE_DIR, CONFIG
@@ -108,7 +110,8 @@ def exec_code(self, code_str, keywords, function_name=None):
108110
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=True) as f:
109111
f.write(_exec_code)
110112
f.flush()
111-
subprocess_result = self._exec(f.name)
113+
with execution_timer(_id):
114+
subprocess_result = self._exec(f.name)
112115
if subprocess_result.returncode != 0:
113116
raise Exception(subprocess_result.stderr or subprocess_result.stdout or "Unknown exception occurred")
114117
lines = subprocess_result.stdout.splitlines()
@@ -244,3 +247,12 @@ def validate_mcp_transport(self, code_str):
244247
for server, config in servers.items():
245248
if config.get('transport') not in ['sse', 'streamable_http']:
246249
raise Exception(_('Only support transport=sse or transport=streamable_http'))
250+
251+
252+
@contextmanager
253+
def execution_timer(id=""):
254+
start = time.perf_counter()
255+
try:
256+
yield
257+
finally:
258+
maxkb_logger.debug(f"Tool execution({id}) takes {time.perf_counter() - start:.6f} seconds.")

0 commit comments

Comments
 (0)