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
126127func 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
161176func 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+
239309func TestCompileRootSkillsAndPluginSelections (t * testing.T ) {
240310 model := v1alpha3.ModelConfigSpec {
241311 Provider : v1alpha3 .ModelProviderAnthropic , Model : "claude-sonnet-4-5" ,
0 commit comments