Skip to content

Commit ad7cf7f

Browse files
committed
address review comments
Signed-off-by: Jet Chiang <[email protected]>
1 parent 5e0f41b commit ad7cf7f

17 files changed

Lines changed: 441 additions & 256 deletions

File tree

‎go/adk/pkg/a2a/server/server_test.go‎

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,9 @@ func TestGRPCAndJSONRPCShareRequestHandler(t *testing.T) {
171171
}
172172
}
173173

174-
// runA2ARequest builds a server against an in-memory batch exporter, serves
175-
// one message/send, and returns the span names exported by the time ServeHTTP
176-
// returned — the last instant before net/http closes the response body (on
177-
// Agent Substrate, the checkpoint instant).
178-
func runA2ARequest(t *testing.T) map[string]bool {
174+
// runA2ARequest builds a server against an in-memory batch exporter and serves
175+
// one message/send.
176+
func runA2ARequest(t *testing.T) tracetest.SpanStubs {
179177
t.Helper()
180178

181179
exporter := tracetest.NewInMemoryExporter()
@@ -213,36 +211,56 @@ func runA2ARequest(t *testing.T) map[string]bool {
213211
t.Fatalf("unexpected status %d: %s", rec.Code, rec.Body.String())
214212
}
215213

216-
exported := map[string]bool{}
217-
for _, s := range exporter.GetSpans() {
218-
exported[s.Name] = true
219-
}
220-
return exported
214+
return exporter.GetSpans()
221215
}
222216

223217
// With KAGENT_PRE_RESPONSE_TRACE_FLUSH (set by the controller on Agent
224-
// Substrate actors), every span of a request's trace — including the otelhttp
225-
// server span, which only ends after the inner handler returns — must be
226-
// exported before the response body closes: substrate checkpoints the actor at
227-
// body close, freezing any still-buffered spans into the snapshot.
218+
// Substrate actors), the interceptor-owned request span and its invocation
219+
// descendants are ended and flushed at the quiescent event. The otelhttp span
220+
// remains open until its handler returns so it can retain response attributes.
228221
func TestSpansExportedBeforeResponseBodyCloses(t *testing.T) {
229222
t.Setenv("KAGENT_PRE_RESPONSE_TRACE_FLUSH", "true")
230223

231-
exported := runA2ARequest(t)
232-
if !exported["invocation"] {
224+
spans := runA2ARequest(t)
225+
exported := map[string]tracetest.SpanStub{}
226+
for _, span := range spans {
227+
exported[span.Name] = span
228+
}
229+
if _, ok := exported["invocation"]; !ok {
233230
t.Errorf("invocation span not exported before body close, got %v", exported)
234231
}
235-
if !exported["POST /"] {
236-
t.Errorf("server span not exported before body close, got %v", exported)
232+
if _, ok := exported["a2a.request"]; !ok {
233+
t.Errorf("A2A request span not exported before body close, got %v", exported)
234+
}
235+
requestSpan := exported["a2a.request"]
236+
httpSpan, ok := exported["POST /"]
237+
if !ok {
238+
t.Errorf("HTTP server span not exported after handler return, got %v", exported)
239+
return
240+
}
241+
var statusCode int64
242+
for _, attr := range httpSpan.Attributes {
243+
if attr.Key == "http.response.status_code" {
244+
statusCode = attr.Value.AsInt64()
245+
}
246+
}
247+
if statusCode != http.StatusOK {
248+
t.Errorf("HTTP server span response status = %d, want %d", statusCode, http.StatusOK)
249+
}
250+
if requestSpan.Parent.SpanID() != httpSpan.SpanContext.SpanID() {
251+
t.Errorf("A2A request parent = %s, want HTTP span %s", requestSpan.Parent.SpanID(), httpSpan.SpanContext.SpanID())
252+
}
253+
if invocation := exported["invocation"]; invocation.Parent.SpanID() != requestSpan.SpanContext.SpanID() {
254+
t.Errorf("invocation parent = %s, want A2A request span %s", invocation.Parent.SpanID(), requestSpan.SpanContext.SpanID())
237255
}
238256
}
239257

240258
// Without the opt-in, spans stay in the batch processor for its timer to
241259
// export — no per-request flush.
242260
func TestNoPreResponseFlushByDefault(t *testing.T) {
243-
exported := runA2ARequest(t)
244-
if len(exported) != 0 {
245-
t.Errorf("spans exported at handler return without opt-in, got %v", exported)
261+
spans := runA2ARequest(t)
262+
if len(spans) != 0 {
263+
t.Errorf("spans exported at handler return without opt-in, got %v", spans)
246264
}
247265
}
248266

@@ -378,7 +396,7 @@ func (o *exportBoundaryObserver) After(_ context.Context, _ *a2asrv.CallContext,
378396
}
379397
exported := false
380398
for _, span := range o.exporter.GetSpans() {
381-
if strings.HasPrefix(span.Name, "POST ") {
399+
if span.Name == "a2a.request" {
382400
exported = true
383401
}
384402
}
@@ -441,7 +459,7 @@ func TestRequestSpanExportedBeforeQuiescentEvent(t *testing.T) {
441459
select {
442460
case exported := <-observer.observed:
443461
if !exported {
444-
t.Fatal("wrapper span was not exported before the quiescent event was sent")
462+
t.Fatal("A2A request span was not exported before the quiescent event was sent")
445463
}
446464
default:
447465
t.Fatal("quiescent event was not observed")

‎go/adk/pkg/a2a/server/tracing.go‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,59 @@ import (
77
a2atype "github.com/a2aproject/a2a-go/v2/a2a"
88
"github.com/a2aproject/a2a-go/v2/a2asrv"
99
"github.com/kagent-dev/kagent/go/pkg/tracing"
10+
"go.opentelemetry.io/otel"
11+
"go.opentelemetry.io/otel/attribute"
1012
"go.opentelemetry.io/otel/trace"
1113
)
1214

1315
// traceFlushInterceptor runs before a response reaches either A2A transport.
1416
// The gateway quiesces on the task event, not on HTTP EOF. End the request
15-
// span here so the batch flush includes it before the actor can be stopped.
16-
// otelhttp's subsequent End is safe and has no effect on an already-ended span.
17+
// span owned by this interceptor so the batch flush includes the invocation
18+
// work before the actor can be stopped. The transport span remains owned by
19+
// otelhttp, which records its response attributes before ending it.
1720
type traceFlushInterceptor struct {
1821
a2asrv.PassthroughCallInterceptor
1922
logger *slog.Logger
2023
}
2124

22-
func (i *traceFlushInterceptor) After(ctx context.Context, _ *a2asrv.CallContext, response *a2asrv.Response) error {
25+
func (i *traceFlushInterceptor) Before(ctx context.Context, callCtx *a2asrv.CallContext, _ *a2asrv.Request) (context.Context, any, error) {
26+
method := callCtx.Method()
27+
if method != "SendMessage" && method != "SendStreamingMessage" {
28+
return ctx, nil, nil
29+
}
30+
ctx, _ = otel.Tracer("github.com/kagent-dev/kagent/go/adk/pkg/a2a/server").Start(
31+
ctx,
32+
"a2a.request",
33+
trace.WithAttributes(attribute.String("a2a.method", method)),
34+
)
35+
return ctx, nil, nil
36+
}
37+
38+
func (i *traceFlushInterceptor) After(ctx context.Context, callCtx *a2asrv.CallContext, response *a2asrv.Response) error {
39+
method := callCtx.Method()
40+
if method != "SendMessage" && method != "SendStreamingMessage" {
41+
return nil
42+
}
43+
2344
var state a2atype.TaskState
2445
switch payload := response.Payload.(type) {
2546
case *a2atype.TaskStatusUpdateEvent:
2647
state = payload.Status.State
2748
case *a2atype.Task:
2849
state = payload.Status.State
29-
default:
30-
return nil
3150
}
32-
if !state.Terminal() && state != a2atype.TaskStateInputRequired && state != a2atype.TaskStateAuthRequired {
51+
quiescent := state.Terminal() || state == a2atype.TaskStateInputRequired || state == a2atype.TaskStateAuthRequired
52+
if method == "SendStreamingMessage" && response.Err == nil && !quiescent {
3353
return nil
3454
}
3555
span := trace.SpanFromContext(ctx)
56+
if state != a2atype.TaskStateUnspecified {
57+
span.SetAttributes(attribute.String("a2a.task.state", string(state)))
58+
}
3659
span.End()
60+
if !quiescent {
61+
return nil
62+
}
3763
if err := tracing.ForceFlush(ctx); err != nil {
3864
i.logger.ErrorContext(ctx, "failed to flush traces before quiescent A2A response", "error", err, "trace_id", span.SpanContext().TraceID().String(), "task_state", state)
3965
}

‎go/core/internal/translator/claude/compiler.go‎

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,8 @@ func (c *Compiler) Compile(ctx context.Context, input *v2translator.HarnessInput
4242
if len(model.Spec.DefaultHeaders) != 0 || !model.Spec.TLS.IsEmpty() || model.Spec.APIKeyPassthrough {
4343
return nil, v2translator.NewValidationError("Claude does not support ModelConfig defaultHeaders, TLS, or apiKeyPassthrough yet")
4444
}
45-
traceConfig, err := v2translator.TraceConfigFromProcess()
46-
if err != nil {
47-
return nil, v2translator.NewValidationError("invalid tracing configuration: %v", err)
48-
}
49-
logConfig, err := v2translator.LogConfigFromProcess()
50-
if err != nil {
51-
return nil, v2translator.NewValidationError("invalid logging configuration: %v", err)
52-
}
45+
telemetryConfig, _ := v2translator.TelemetryConfigFromProcess()
46+
traceConfig, logConfig := telemetryConfig.Traces, telemetryConfig.Logs
5347

5448
providerEnvironment, egress, err := c.provider(ctx, model)
5549
if err != nil {
@@ -66,7 +60,7 @@ func (c *Compiler) Compile(ctx context.Context, input *v2translator.HarnessInput
6660
environment := append([]corev1.EnvVar(nil), providerEnvironment...)
6761
environment = append(environment, mcp.environment...)
6862
for _, variable := range input.Harness.Spec.Env {
69-
if claudeconfig.OwnsEnvironment(variable.Name) {
63+
if claudeconfig.OwnsEnvironment(variable.Name) || v2translator.OwnsTelemetryEnvironment(variable.Name) {
7064
return nil, v2translator.NewValidationError("Harness env %q conflicts with Claude-owned runtime configuration", variable.Name)
7165
}
7266
envVar := corev1.EnvVar{Name: variable.Name}
@@ -86,8 +80,8 @@ func (c *Compiler) Compile(ctx context.Context, input *v2translator.HarnessInput
8680
corev1.EnvVar{Name: env.KagentName.Name(), Value: template.Name + "-" + harness.Name},
8781
corev1.EnvVar{Name: env.KagentNamespace.Name(), Value: template.Namespace},
8882
)
89-
environment = append(environment, traceConfig.Environment()...)
90-
environment = append(environment, logConfig.Environment()...)
83+
environment = append(environment, telemetryConfig.TraceEnvironment()...)
84+
environment = append(environment, telemetryConfig.LogEnvironment()...)
9185
if traceConfig.Enabled || logConfig.Enabled {
9286
tracesExporter := "none"
9387
if traceConfig.Enabled {
@@ -103,17 +97,21 @@ func (c *Compiler) Compile(ctx context.Context, input *v2translator.HarnessInput
10397
corev1.EnvVar{Name: "OTEL_TRACES_EXPORTER", Value: tracesExporter},
10498
corev1.EnvVar{Name: "OTEL_METRICS_EXPORTER", Value: "none"},
10599
corev1.EnvVar{Name: "OTEL_LOGS_EXPORTER", Value: logsExporter},
106-
corev1.EnvVar{Name: "OTEL_LOG_USER_PROMPTS", Value: "1"},
107-
corev1.EnvVar{Name: "OTEL_LOG_TOOL_DETAILS", Value: "1"},
108100
)
109-
if traceConfig.Enabled {
110-
environment = append(environment, corev1.EnvVar{Name: "OTEL_LOG_TOOL_CONTENT", Value: "1"})
111-
}
112-
if logConfig.Enabled {
101+
if telemetryConfig.CaptureSensitiveContent {
113102
environment = append(environment,
114-
corev1.EnvVar{Name: "OTEL_LOG_ASSISTANT_RESPONSES", Value: "1"},
115-
corev1.EnvVar{Name: "OTEL_LOG_RAW_API_BODIES", Value: "1"},
103+
corev1.EnvVar{Name: "OTEL_LOG_USER_PROMPTS", Value: "1"},
104+
corev1.EnvVar{Name: "OTEL_LOG_TOOL_DETAILS", Value: "1"},
116105
)
106+
if traceConfig.Enabled {
107+
environment = append(environment, corev1.EnvVar{Name: "OTEL_LOG_TOOL_CONTENT", Value: "1"})
108+
}
109+
if logConfig.Enabled {
110+
environment = append(environment, corev1.EnvVar{Name: "OTEL_LOG_ASSISTANT_RESPONSES", Value: "1"})
111+
}
112+
}
113+
if telemetryConfig.CaptureRawAPIBodies && logConfig.Enabled {
114+
environment = append(environment, corev1.EnvVar{Name: "OTEL_LOG_RAW_API_BODIES", Value: "1"})
117115
}
118116
}
119117

@@ -150,10 +148,10 @@ func (c *Compiler) Compile(ctx context.Context, input *v2translator.HarnessInput
150148
egress = append(egress, skillEgress...)
151149
egress = append(egress, mcp.egress...)
152150
if traceConfig.Enabled {
153-
egress = append(egress, traceConfig.CollectorHostname())
151+
egress = append(egress, traceConfig.Hostname)
154152
}
155153
if logConfig.Enabled {
156-
egress = append(egress, logConfig.CollectorHostname())
154+
egress = append(egress, logConfig.Hostname)
157155
}
158156
slices.Sort(egress)
159157
egress = slices.Compact(egress)

‎go/core/internal/translator/claude/compiler_test.go‎

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"reflect"
9+
"slices"
910
"strings"
1011
"testing"
1112
"time"
@@ -124,6 +125,7 @@ func TestCompileSupportedProviders(t *testing.T) {
124125
}
125126

126127
func TestCompileTracing(t *testing.T) {
128+
t.Setenv("KAGENT_OTEL_CAPTURE_SENSITIVE_CONTENT", "false")
127129
t.Setenv("OTEL_TRACING_ENABLED", "true")
128130
t.Setenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", "http://collector:4317")
129131
t.Setenv("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL", "grpc")
@@ -148,17 +150,32 @@ func TestCompileTracing(t *testing.T) {
148150
"OTEL_EXPORTER_OTLP_TRACES_PROTOCOL": "grpc", "CLAUDE_CODE_ENABLE_TELEMETRY": "1",
149151
"CLAUDE_CODE_ENHANCED_TELEMETRY_BETA": "1", "OTEL_TRACES_EXPORTER": "otlp",
150152
"OTEL_METRICS_EXPORTER": "none", "OTEL_LOGS_EXPORTER": "none",
151-
"OTEL_LOG_USER_PROMPTS": "1", "OTEL_LOG_TOOL_DETAILS": "1",
152-
"OTEL_LOG_TOOL_CONTENT": "1", "KAGENT_NAME": "assistant-claude",
153+
"KAGENT_NAME": "assistant-claude",
153154
"KAGENT_NAMESPACE": "test", claudeconfig.PreResponseTraceFlushEnvName: "true",
154155
} {
155156
if environment[name] != value {
156157
t.Errorf("environment[%s] = %q, want %q", name, environment[name], value)
157158
}
158159
}
160+
t.Setenv("KAGENT_OTEL_CAPTURE_SENSITIVE_CONTENT", "true")
161+
revision, err = NewCompiler(krt.TestingDummyContext{}, reader).Compile(context.Background(), input)
162+
if err != nil {
163+
t.Fatal(err)
164+
}
165+
environment = map[string]string{}
166+
for _, variable := range revision.Environment {
167+
environment[variable.Name] = variable.Value
168+
}
169+
for _, name := range []string{"OTEL_LOG_USER_PROMPTS", "OTEL_LOG_TOOL_DETAILS", "OTEL_LOG_TOOL_CONTENT"} {
170+
if environment[name] != "1" {
171+
t.Errorf("sensitive trace environment[%s] = %q, want 1", name, environment[name])
172+
}
173+
}
159174
}
160175

161176
func TestCompileLogging(t *testing.T) {
177+
t.Setenv("KAGENT_OTEL_CAPTURE_SENSITIVE_CONTENT", "false")
178+
t.Setenv("KAGENT_OTEL_CAPTURE_RAW_API_BODIES", "false")
162179
t.Setenv("OTEL_LOGGING_ENABLED", "true")
163180
t.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://logs:4318")
164181
t.Setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf")
@@ -183,13 +200,32 @@ func TestCompileLogging(t *testing.T) {
183200
"OTEL_EXPORTER_OTLP_LOGS_PROTOCOL": "http/protobuf", "CLAUDE_CODE_ENABLE_TELEMETRY": "1",
184201
"CLAUDE_CODE_ENHANCED_TELEMETRY_BETA": "1", "OTEL_TRACES_EXPORTER": "none",
185202
"OTEL_LOGS_EXPORTER": "otlp", "OTEL_METRICS_EXPORTER": "none",
186-
"OTEL_LOG_USER_PROMPTS": "1", "OTEL_LOG_TOOL_DETAILS": "1",
187-
"OTEL_LOG_ASSISTANT_RESPONSES": "1", "OTEL_LOG_RAW_API_BODIES": "1",
188203
} {
189204
if environment[name] != value {
190205
t.Errorf("environment[%s] = %q, want %q", name, environment[name], value)
191206
}
192207
}
208+
for _, name := range []string{"OTEL_LOG_USER_PROMPTS", "OTEL_LOG_TOOL_DETAILS", "OTEL_LOG_ASSISTANT_RESPONSES", "OTEL_LOG_TOOL_CONTENT", "OTEL_LOG_RAW_API_BODIES"} {
209+
if _, exists := environment[name]; exists {
210+
t.Fatalf("logging-only revision enables sensitive telemetry %s by default", name)
211+
}
212+
}
213+
214+
t.Setenv("KAGENT_OTEL_CAPTURE_SENSITIVE_CONTENT", "true")
215+
t.Setenv("KAGENT_OTEL_CAPTURE_RAW_API_BODIES", "true")
216+
revision, err = NewCompiler(krt.TestingDummyContext{}, reader).Compile(context.Background(), input)
217+
if err != nil {
218+
t.Fatal(err)
219+
}
220+
environment = map[string]string{}
221+
for _, variable := range revision.Environment {
222+
environment[variable.Name] = variable.Value
223+
}
224+
for _, name := range []string{"OTEL_LOG_USER_PROMPTS", "OTEL_LOG_TOOL_DETAILS", "OTEL_LOG_ASSISTANT_RESPONSES", "OTEL_LOG_RAW_API_BODIES"} {
225+
if environment[name] != "1" {
226+
t.Errorf("sensitive log environment[%s] = %q, want 1", name, environment[name])
227+
}
228+
}
193229
if _, exists := environment["OTEL_LOG_TOOL_CONTENT"]; exists {
194230
t.Fatal("logging-only revision enables trace-based tool content")
195231
}
@@ -236,6 +272,40 @@ func TestCompileRejectsProviderOwnedHarnessEnvironment(t *testing.T) {
236272
}
237273
}
238274

275+
func TestCompileRejectsManagedOTELEnvironment(t *testing.T) {
276+
model := v1alpha3.ModelConfigSpec{
277+
Provider: v1alpha3.ModelProviderAnthropic, Model: "claude-sonnet-4-5",
278+
APIKeySecret: "model-auth", APIKeySecretKey: "api-key",
279+
}
280+
input, reader := testInput(t, model, map[string][]byte{"api-key": []byte("secret")})
281+
value := "http://other-collector:4317"
282+
input.Harness.Spec.Env = []v1alpha3.HarnessEnvVar{{Name: "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", Value: &value}}
283+
284+
_, err := NewCompiler(krt.TestingDummyContext{}, reader).Compile(context.Background(), input)
285+
var validation *v2translator.ValidationError
286+
if !errors.As(err, &validation) {
287+
t.Fatalf("Compile() error = %v, want managed OTEL environment conflict", err)
288+
}
289+
}
290+
291+
func TestCompileAllowsUnmanagedOTELEnvironment(t *testing.T) {
292+
model := v1alpha3.ModelConfigSpec{
293+
Provider: v1alpha3.ModelProviderAnthropic, Model: "claude-sonnet-4-5",
294+
APIKeySecret: "model-auth", APIKeySecretKey: "api-key",
295+
}
296+
input, reader := testInput(t, model, map[string][]byte{"api-key": []byte("secret")})
297+
value := "department=engineering"
298+
input.Harness.Spec.Env = []v1alpha3.HarnessEnvVar{{Name: "OTEL_RESOURCE_ATTRIBUTES", Value: &value}}
299+
300+
revision, err := NewCompiler(krt.TestingDummyContext{}, reader).Compile(context.Background(), input)
301+
if err != nil {
302+
t.Fatal(err)
303+
}
304+
if !slices.Contains(revision.Environment, corev1.EnvVar{Name: "OTEL_RESOURCE_ATTRIBUTES", Value: value}) {
305+
t.Fatalf("unmanaged OTEL environment missing from revision: %#v", revision.Environment)
306+
}
307+
}
308+
239309
func TestCompileRootSkillsAndPluginSelections(t *testing.T) {
240310
model := v1alpha3.ModelConfigSpec{
241311
Provider: v1alpha3.ModelProviderAnthropic, Model: "claude-sonnet-4-5",

0 commit comments

Comments
 (0)