@@ -179,16 +179,28 @@ func (p *adkObservabilityPlugin) AfterRun(ctx agent.InvocationContext) {
179179
180180 // Record final metrics for invocation
181181 if ! meta .StartTime .IsZero () {
182- elapsed := time .Since (meta .StartTime ).Seconds ()
183- metricAttrs := []attribute.KeyValue {
184- attribute .String ("gen_ai_operation_name" , "chain" ),
185- attribute .String ("gen_ai_operation_type" , "workflow" ),
186- attribute .String ("gen_ai.system" , GetModelProvider (context .Context (ctx ))),
187- }
188182 if p .isMetricsEnabled () {
183+ elapsed := time .Since (meta .StartTime ).Seconds ()
184+ metricAttrs := []attribute.KeyValue {
185+ attribute .String ("gen_ai_operation_name" , "chain" ),
186+ attribute .String ("gen_ai_operation_type" , "workflow" ),
187+ attribute .String ("gen_ai.system" , GetModelProvider (context .Context (ctx ))),
188+ }
189189 RecordOperationDuration (context .Background (), elapsed , metricAttrs ... )
190190 RecordAPMPlusSpanLatency (context .Background (), elapsed , metricAttrs ... )
191+
192+ agentKitsAttrs := []attribute.KeyValue {
193+ attribute .String ("gen_ai_operation_name" , "chain" ),
194+ attribute .String ("gen_ai_operation_type" , "workflow" ),
195+ }
196+
197+ var lastErr error
198+ if val , _ := ctx .Session ().State ().Get (stateKeyLastError ); val != nil {
199+ lastErr = val .(error )
200+ }
201+ RecordAgentKitDuration (context .Background (), elapsed , lastErr , agentKitsAttrs ... )
191202 }
203+
192204 }
193205
194206 // Clean up from global map with delay to allow children to be exported.
@@ -346,6 +358,7 @@ func (p *adkObservabilityPlugin) AfterModel(ctx agent.CallbackContext, resp *mod
346358
347359 if err != nil {
348360 span .SetStatus (codes .Error , err .Error ())
361+ _ = ctx .State ().Set (stateKeyLastError , err )
349362 // Record Exceptions metric
350363 if p .isMetricsEnabled () {
351364 meta := p .getSpanMetadata (ctx .State ())
@@ -357,6 +370,7 @@ func (p *adkObservabilityPlugin) AfterModel(ctx agent.CallbackContext, resp *mod
357370 attribute .String ("error_type" , "error" ), // Simple error type
358371 }
359372 RecordExceptions (context .Context (ctx ), 1 , metricAttrs ... )
373+ p .recordFinalResponseMetrics (ctx , meta , meta .ModelName , err )
360374 }
361375 return nil , nil
362376 }
@@ -450,7 +464,7 @@ func (p *adkObservabilityPlugin) AfterModel(ctx agent.CallbackContext, resp *mod
450464
451465 if ! resp .Partial {
452466 // Record Operation Duration and Latency
453- p .recordFinalResponseMetrics (ctx , meta , finalModelName )
467+ p .recordFinalResponseMetrics (ctx , meta , finalModelName , nil )
454468 }
455469
456470 return nil , nil
@@ -563,7 +577,7 @@ func (p *adkObservabilityPlugin) recordStreamingGenerationMetrics(ctx agent.Call
563577 }
564578}
565579
566- func (p * adkObservabilityPlugin ) recordFinalResponseMetrics (ctx agent.CallbackContext , meta * spanMetadata , finalModelName string ) {
580+ func (p * adkObservabilityPlugin ) recordFinalResponseMetrics (ctx agent.CallbackContext , meta * spanMetadata , finalModelName string , err error ) {
567581 if ! meta .StartTime .IsZero () {
568582 duration := time .Since (meta .StartTime ).Seconds ()
569583 metricAttrs := []attribute.KeyValue {
@@ -575,6 +589,12 @@ func (p *adkObservabilityPlugin) recordFinalResponseMetrics(ctx agent.CallbackCo
575589 if p .isMetricsEnabled () {
576590 RecordOperationDuration (context .Context (ctx ), duration , metricAttrs ... )
577591 RecordAPMPlusSpanLatency (context .Context (ctx ), duration , metricAttrs ... )
592+
593+ agentKitsAttrs := []attribute.KeyValue {
594+ attribute .String ("gen_ai_operation_name" , "chat" ),
595+ attribute .String ("gen_ai_operation_type" , "llm" ),
596+ }
597+ RecordAgentKitDuration (context .Context (ctx ), duration , err , agentKitsAttrs ... )
578598 }
579599 }
580600}
@@ -901,6 +921,9 @@ func (p *adkObservabilityPlugin) BeforeTool(ctx tool.Context, tool tool.Tool, ar
901921
902922// AfterTool is called after a tool is executed.
903923func (p * adkObservabilityPlugin ) AfterTool (ctx tool.Context , tool tool.Tool , args , result map [string ]any , err error ) (map [string ]any , error ) {
924+ if err != nil {
925+ _ = ctx .State ().Set (stateKeyLastError , err )
926+ }
904927 // Metrics recording only
905928 meta := p .getSpanMetadata (ctx .State ())
906929 if ! meta .StartTime .IsZero () {
@@ -913,6 +936,12 @@ func (p *adkObservabilityPlugin) AfterTool(ctx tool.Context, tool tool.Tool, arg
913936 if p .isMetricsEnabled () {
914937 RecordOperationDuration (context .Background (), duration , metricAttrs ... )
915938 RecordAPMPlusSpanLatency (context .Background (), duration , metricAttrs ... )
939+
940+ agentKitsAttrs := []attribute.KeyValue {
941+ attribute .String ("gen_ai_operation_name" , tool .Name ()),
942+ attribute .String ("gen_ai_operation_type" , "tool" ),
943+ }
944+ RecordAgentKitDuration (context .Background (), duration , err , agentKitsAttrs ... )
916945 }
917946
918947 if p .isMetricsEnabled () {
@@ -1052,6 +1081,7 @@ const (
10521081 stateKeyMetadata = "veadk.observability.metadata"
10531082 stateKeyStreamingOutput = "veadk.observability.streaming_output"
10541083 stateKeyStreamingSpan = "veadk.observability.streaming_span"
1084+ stateKeyLastError = "veadk.observability.last_error"
10551085)
10561086
10571087// spanMetadata groups various observational data points in a single structure
0 commit comments