Skip to content

Commit ede0ef2

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

File tree

4 files changed

+78
-27
lines changed

4 files changed

+78
-27
lines changed

observability/attributes.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ package observability
1717
import (
1818
"context"
1919
"os"
20+
"strings"
2021

2122
"github.com/volcengine/veadk-go/configs"
2223
"go.opentelemetry.io/otel/attribute"
2324
"go.opentelemetry.io/otel/trace"
2425
)
2526

27+
var isAgentKitRuntime = checkAgentKitRuntime()
28+
2629
// setCommonAttributes enriches the span with common attributes from context, config, or env.
2730
func setCommonAttributes(ctx context.Context, span trace.Span) {
2831
// 1. Fixed attributes
@@ -192,3 +195,21 @@ func getServiceName(cfg *configs.OpenTelemetryConfig) string {
192195
}
193196
return "<unknown_service>"
194197
}
198+
199+
// OTEL_RESOURCE_ATTRIBUTES="instance.id=123456,apmplus.business_carrier=agentkit_runtime"
200+
func checkAgentKitRuntime() bool {
201+
otelAttrStr := os.Getenv("OTEL_RESOURCE_ATTRIBUTES")
202+
if otelAttrStr == "" {
203+
return false
204+
}
205+
206+
otelAttrs := strings.Split(otelAttrStr, ",")
207+
for _, attr := range otelAttrs {
208+
kv := strings.Split(attr, "=")
209+
if len(kv) == 2 && kv[0] == "apmplus.business_carrier" && kv[1] == "agentkit_runtime" {
210+
return true
211+
}
212+
}
213+
214+
return false
215+
}

observability/metrics.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package observability
1616

1717
import (
1818
"context"
19-
"fmt"
2019
"sync"
2120

2221
"go.opentelemetry.io/otel"
@@ -290,10 +289,7 @@ func RecordAPMPlusToolTokenUsage(ctx context.Context, tokens int64, attrs ...att
290289
}
291290
}
292291

293-
func RecordAgentKitDuration(ctx context.Context, durationSeconds float64, err error, attrs ...attribute.KeyValue) {
294-
if err != nil {
295-
attrs = append(attrs, attribute.String("error_type", fmt.Sprintf("%T", err)))
296-
}
292+
func RecordAgentKitDuration(ctx context.Context, durationSeconds float64, attrs ...attribute.KeyValue) {
297293
for _, histogram := range agentkitDurationHistograms {
298294
histogram.Record(ctx, durationSeconds, metric.WithAttributes(attrs...))
299295
}

observability/metrics_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package observability
1616

1717
import (
1818
"context"
19-
"fmt"
2019
"testing"
2120

2221
"github.com/stretchr/testify/assert"
@@ -120,8 +119,7 @@ func TestMetricsRecording(t *testing.T) {
120119
})
121120

122121
t.Run("RecordAgentKitDurationWithError", func(t *testing.T) {
123-
testErr := fmt.Errorf("test error")
124-
RecordAgentKitDuration(ctx, 2.5, testErr, attrs...)
122+
RecordAgentKitDuration(ctx, 2.5, attrs...)
125123

126124
var rm metricdata.ResourceMetrics
127125
err := reader.Collect(ctx, &rm)
@@ -136,8 +134,6 @@ func TestMetricsRecording(t *testing.T) {
136134
if dp.Count > 0 {
137135
assert.Equal(t, uint64(1), dp.Count)
138136
assert.Equal(t, 2.5, dp.Sum)
139-
errType, _ := dp.Attributes.Value("error_type")
140-
assert.Equal(t, "*errors.errorString", errType.AsString())
141137
found = true
142138
}
143139
}

observability/plugin.go

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,22 @@ func (p *adkObservabilityPlugin) AfterRun(ctx agent.InvocationContext) {
189189
RecordOperationDuration(context.Background(), elapsed, metricAttrs...)
190190
RecordAPMPlusSpanLatency(context.Background(), elapsed, metricAttrs...)
191191

192-
agentKitsAttrs := []attribute.KeyValue{
193-
attribute.String("gen_ai_operation_name", "chain"),
194-
attribute.String("gen_ai_operation_type", "workflow"),
192+
if isAgentKitRuntime {
193+
agentKitsAttrs := []attribute.KeyValue{
194+
attribute.String("gen_ai_operation_name", "chain"),
195+
attribute.String("gen_ai_operation_type", "workflow"),
196+
}
197+
198+
var lastErr error
199+
if val, _ := ctx.Session().State().Get(stateKeyLastError); val != nil {
200+
lastErr = val.(error)
201+
}
202+
if lastErr != nil {
203+
agentKitsAttrs = append(agentKitsAttrs, attribute.String("error_type", "invocation_error"))
204+
}
205+
RecordAgentKitDuration(context.Background(), elapsed, agentKitsAttrs...)
195206
}
196207

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...)
202208
}
203209

204210
}
@@ -367,7 +373,7 @@ func (p *adkObservabilityPlugin) AfterModel(ctx agent.CallbackContext, resp *mod
367373
attribute.String("gen_ai_response_model", meta.ModelName),
368374
attribute.String("gen_ai_operation_name", "chat"),
369375
attribute.String("gen_ai_operation_type", "llm"),
370-
attribute.String("error_type", "error"), // Simple error type
376+
attribute.String("error_type", "llm_error"), // Simple error type
371377
}
372378
RecordExceptions(context.Context(ctx), 1, metricAttrs...)
373379
p.recordFinalResponseMetrics(ctx, meta, meta.ModelName, err)
@@ -590,11 +596,16 @@ func (p *adkObservabilityPlugin) recordFinalResponseMetrics(ctx agent.CallbackCo
590596
RecordOperationDuration(context.Context(ctx), duration, metricAttrs...)
591597
RecordAPMPlusSpanLatency(context.Context(ctx), duration, metricAttrs...)
592598

593-
agentKitsAttrs := []attribute.KeyValue{
594-
attribute.String("gen_ai_operation_name", "chat"),
595-
attribute.String("gen_ai_operation_type", "llm"),
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...)
596608
}
597-
RecordAgentKitDuration(context.Context(ctx), duration, err, agentKitsAttrs...)
598609
}
599610
}
600611
}
@@ -937,11 +948,16 @@ func (p *adkObservabilityPlugin) AfterTool(ctx tool.Context, tool tool.Tool, arg
937948
RecordOperationDuration(context.Background(), duration, metricAttrs...)
938949
RecordAPMPlusSpanLatency(context.Background(), duration, metricAttrs...)
939950

940-
agentKitsAttrs := []attribute.KeyValue{
941-
attribute.String("gen_ai_operation_name", tool.Name()),
942-
attribute.String("gen_ai_operation_type", "tool"),
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...)
943960
}
944-
RecordAgentKitDuration(context.Background(), duration, err, agentKitsAttrs...)
945961
}
946962

947963
if p.isMetricsEnabled() {
@@ -1034,6 +1050,28 @@ func (p *adkObservabilityPlugin) AfterAgent(ctx agent.CallbackContext) (*genai.C
10341050
span.End()
10351051
}
10361052
}
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+
}
10371075
return nil, nil
10381076
}
10391077

0 commit comments

Comments
 (0)