diff --git a/core/src/telemetry/tracing.ts b/core/src/telemetry/tracing.ts index 4d97440bc..a284d3d2b 100644 --- a/core/src/telemetry/tracing.ts +++ b/core/src/telemetry/tracing.ts @@ -397,14 +397,21 @@ export function runAsyncGeneratorWithOtelContext( /** * Determines whether to add request/response content to spans. * + * Matches adk-python, which normalizes the same variable with `.strip().lower()` + * and tests it against a falsy set of `{'0', 'false'}` rather than against an + * affirmative allow-list (see `src/google/adk/telemetry/context.py`). + * * Defaults to true for now to preserve backward compatibility. * Once prompt and response logging is well established in ADK, we might start * a deprecation of request/response content in spans by switching the default * to false. * - * @returns false only when ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS is explicitly set to 'false' or '0' + * @returns false only when ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS is set to + * 'false' or '0' (case-insensitive, surrounding whitespace ignored). */ function shouldAddRequestResponseToSpans(): boolean { - const envValue = process.env.ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS || 'true'; - return envValue === 'true' || envValue === '1'; + const envValue = (process.env.ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS ?? 'true') + .trim() + .toLowerCase(); + return envValue !== 'false' && envValue !== '0'; } diff --git a/core/test/telemetry/tracing_test.ts b/core/test/telemetry/tracing_test.ts index 21a64b40d..add5f6dc4 100644 --- a/core/test/telemetry/tracing_test.ts +++ b/core/test/telemetry/tracing_test.ts @@ -68,9 +68,9 @@ interface CaptureEnvCase { * Every value of ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS that the current * implementation distinguishes, and the payload capture it selects. * - * `''` and `'TRUE'` are the non-obvious rows: the empty string is falsy so it - * takes the `|| 'true'` default, while `'TRUE'` fails the case-sensitive - * comparison and therefore disables capture. + * The gate is an opt-out: capture stays on unless the value is `'false'` or + * `'0'` after trimming and lowercasing. An unrecognized value is therefore not + * an error condition, it resolves to the default. */ const CAPTURE_ENV_CASES: CaptureEnvCase[] = [ { @@ -105,13 +105,65 @@ const CAPTURE_ENV_CASES: CaptureEnvCase[] = [ }, { envValue: 'TRUE', + captureEnabled: true, + description: "captures payloads when the variable is 'TRUE'", + }, + { + envValue: 'True', + captureEnabled: true, + description: "captures payloads when the variable is 'True'", + }, + { + envValue: 'FALSE', captureEnabled: false, - description: "redacts payloads when the variable is 'TRUE' (case matters)", + description: "redacts payloads when the variable is 'FALSE'", }, { - envValue: 'not-a-boolean', + envValue: ' true', + captureEnabled: true, + description: 'captures payloads when the variable is padded with a space', + }, + { + envValue: 'true\n', + captureEnabled: true, + description: 'captures payloads when the variable has a trailing newline', + }, + { + envValue: ' false ', + captureEnabled: false, + description: 'redacts payloads when the disabling value is padded', + }, + { + envValue: '0\n', captureEnabled: false, - description: 'redacts payloads when the variable is unrecognized', + description: 'redacts payloads when the disabling value has a newline', + }, + { + envValue: ' ', + captureEnabled: true, + description: 'captures payloads when the variable is whitespace only', + }, + { + envValue: 'yes', + captureEnabled: true, + description: "captures payloads when the variable is 'yes'", + }, + { + // 'no' is not a supported disabling value in either SDK, so it resolves to + // the default. Operators relying on it must switch to 'false'. + envValue: 'no', + captureEnabled: true, + description: "captures payloads when the variable is 'no'", + }, + { + envValue: 'treu', + captureEnabled: true, + description: "captures payloads when the variable misspells 'true'", + }, + { + envValue: 'not-a-boolean', + captureEnabled: true, + description: 'captures payloads when the variable is unrecognized', }, ];