@@ -35,12 +35,10 @@ const (
3535
3636 SpanNameCallLLM = "call_llm"
3737 SpanNamePrefixExecuteTool = "execute_tool"
38- SpanNamePrefixAgentRun = "agent_run"
39- SpanNameInvocation = "invocation"
4038
4139 OperationExecuteTool = "execute_tool"
4240 OperationCallLLM = "call_llm"
43- OperationRunRunner = "run_runner" // attribute of SpanNameInvocation
41+ OperationInvokeAgent = "invoke_agent"
4442)
4543
4644const (
@@ -75,11 +73,19 @@ var (
7573 KeyToolID = "trpc.go.agent.tool_id"
7674
7775 // GenAI operation attributes
78- KeyGenAIOperationName = "gen_ai.operation.name"
79- KeyGenAISystem = "gen_ai.system"
80- KeyGenAIToolName = "gen_ai.tool.name"
81- KeyGenAIToolDesc = "gen_ai.tool.description"
82- KeyGenAIRequestModel = "gen_ai.request.model"
76+ KeyGenAIOperationName = "gen_ai.operation.name"
77+ KeyGenAISystem = "gen_ai.system"
78+ KeyGenAIToolName = "gen_ai.tool.name"
79+ KeyGenAIToolDesc = "gen_ai.tool.description"
80+ KeyGenAIRequestModel = "gen_ai.request.model"
81+ KeyGenAIInputMessages = "gen_ai.input.messages"
82+ KeyGenAIOutputMessages = "gen_ai.output.messages"
83+ KeyGenAIAgentName = "gen_ai.agent.name"
84+ KeyGenAIConversationID = "gen_ai.conversation.id"
85+ KeyGenAIResponseModel = "gen_ai.response.model"
86+ KeyGenAIUsageOutputTokens = "gen_ai.usage.output_tokens"
87+ KeyGenAIResponseID = "gen_ai.response.id"
88+ KeyGenAIUsageInputTokens = "gen_ai.usage.input_tokens"
8389
8490 // System value
8591 SystemTRPCGoAgent = "trpc.go.agent"
@@ -142,24 +148,45 @@ func TraceMergedToolCalls(span trace.Span, rspEvent *event.Event) {
142148 )
143149}
144150
145- // TraceRunner traces the invocation of a runner .
146- func TraceRunner (span trace.Span , appName string , invoke * agent.Invocation , message model. Message ) {
147- if bts , err := json .Marshal (& model.Request {Messages : []model.Message {message }}); err == nil {
151+ // TraceBeforeInvokeAgent traces the before invocation of an agent .
152+ func TraceBeforeInvokeAgent (span trace.Span , invoke * agent.Invocation ) {
153+ if bts , err := json .Marshal (& model.Request {Messages : []model.Message {invoke . Message }}); err == nil {
148154 span .SetAttributes (
149- attribute .String (KeyRunnerInput , string (bts )),
155+ attribute .String (KeyGenAIInputMessages , string (bts )),
150156 )
151157 } else {
152- span .SetAttributes (attribute .String (KeyRunnerInput , "<not json serializable>" ))
158+ span .SetAttributes (attribute .String (KeyGenAIInputMessages , "<not json serializable>" ))
153159 }
154-
155160 span .SetAttributes (
156161 attribute .String (KeyGenAISystem , SystemTRPCGoAgent ),
157- attribute .String (KeyGenAIOperationName , OperationRunRunner ),
158- attribute .String (KeyRunnerName , fmt . Sprintf ( "[trpc-go-agent]: %s/%s" , appName , invoke .AgentName ) ),
162+ attribute .String (KeyGenAIOperationName , OperationInvokeAgent ),
163+ attribute .String (KeyGenAIAgentName , invoke .AgentName ),
159164 attribute .String (KeyInvocationID , invoke .InvocationID ),
160- attribute .String (KeyRunnerSessionID , invoke .Session .ID ),
161- attribute .String (KeyRunnerUserID , invoke .Session .UserID ),
162165 )
166+ if invoke .Session != nil {
167+ span .SetAttributes (
168+ attribute .String (KeyRunnerUserID , invoke .Session .UserID ),
169+ attribute .String (KeyGenAIConversationID , invoke .Session .ID ),
170+ )
171+ }
172+ }
173+
174+ // TraceAfterInvokeAgent traces the after invocation of an agent.
175+ func TraceAfterInvokeAgent (span trace.Span , rsp * model.Response ) {
176+ if len (rsp .Choices ) > 0 {
177+ if bts , err := json .Marshal (rsp .Choices [0 ].Message ); err == nil {
178+ span .SetAttributes (
179+ attribute .String (KeyGenAIOutputMessages , string (bts )),
180+ )
181+ }
182+ }
183+ span .SetAttributes (attribute .String (KeyGenAIResponseModel , rsp .Model ))
184+ if rsp .Usage != nil {
185+ span .SetAttributes (attribute .Int (KeyGenAIUsageInputTokens , rsp .Usage .PromptTokens ))
186+ span .SetAttributes (attribute .Int (KeyGenAIUsageOutputTokens , rsp .Usage .CompletionTokens ))
187+ }
188+ span .SetAttributes (attribute .String (KeyGenAIResponseID , rsp .ID ))
189+
163190}
164191
165192// TraceCallLLM traces the invocation of an LLM call.
0 commit comments