1414
1515import json
1616from abc import ABC , abstractmethod
17- from typing import Optional
17+ from typing import Optional , Any
1818
1919from google .adk .agents .callback_context import CallbackContext
2020from google .adk .models import LlmRequest , LlmResponse
21+ from google .adk .tools import BaseTool , ToolContext
2122from opentelemetry import trace
2223
2324from veadk .utils .logger import get_logger
2728
2829class BaseTracer (ABC ):
2930 def __init__ (self , name : str ):
31+ self .app_name = "veadk_app_name"
3032 pass
3133
3234 @abstractmethod
3335 def dump (self ) -> str : ...
3436
35- def llm_metrics_hook (
37+ def tracer_hook_before_model (
3638 self , callback_context : CallbackContext , llm_request : LlmRequest
3739 ) -> Optional [LlmResponse ]:
40+ """agent run stage"""
3841 trace .get_tracer ("gcp.vertex.agent" )
3942 span = trace .get_current_span ()
4043 # logger.debug(f"llm_request: {llm_request}")
4144
4245 req = llm_request .model_dump ()
4346
47+ app_name = getattr (self , "app_name" , "veadk_app" )
48+ agent_name = callback_context .agent_name
4449 model_name = req .get ("model" , "unknown" )
4550 max_tokens = (
4651 None
@@ -59,6 +64,8 @@ def llm_metrics_hook(
5964 )
6065
6166 attributes = {}
67+ attributes ["agent.name" ] = agent_name
68+ attributes ["app.name" ] = app_name
6269 attributes ["gen_ai.system" ] = "veadk"
6370 if model_name :
6471 attributes ["gen_ai.request.model" ] = model_name
@@ -84,9 +91,10 @@ def llm_metrics_hook(
8491 for k , v in attributes .items ():
8592 span .set_attribute (k , v )
8693
87- def token_metrics_hook (
94+ def tracer_hook_after_model (
8895 self , callback_context : CallbackContext , llm_response : LlmResponse
8996 ) -> Optional [LlmResponse ]:
97+ """call llm stage"""
9098 trace .get_tracer ("gcp.vertex.agent" )
9199 span = trace .get_current_span ()
92100 # logger.debug(f"llm_response: {llm_response}")
@@ -95,6 +103,10 @@ def token_metrics_hook(
95103 # Refined: collect all attributes, use set_attributes, print for debugging
96104 attributes = {}
97105
106+ app_name = getattr (self , "app_name" , "veadk_app" )
107+ agent_name = callback_context .agent_name
108+ attributes ["agent.name" ] = agent_name
109+ attributes ["app.name" ] = app_name
98110 # prompt
99111 user_content = callback_context .user_content
100112 if getattr (user_content , "role" , None ):
@@ -170,3 +182,32 @@ def token_metrics_hook(
170182 # Fallback for OpenTelemetry versions without set_attributes
171183 for k , v in attributes .items ():
172184 span .set_attribute (k , v )
185+
186+ def tracer_hook_after_tool (
187+ self ,
188+ tool : BaseTool ,
189+ args : dict [str , Any ],
190+ tool_context : ToolContext ,
191+ tool_response : dict ,
192+ ):
193+ trace .get_tracer ("gcp.vertex.agent" )
194+ span = trace .get_current_span ()
195+ agent_name = tool_context .agent_name
196+ tool_name = tool .name
197+ app_name = getattr (self , "app_name" , "veadk_app" )
198+ attributes = {
199+ "agent.name" : agent_name ,
200+ "app.name" : app_name ,
201+ "tool.name" : tool_name ,
202+ }
203+
204+ # Set all attributes at once if possible, else fallback to individual
205+ if hasattr (span , "set_attributes" ):
206+ span .set_attributes (attributes )
207+ else :
208+ # Fallback for OpenTelemetry versions without set_attributes
209+ for k , v in attributes .items ():
210+ span .set_attribute (k , v )
211+
212+ def set_app_name (self , app_name ):
213+ self .app_name = app_name
0 commit comments