@@ -51,10 +51,7 @@ def upload_metrics(
5151 exporter .meter_uploader .record (llm_request , llm_response )
5252
5353
54- def trace_send_data (): ...
55-
56-
57- def set_common_attributes (
54+ def set_common_attributes_on_model_span (
5855 invocation_context : InvocationContext , current_span : _Span , ** kwargs
5956) -> None :
6057 if current_span .context :
@@ -66,7 +63,7 @@ def set_common_attributes(
6663 return
6764
6865 try :
69- spans = _INMEMORY_EXPORTER_INSTANCE .processor .spans # # type: ignore
66+ spans = _INMEMORY_EXPORTER_INSTANCE .processor .spans # type: ignore
7067
7168 spans_in_current_trace = [
7269 span
@@ -76,24 +73,52 @@ def set_common_attributes(
7673
7774 common_attributes = ATTRIBUTES .get ("common" , {})
7875 for span in spans_in_current_trace :
79- if span .name .startswith ("invocation" ):
80- span .set_attribute ("gen_ai.operation.name" , "chain" )
81- elif span .name .startswith ("agent_run" ):
82- span .set_attribute ("gen_ai.operation.name" , "agent" )
83- for attr_name , attr_extractor in common_attributes .items ():
84- value = attr_extractor (** kwargs )
85- span .set_attribute (attr_name , value )
76+ if span .is_recording ():
77+ if span .name .startswith ("invocation" ):
78+ span .set_attribute ("gen_ai.operation.name" , "chain" )
79+ elif span .name .startswith ("agent_run" ):
80+ span .set_attribute ("gen_ai.operation.name" , "agent" )
81+ for attr_name , attr_extractor in common_attributes .items ():
82+ value = attr_extractor (** kwargs )
83+ span .set_attribute (attr_name , value )
8684 except Exception as e :
8785 logger .error (f"Failed to set common attributes for spans: { e } " )
8886
8987
88+ def set_common_attributes_on_tool_span (current_span : _Span ) -> None :
89+ # find parent span (generally a llm span)
90+ if not current_span .context :
91+ logger .warning (
92+ f"Get tool span's context failed. Skip setting common attributes for span { current_span .name } "
93+ )
94+ return
95+
96+ if not current_span .parent :
97+ logger .warning (
98+ f"Get tool span's parent failed. Skip setting common attributes for span { current_span .name } "
99+ )
100+ return
101+
102+ parent_span_id = current_span .parent .span_id
103+ for span in _INMEMORY_EXPORTER_INSTANCE .processor .spans : # type: ignore
104+ if span .context .span_id == parent_span_id :
105+ common_attributes = ATTRIBUTES .get ("common" , {})
106+ for attr_name in common_attributes .keys ():
107+ current_span .set_attribute (attr_name , span .attributes [attr_name ])
108+
109+
110+ def trace_send_data (): ...
111+
112+
90113def trace_tool_call (
91114 tool : BaseTool ,
92115 args : dict [str , Any ],
93116 function_response_event : Event ,
94117) -> None :
95118 span = trace .get_current_span ()
96119
120+ set_common_attributes_on_tool_span (current_span = span ) # type: ignore
121+
97122 tool_attributes_mapping = ATTRIBUTES .get ("tool" , {})
98123 params = ToolAttributesParams (tool , args , function_response_event )
99124
@@ -112,7 +137,7 @@ def trace_call_llm(
112137
113138 from veadk .agent import Agent
114139
115- set_common_attributes (
140+ set_common_attributes_on_model_span (
116141 invocation_context = invocation_context ,
117142 current_span = span , # type: ignore
118143 agent_name = invocation_context .agent .name ,
0 commit comments