1414
1515import time
1616from dataclasses import dataclass
17- import random
1817from typing import Any
1918
2019from google .adk .agents .invocation_context import InvocationContext
@@ -299,16 +298,14 @@ def record_call_llm(
299298 # record span latency
300299 if hasattr (span , "start_time" ) and self .apmplus_span_latency :
301300 # span 耗时
302- duration = (time .time_ns () - span .start_time )/ 1e9 # type: ignore
303- self .apmplus_span_latency .record (
304- duration , attributes = attributes
305- )
301+ duration = (time .time_ns () - span .start_time ) / 1e9 # type: ignore
302+ self .apmplus_span_latency .record (duration , attributes = attributes )
306303
307304 def record_tool_call (
308- self ,
309- tool : BaseTool ,
310- args : dict [str , Any ],
311- function_response_event : Event ,
305+ self ,
306+ tool : BaseTool ,
307+ args : dict [str , Any ],
308+ function_response_event : Event ,
312309 ):
313310 logger .debug (f"Record tool call work in progress. Tool: { tool .name } " )
314311 span = trace .get_current_span ()
@@ -329,23 +326,25 @@ def record_tool_call(
329326 if hasattr (span , "start_time" ) and self .apmplus_span_latency :
330327 # span 耗时
331328 duration = (time .time_ns () - span .start_time ) / 1e9 # type: ignore
332- self .apmplus_span_latency .record (
333- duration , attributes = attributes
334- )
329+ self .apmplus_span_latency .record (duration , attributes = attributes )
335330
336331 if self .apmplus_tool_token_usage and hasattr (span , "attributes" ):
337332 tool_input = span .attributes ["gen_ai.tool.input" ]
338- tool_token_usage = len (tool_input ) / 4 # tool token 数量,使用文本长度/4 # tool token 数量,使用文本长度/4
333+ tool_token_usage_input = (
334+ len (tool_input ) / 4
335+ ) # tool token 数量,使用文本长度/4
339336 input_tool_token_attributes = {** attributes , "token_type" : "input" }
340337 self .apmplus_tool_token_usage .record (
341- tool_token_usage , attributes = input_tool_token_attributes
338+ tool_token_usage_input , attributes = input_tool_token_attributes
342339 )
343340
344341 tool_output = span .attributes ["gen_ai.tool.output" ]
345- tool_token_usage = len (tool_output ) / 4 # tool token 数量,使用文本长度/4 # tool token 数量,使用文本长度/4
342+ tool_token_usage_output = (
343+ len (tool_output ) / 4
344+ ) # tool token 数量,使用文本长度/4
346345 output_tool_token_attributes = {** attributes , "token_type" : "output" }
347346 self .apmplus_tool_token_usage .record (
348- tool_token_usage , attributes = output_tool_token_attributes
347+ tool_token_usage_output , attributes = output_tool_token_attributes
349348 )
350349
351350
0 commit comments