Skip to content

Commit 6bb9668

Browse files
shunjiazhusjy3
authored andcommitted
add agentkit_runtime_operation_latency metric for agentkit
1 parent ede0ef2 commit 6bb9668

File tree

1 file changed

+10
-60
lines changed

1 file changed

+10
-60
lines changed

observability/plugin.go

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,18 @@ func (p *adkObservabilityPlugin) AfterRun(ctx agent.InvocationContext) {
195195
attribute.String("gen_ai_operation_type", "workflow"),
196196
}
197197

198-
var lastErr error
199-
if val, _ := ctx.Session().State().Get(stateKeyLastError); val != nil {
200-
lastErr = val.(error)
198+
var errorCode string
199+
eventLen := ctx.Session().Events().Len()
200+
if eventLen > 0 {
201+
lastEvent := ctx.Session().Events().At(eventLen - 1)
202+
errorCode = lastEvent.ErrorCode
201203
}
202-
if lastErr != nil {
203-
agentKitsAttrs = append(agentKitsAttrs, attribute.String("error_type", "invocation_error"))
204+
if errorCode != "" {
205+
agentKitsAttrs = append(agentKitsAttrs, attribute.String("error_type", errorCode))
204206
}
205207
RecordAgentKitDuration(context.Background(), elapsed, agentKitsAttrs...)
206208
}
207-
208209
}
209-
210210
}
211211

212212
// Clean up from global map with delay to allow children to be exported.
@@ -364,7 +364,6 @@ func (p *adkObservabilityPlugin) AfterModel(ctx agent.CallbackContext, resp *mod
364364

365365
if err != nil {
366366
span.SetStatus(codes.Error, err.Error())
367-
_ = ctx.State().Set(stateKeyLastError, err)
368367
// Record Exceptions metric
369368
if p.isMetricsEnabled() {
370369
meta := p.getSpanMetadata(ctx.State())
@@ -373,10 +372,9 @@ func (p *adkObservabilityPlugin) AfterModel(ctx agent.CallbackContext, resp *mod
373372
attribute.String("gen_ai_response_model", meta.ModelName),
374373
attribute.String("gen_ai_operation_name", "chat"),
375374
attribute.String("gen_ai_operation_type", "llm"),
376-
attribute.String("error_type", "llm_error"), // Simple error type
375+
attribute.String("error_type", "error"), // Simple error type
377376
}
378377
RecordExceptions(context.Context(ctx), 1, metricAttrs...)
379-
p.recordFinalResponseMetrics(ctx, meta, meta.ModelName, err)
380378
}
381379
return nil, nil
382380
}
@@ -470,7 +468,7 @@ func (p *adkObservabilityPlugin) AfterModel(ctx agent.CallbackContext, resp *mod
470468

471469
if !resp.Partial {
472470
// Record Operation Duration and Latency
473-
p.recordFinalResponseMetrics(ctx, meta, finalModelName, nil)
471+
p.recordFinalResponseMetrics(ctx, meta, finalModelName)
474472
}
475473

476474
return nil, nil
@@ -583,7 +581,7 @@ func (p *adkObservabilityPlugin) recordStreamingGenerationMetrics(ctx agent.Call
583581
}
584582
}
585583

586-
func (p *adkObservabilityPlugin) recordFinalResponseMetrics(ctx agent.CallbackContext, meta *spanMetadata, finalModelName string, err error) {
584+
func (p *adkObservabilityPlugin) recordFinalResponseMetrics(ctx agent.CallbackContext, meta *spanMetadata, finalModelName string) {
587585
if !meta.StartTime.IsZero() {
588586
duration := time.Since(meta.StartTime).Seconds()
589587
metricAttrs := []attribute.KeyValue{
@@ -595,17 +593,6 @@ func (p *adkObservabilityPlugin) recordFinalResponseMetrics(ctx agent.CallbackCo
595593
if p.isMetricsEnabled() {
596594
RecordOperationDuration(context.Context(ctx), duration, metricAttrs...)
597595
RecordAPMPlusSpanLatency(context.Context(ctx), duration, metricAttrs...)
598-
599-
if isAgentKitRuntime {
600-
agentKitsAttrs := []attribute.KeyValue{
601-
attribute.String("gen_ai_operation_name", "chat"),
602-
attribute.String("gen_ai_operation_type", "llm"),
603-
}
604-
if err != nil {
605-
agentKitsAttrs = append(agentKitsAttrs, attribute.String("error_type", "llm_error"))
606-
}
607-
RecordAgentKitDuration(context.Context(ctx), duration, agentKitsAttrs...)
608-
}
609596
}
610597
}
611598
}
@@ -932,9 +919,6 @@ func (p *adkObservabilityPlugin) BeforeTool(ctx tool.Context, tool tool.Tool, ar
932919

933920
// AfterTool is called after a tool is executed.
934921
func (p *adkObservabilityPlugin) AfterTool(ctx tool.Context, tool tool.Tool, args, result map[string]any, err error) (map[string]any, error) {
935-
if err != nil {
936-
_ = ctx.State().Set(stateKeyLastError, err)
937-
}
938922
// Metrics recording only
939923
meta := p.getSpanMetadata(ctx.State())
940924
if !meta.StartTime.IsZero() {
@@ -947,17 +931,6 @@ func (p *adkObservabilityPlugin) AfterTool(ctx tool.Context, tool tool.Tool, arg
947931
if p.isMetricsEnabled() {
948932
RecordOperationDuration(context.Background(), duration, metricAttrs...)
949933
RecordAPMPlusSpanLatency(context.Background(), duration, metricAttrs...)
950-
951-
if isAgentKitRuntime {
952-
agentKitsAttrs := []attribute.KeyValue{
953-
attribute.String("gen_ai_operation_name", tool.Name()),
954-
attribute.String("gen_ai_operation_type", "tool"),
955-
}
956-
if err == nil {
957-
agentKitsAttrs = append(agentKitsAttrs, attribute.String("error_type", "tool_error"))
958-
}
959-
RecordAgentKitDuration(context.Background(), duration, agentKitsAttrs...)
960-
}
961934
}
962935

963936
if p.isMetricsEnabled() {
@@ -1050,28 +1023,6 @@ func (p *adkObservabilityPlugin) AfterAgent(ctx agent.CallbackContext) (*genai.C
10501023
span.End()
10511024
}
10521025
}
1053-
1054-
meta := p.getSpanMetadata(ctx.State())
1055-
if !meta.StartTime.IsZero() {
1056-
if p.isMetricsEnabled() {
1057-
if isAgentKitRuntime {
1058-
duration := time.Since(meta.StartTime).Seconds()
1059-
agentKitsAttrs := []attribute.KeyValue{
1060-
attribute.String("gen_ai_operation_name", "invoke_agent"),
1061-
attribute.String("gen_ai_operation_type", "agent_server"),
1062-
}
1063-
var lastErr error
1064-
if val, _ := ctx.State().Get(stateKeyLastError); val != nil {
1065-
lastErr = val.(error)
1066-
}
1067-
1068-
if lastErr != nil {
1069-
agentKitsAttrs = append(agentKitsAttrs, attribute.String("error_type", "invoke_agent_error"))
1070-
}
1071-
RecordAgentKitDuration(context.Background(), duration, agentKitsAttrs...)
1072-
}
1073-
}
1074-
}
10751026
return nil, nil
10761027
}
10771028

@@ -1119,7 +1070,6 @@ const (
11191070
stateKeyMetadata = "veadk.observability.metadata"
11201071
stateKeyStreamingOutput = "veadk.observability.streaming_output"
11211072
stateKeyStreamingSpan = "veadk.observability.streaming_span"
1122-
stateKeyLastError = "veadk.observability.last_error"
11231073
)
11241074

11251075
// spanMetadata groups various observational data points in a single structure

0 commit comments

Comments
 (0)